我想從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