我想从PySerial文档中运行这个示例程序来打开串口。来源:http://pyserial.sourceforge.net/shortintro.html 我试着在Python 2.7和3.4版本中运行代码,但仍然得到相同的错误。Python PySerial,如何打开串行端口?
>>> 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
我收到以下错误运行代码的第二行之后:
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.')
试过数使用另一个程序,如超级终端或附带的MiniTerm连接到串行端口,以确保它们可用?你在使用什么操作系统? – skrrgwasme
您的系统实际上是否有COM1端口? –
我如何知道我的系统是否有COM1端口? – George