0

I'm trying to test pySerial to read from COM5, I've looked up some sample code online, I've installed pySerial using pip command but I get the folliwing error:

c:\Serial Test>link.py
Traceback (most recent call last):
  File "C:\Serial Test\link.py", line 9, in <module>
    timeout=0)
  File "C:\Python36\lib\site-packages\serial\serialwin32.py", line 31, in __init__
    super(Serial, self).__init__(*args, **kwargs)
  File "C:\Python36\lib\site-packages\serial\serialutil.py", line 240, in __init__
    self.open()
  File "C:\Python36\lib\site-packages\serial\serialwin32.py", line 62, in open
    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM5': FileNotFoundError(2, 'Impossibile trovare il file specificato.', None, 2)

This is the code I'm trying to run as a test purpose:

import serial

ser = serial.Serial(
    port='COM5',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr);

count=0
while count <100:
    line=ser.readline()
    print (line)
    count+=1

ser.close()

My purpose her is just being able to listen to COM5 without error, then I'll add some code to elaborate the data, but for now I'm stuck at very beginning.

S. W. G.
  • 433
  • 1
  • 6
  • 19

1 Answers1

0

Looks like pySerial was unable to find something at COM5.

Are you sure you have your device connected at COM5? You can try checking the available ports as explained in this question.

  • cable is connected but just doesn't receive data all the time, it depends on another device sending data when activated. – S. W. G. Jul 04 '19 at 21:48
  • The problem is the port your cable is associated with is not called COM5, go to the control panel's Device Manager, disconnect and reconnect it and observe the name of the port that shows up; it won't be COM5 but a different name, maybe COM4 or COM9. Change the `port` variable on your code accordingly and it will work. – Marcos G. Jul 05 '19 at 07:18
  • You can do it dinamically, as shown in the link. That way you won't have to keep reconfiguring your program. – Nicolas Abril Jul 05 '19 at 23:20