win32.SetCommState with Pyserial in win10
Asked Answered
C

1

6

Until recently I was happily using Win 7 + Python.x and PySerial.
Lately I upgraded to Win 10, now I have a problem running some Python serial code, which I had working on Win 7.

However, I can even run the simple example from: Full examples of using pySerial package

What I get is the same error, on all PySerial related codes:

Traceback (most recent call last):
  File ".\t.py", line 10, in <module>
    bytesize=serial.SEVENBITS
  File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 31, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Python37\lib\site-packages\serial\serialutil.py", line 240, in __init__
    self.open()
  File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 78, in open
    self._reconfigure_port()
  File "C:\Python37\lib\site-packages\serial\serialwin32.py", line 222, in _reconfigure_port
    'Original message: {!r}'.format(ctypes.WinError()))
serial.serialutil.SerialException: Cannot configure port, something went wrong. Original message: OSError(22, 'The parameter is incorrect.', None, 87)

However, I found out that the reason for this is the exception raised by a call win32.SetCommState(self._port_handle, ctypes.byref(comDCB)): from serialwin32.py

This call returns 0, indicating something is wrong, but when I comment it out, then I can get the serial communication working again.

Anyone else experienced this?

Crop answered 6/12, 2018 at 10:57 Comment(4)
I have the exact same issue. It's a bummer you didn't get any answers. I wish the error message said which parameter was incorrect.Metacarpus
I get the same error with following code: with serial.Serial("COM4", baudrate=115200, timeout=1) as ser:Teammate
I haven't faced any issue, can you try updating the packagesWee
You may have better luck asking on GitHub: github.com/pyserial/pyserial/issues?q=is%3Aissue+is%3AopenSerow
L
1

This seems to be a known issue with pyserial when switching from windows 7 to windows 10. User @BrendanSimo suggested one of the workarounds is to create a new class inherited from serial.Serial class and override _reconfigure_port() method to ignore raised serial.SerialException

class MySerial( serial.Serial ):
    def _reconfigure_port( self, *args, **kwargs ):
        try:
            super()._reconfigure_port( *args, **kwargs )
        except serial.SerialException:
            pass
Longspur answered 26/9 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.