Python 接收串口数据
Python receiving serial data
我正在尝试从 Python 3.6 向 Arduino 发送消息,而 Arduino 需要响应。
目前我可以发送给 Arduino 并且它有响应,但我没有收到完整的消息,而是收到了 1 个字节。
Arduino 代码:
byte FullResponse[] = {0xA1, 0xA3, 0xA4, 0x3B, 0x1F, 0xB4, 0x1F, 0x74, 0x19,
0x79, 0x44, 0x9C, 0x1F, 0xD4, 0x4A, 0xC0};
byte SerialBuf[11] = {0,0,0,0,0,0,0,0,0,0,0};
int i = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
//Reads values from the Serial port, if there are any
for(int i=0; i<=10; i++){
while(!Serial.available());
SerialBuf[i] = Serial.read();
}
//Sends the full response back if EF is the 8th byte
if(SerialBuf[8] == 0xEF){
Serial.write(FullResponse, sizeof(FullResponse));
SerialBuf[8] = 15;
}
}
Python代码:
## import the serial library
import serial
import time
Response = []
FullResponse = bytes([0xC0, 0x43, 0xA1, 0x04, 0x0A, 0x90, 0x00, 0x30, 0xEF, 0xFF, 0xC0])
## Serial port is
ser = serial.Serial(port='COM3', baudrate=115200)
time.sleep(2)
#converts values to byte array and sends to arduino
ConArray = bytearray(FullResponse)
ser.write(ConArray[0:len(FullResponse)])
time.sleep(1)
if ser.inWaiting():
for Val in ser.read():
Response.append(Val)
print(*Response) #prints 161
print(len(Response)) #prints 1
time.sleep(1)
## close the port and end the program
ser.close()
从我在 python 代码中留下的评论中可以看出。我没有得到整个值数组,而是只收到 161.
有人对我哪里出错有什么建议吗?
如果您阅读 Serial.read()
函数的 documentation,您会看到它的默认参数是“1”。所以它按预期只从串行端口读取 1 个字节。您应该首先检查(或等到)是否有足够的字节并将要读取的字节数传递给 read()
函数。
通过将 if 语句更改为 while 语句,代码现在可以读取所有保存的字节。谢谢你的帮助嘿哟
## import the serial library
import serial
import time
Response = []
FullResponse = bytes([0xC0, 0x43, 0xA1, 0x04, 0x0A, 0x90, 0x00, 0x30, 0xEF, 0xFF, 0xC0])
## Serial port is
ser = serial.Serial(port='COM3', baudrate=115200)
time.sleep(2)
#converts values to byte array and sends to arduino
ConArray = bytearray(FullResponse)
ser.write(ConArray[0:len(FullResponse)])
time.sleep(1)
while ser.inWaiting(): ########### Changed from if to while
for Val in ser.read():
Response.append(Val)
print(*Response) #prints 161
print(len(Response)) #prints 1
time.sleep(1)
## close the port and end the program
ser.close()
我正在尝试从 Python 3.6 向 Arduino 发送消息,而 Arduino 需要响应。
目前我可以发送给 Arduino 并且它有响应,但我没有收到完整的消息,而是收到了 1 个字节。
Arduino 代码:
byte FullResponse[] = {0xA1, 0xA3, 0xA4, 0x3B, 0x1F, 0xB4, 0x1F, 0x74, 0x19,
0x79, 0x44, 0x9C, 0x1F, 0xD4, 0x4A, 0xC0};
byte SerialBuf[11] = {0,0,0,0,0,0,0,0,0,0,0};
int i = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
//Reads values from the Serial port, if there are any
for(int i=0; i<=10; i++){
while(!Serial.available());
SerialBuf[i] = Serial.read();
}
//Sends the full response back if EF is the 8th byte
if(SerialBuf[8] == 0xEF){
Serial.write(FullResponse, sizeof(FullResponse));
SerialBuf[8] = 15;
}
}
Python代码:
## import the serial library
import serial
import time
Response = []
FullResponse = bytes([0xC0, 0x43, 0xA1, 0x04, 0x0A, 0x90, 0x00, 0x30, 0xEF, 0xFF, 0xC0])
## Serial port is
ser = serial.Serial(port='COM3', baudrate=115200)
time.sleep(2)
#converts values to byte array and sends to arduino
ConArray = bytearray(FullResponse)
ser.write(ConArray[0:len(FullResponse)])
time.sleep(1)
if ser.inWaiting():
for Val in ser.read():
Response.append(Val)
print(*Response) #prints 161
print(len(Response)) #prints 1
time.sleep(1)
## close the port and end the program
ser.close()
从我在 python 代码中留下的评论中可以看出。我没有得到整个值数组,而是只收到 161.
有人对我哪里出错有什么建议吗?
如果您阅读 Serial.read()
函数的 documentation,您会看到它的默认参数是“1”。所以它按预期只从串行端口读取 1 个字节。您应该首先检查(或等到)是否有足够的字节并将要读取的字节数传递给 read()
函数。
通过将 if 语句更改为 while 语句,代码现在可以读取所有保存的字节。谢谢你的帮助嘿哟
## import the serial library
import serial
import time
Response = []
FullResponse = bytes([0xC0, 0x43, 0xA1, 0x04, 0x0A, 0x90, 0x00, 0x30, 0xEF, 0xFF, 0xC0])
## Serial port is
ser = serial.Serial(port='COM3', baudrate=115200)
time.sleep(2)
#converts values to byte array and sends to arduino
ConArray = bytearray(FullResponse)
ser.write(ConArray[0:len(FullResponse)])
time.sleep(1)
while ser.inWaiting(): ########### Changed from if to while
for Val in ser.read():
Response.append(Val)
print(*Response) #prints 161
print(len(Response)) #prints 1
time.sleep(1)
## close the port and end the program
ser.close()