回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正试图从Python3.6向Arduino发送一条消息,Arduino需要响应。你知道吗</p>
<p>目前,我能够发送到Arduino和它的反应,但不是得到一个完整的信息回来,我得到1字节。你知道吗</p>
<p>Arduino代码:</p>
<pre class="lang-cpp prettyprint-override"><code>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;
}
}
</code></pre>
<p>Python代码:</p>
<pre class="lang-py prettyprint-override"><code>## 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()
</code></pre>
<p>从我在python代码中留下的注释中可以看到。我没有得到一个完整的值数组,只得到161。你知道吗</p>
<p>有人对我的错误有什么建议吗?你知道吗</p>