pygatt
pygatt copied to clipboard
pygatt needs all the time three attempts to open serial port with BLED112
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!')