ap-- / python-seabreeze

Python module for OceanOptics spectrometers
https://python-seabreeze.readthedocs.io
MIT License
201 stars 80 forks source link

HDX spectrometer connected via USB errors after manually interrupting connection #239

Open fredericjs opened 3 months ago

fredericjs commented 3 months ago

spectrometer and system information

current problem

I have some code to read data from my HDX spectrometer using pyseabreeze backend. The issue I get is when interrupting the program execution and running the program again, I get the following error:

Traceback (most recent call last):
  File "C:\Development\Spectrometer\main.py", line 72, in <module>
    spec = Spectrometer.from_first_available()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\spectrometers.py", line 109, in from_first_available
    for dev in list_devices():
               ^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\spectrometers.py", line 57, in list_devices
    return api.list_devices()
           ^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\api.py", line 120, in list_devices
    dev.open()
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\devices.py", line 381, in open
    self._serial_number = self.get_serial_number()
                          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\devices.py", line 424, in get_serial_number
    msg = protocol.query(0x00000101)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\protocol.py", line 392, in query
    return self.receive(timeout_ms=timeout_ms)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\protocol.py", line 328, in receive
    remaining_bytes, checksum_type = self._check_incoming_message_header(
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\protocol.py", line 490, in _check_incoming_message_header
    raise SeaBreezeError('Header start_bytes wrong: "%d"' % data[0])
seabreeze.pyseabreeze.exceptions.SeaBreezeError: Header start_bytes wrong: "44889"

When I run the spectrometer in a separate daemon thread and then interrupt the program execution, I get the following error when restarting the program:

Traceback (most recent call last):
  File "C:\Development\UoB_Spectrometer\main.py", line 72, in <module>
    spec = Spectrometer.from_first_available()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\UoB_Spectrometer\venv\Lib\site-packages\seabreeze\spectrometers.py", line 111, in from_first_available
    return cls(dev)
           ^^^^^^^^
  File "C:\Development\UoB_Spectrometer\venv\Lib\site-packages\seabreeze\spectrometers.py", line 80, in __init__
    self.open()  # always open the device here to allow caching values
    ^^^^^^^^^^^
  File "C:\Development\UoB_Spectrometer\venv\Lib\site-packages\seabreeze\spectrometers.py", line 372, in open
    self._dev.open()
  File "C:\Development\UoB_Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\devices.py", line 381, in open
    self._serial_number = self.get_serial_number()
                          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\UoB_Spectrometer\venv\Lib\site-packages\seabreeze\pyseabreeze\devices.py", line 426, in get_serial_number
    serial_len = ord(msg)
                 ^^^^^^^^
TypeError: ord() expected a character, but string of length 8 found

It seems to me that there is unread data in the USB buffer and the next time the program starts, it mistakenly reads that data when trying to read the header. The error occurs only when the program was halted during reading. The issue disappears when unplugging the USB connector and plugging it back in.

steps to reproduce

Use the minimal code example and force terminate the program during the while loop. Afterwards, the spectrometer cannot be correctly connected. Even the OceanView software at this point can't detect the spectrometer.

minimal code example and error (very helpful if available)

import seabreeze
seabreeze.use('pyseabreeze')
from seabreeze.spectrometers import Spectrometer

spec = Spectrometer.from_first_available()
x = spec.wavelengths()
while True:
    y = spec.intensities() # Interrupt the program here
ap-- commented 1 month ago

Currently python-seabreeze still has some issues with the OceanHDX models.

If you can produce a reproducible error case that does not require manual intervention, it should be possible to start debugging where the issue occurs. Maybe we just need to flush the usb buffer on system exit.

I don't have a OceanHDX spectrometer myself, but I can help you debug your use case.

Cheers, Andreas 😃

ap-- commented 1 week ago

The HDX spectrometers now work via the network interface!