如何用简单的基本脚本测试pySerial?

2024-09-30 01:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试测试pySerial以从COM5读取,我在网上查找了一些示例代码,我使用pip命令安装了pySerial,但出现错误:

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)

这是我试图作为测试目的运行的代码:

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

我的目的是让她能够毫无错误地收听COM5,然后我会添加一些代码来详细说明数据,但目前我还停留在一开始


Tags: 代码inpyinitlibpackageslineserial

热门问题