Python Forum

Full Version: Cannot attach to com port with Pyserial
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using a microcontroller to send some data to a PC using Virtual Com Port (USB interface). I am using pyserial to receive the data on the PC. I am having issues with the com port. Sometimes, I am able to connect to it, but other times I am not (even if I restart my PC). Would anyone here be able to advice?
My code to attach to serial port:
while True:
    try:
        ser_handler = ser.Serial(port= "com4", baudrate=115200, bytesize=ser.EIGHTBITS, parity=ser.PARITY_NONE, stopbits=ser.STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, write_timeout=None, dsrdtr=False, inter_byte_timeout=None, exclusive=None)
        break
    except ser.SerialException as excp:
        print('Serial Exception')
        print (excp.args)
        time.sleep(2)
    else:
        print('Some other error')
        time.sleep(2)
The error I get:
Error:
Serial Exception ("Cannot configure port, something went wrong. Original message: OSError(22, 'The parameter is incorrect.', None, 87)",)
I know it has something to do with the release of com port. Any help would be appreciated.
Thanks,
AQ