我想通过pyserial将2MB数据文件发送到Arduino,然后将数据发送回Arduino。到目前为止,我已经从Arduino连接到pyserial和Vice Vera。
当我发送仅包含0和1的数据文件时,我可以将数据从Arduino发送到python而没有问题,但是只能从Arduino发送静态整数。
b'VMDPV_1|1_VM'
b'DPV\r\n'
b'VMDPE_1:2:6064:0:6064|6065_VMDPE\r\n'
我在Python中进行交流的代码:
def load_data_to_arduino(self, data):
self.arduino_serial.write(bytes(self.start_flag))
data = [2 if x == 0 else x for x in data]
for number in data[:int(len(data)/1000)]: #Shorter time for testing
#print(number)
encoded_data = bytes(number, 'utf-8')
self.arduino_serial.write(int(number)) # Write serial data to arduino
#self.arduino_serial.write(3) # Also don't work
self.arduino_serial.write(self.end_flag)
发送文件后,我在数据后面寻找一个额外的线程:
def log_arduino_data(self):
while(self.log_arduino):
if(self.arduino_serial.inWaiting()> 0):
bytesToRead = self.arduino_serial.inWaiting() #Also tried normal read(), readline etc.
recieved_data = self.arduino_serial.readline(bytesToRead)
print(recieved_data)
现在是Arduino代码:
void setup() {
Serial.begin(9600);
}
bool run = false;
// the loop function runs over and over again until power down or reset
void loop() {
while (Serial.available()){
delay(30); //delay to allow buffer to fill
if (Serial.available() > 0)
{
int data = Serial.read();
if (data == 6) { //Startflag
run = true;
Serial.println("2");
}
if (run) { //Collecting loop until endflag
if (data != 9) {
Serial.println(data, DEC);
}
else {
run = false;
Serial.println("9");
}
}
}
}
}
我知道消息仅在Arduino的Debug-Mode下发送,但是如果我以Release模式运行Arduino,则无论我发送什么内容,我都只能收到0作为值。
有人知道为什么我只发送0或调试消息吗?