[Python] Python PySerial, How to Open Serial Ports?

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to run this example program from the PySerial Documentation for opening serial ports. Source: http://pyserial.sourceforge.net/shortintro.html I tried running the code in both python version 2.7 and 3.4 but still get same error.

    >>> import serial
    >>> ser = serial.Serial(0) # open first serial port
    >>> print ser.name # check which port was really used
    >>> ser.write("hello") # write a string
    >>> ser.close() # close port


    I get the following error after running the second line of code:

    Traceback (most recent call last):
    File "<pyshell#2>", line 1, in <module>
    ser = serial.Serial(0)
    File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 38, in __init__
    SerialBase.__init__(self, *args, **kwargs)
    File "C:\Python27\lib\site-packages\serial\serialutil.py", line 282, in __init__
    self.open()
    File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
    raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
    SerialException: could not open port 'COM1': WindowsError(2, 'The system cannot find the file specified.')

    Continue reading...

Compartilhe esta Página