我正在尝试让Python读取连接到串口3上的Arduino Uno,这样就可以在python代码中使用COM3了。我使用的是Python33,Arduino和pySerial 2.7的最新版本。这是Arduino的代码:
void setup() {
Serial.begin(9600); // set the baud rate
Serial.println("Ready"); // print "Ready" once
}
void loop() {
char inByte = ' ';
if(Serial.available()){ // only send data back if data has been sent
char inByte = Serial.read(); // read the incoming data
Serial.println(inByte); // send the data back in a new line so that it is not all one long line
}
delay(100); // delay for 1/10 of a second
}
这是python代码:
import serial
ser = serial.Serial("COM3", 9600)
然后我收到了这个错误:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
ser = serial.Serial("COM3", 9600)
File "C:\Python33\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python33\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python33\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM3': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
这可能很容易解决,我几乎无处不在,我似乎无法找到问题的答案。
答案 0 :(得分:1)
为什么要在Linux机器上使用“COM3”?这是一个Windows端口名称。 Linux / Unix端口名称的格式为/dev/ttyUSB0
。
但是,正如the docs所示,您可以直接使用端口号 - 它们从0开始,因此您可以执行ser = serial.Serial(2, 9600)
。
答案 1 :(得分:0)
如果你打开了arduino ide,那么python可能无法访问该端口。我也使用Processing来解决这个问题。