Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygatt needs all the time three attempts to open serial port with BLED112 #347

Open
buhln opened this issue Mar 14, 2024 · 0 comments
Open

Comments

@buhln
Copy link

buhln commented Mar 14, 2024

In my application pygatt needs all the time three attempts to open serial port with BLED112 but at the end it is able to connect to the device. Any ideas how to fix this?

2024-03-14 12:49:42,189 - INFO: Auto-detecting serial port for BLED112
2024-03-14 12:49:42,194 - DEBUG: Found 1 serial USB devices
2024-03-14 12:49:42,194 - DEBUG: Checking serial USB device: /dev/ttyACM0 - Low Energy Dongle - CDC control
2024-03-14 12:49:42,194 - DEBUG: USB device: Low Energy Dongle - CDC control VID=0x2458 PID=0x0001 on /dev/ttyACM0
2024-03-14 12:49:42,194 - INFO: Found BLED112 on serial port /dev/ttyACM0
2024-03-14 12:49:42,949 - DEBUG: Opening connection to serial port (attempt 1)
2024-03-14 12:49:42,949 - INFO: Auto-detecting serial port for BLED112
2024-03-14 12:49:42,959 - DEBUG: Found 1 serial USB devices
2024-03-14 12:49:42,960 - DEBUG: Checking serial USB device: /dev/ttyACM0 - n/a
2024-03-14 12:49:42,960 - DEBUG: Failed to open serial port
Traceback (most recent call last):
  File "//main.py", line 72, in connect_gauge
    self.device = self.adapter.connect(self.Device_Address, address_type=pygatt.BLEAddressType.random)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 456, in connect
    raise exc
pygatt.exceptions.NotConnectedError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 171, in _open_serial_port
    serial_port = self._serial_port or self._detect_device_port()
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 147, in _detect_device_port
    raise BGAPIError("Unable to auto-detect BLED112 serial port")
pygatt.backends.bgapi.exceptions.BGAPIError: Unable to auto-detect BLED112 serial port
2024-03-14 12:49:43,212 - DEBUG: Opening connection to serial port (attempt 2)
2024-03-14 12:49:43,213 - INFO: Auto-detecting serial port for BLED112
2024-03-14 12:49:43,220 - DEBUG: Found 1 serial USB devices
2024-03-14 12:49:43,220 - DEBUG: Checking serial USB device: /dev/ttyACM0 - n/a
2024-03-14 12:49:43,220 - DEBUG: Failed to open serial port
Traceback (most recent call last):
  File "//main.py", line 72, in connect_gauge
    self.device = self.adapter.connect(self.Device_Address, address_type=pygatt.BLEAddressType.random)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 456, in connect
    raise exc
pygatt.exceptions.NotConnectedError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 171, in _open_serial_port
    serial_port = self._serial_port or self._detect_device_port()
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pygatt/backends/bgapi/bgapi.py", line 147, in _detect_device_port
    raise BGAPIError("Unable to auto-detect BLED112 serial port")
pygatt.backends.bgapi.exceptions.BGAPIError: Unable to auto-detect BLED112 serial port
2024-03-14 12:49:43,471 - DEBUG: Opening connection to serial port (attempt 3)
2024-03-14 12:49:43,471 - INFO: Auto-detecting serial port for BLED112
2024-03-14 12:49:43,477 - DEBUG: Found 1 serial USB devices
2024-03-14 12:49:43,477 - DEBUG: Checking serial USB device: /dev/ttyACM0 - Low Energy Dongle - CDC control
2024-03-14 12:49:43,477 - DEBUG: USB device: Low Energy Dongle - CDC control VID=0x2458 PID=0x0001 on /dev/ttyACM0
2024-03-14 12:49:43,478 - INFO: Found BLED112 on serial port /dev/ttyACM0
2024-03-14 12:49:43,730 - INFO: Running receiver

Here my code to connect to the device:

# Function to connect gauge
def connect_gauge(self):
    try:
        # attached BGAPI-compatible USB adapter
        self.adapter = pygatt.BGAPIBackend()
        self.adapter.start()
        self.device = self.adapter.connect(self.Device_Address, address_type=pygatt.BLEAddressType.random)
        # Configure device to streaming data
        self.device.char_write_handle(42, bytearray(b'\x01\x00'), wait_for_response=False)
        
    except Exception as error:
        logging.error("An error occurred:", error)
        # If connection not successful sleep for some seconds and try again
        logging.error('Connection to device was not successful -> try again in some seconds')
        # Stop adapter for a fresh restart
        self.adapter.stop()
        time.sleep(1)
        self.connect_gauge()

    else:
        logging.debug('------------------------ Connection to device with address '+ self.Device_Address +' successful established!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant