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

Error in connecting to spectrometer after interrupting program #239

Open
fredericjs opened this issue Mar 13, 2024 · 0 comments
Open

Error in connecting to spectrometer after interrupting program #239

fredericjs opened this issue Mar 13, 2024 · 0 comments

Comments

@fredericjs
Copy link

spectrometer and system information

  • model: HDX
  • operating system: Windows 10 64bit
  • python version: 3.11
  • python-seabreeze version: 2.7.0
  • installed-via: pip

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
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