我有2个脚本,一个用于将数据发送到arduino,另一个用于从arduino接收数据。当执行接收脚本时无法产生预期的结果。
# script1 code fro sending data to arduino
arduinoData = serial.Serial('/dev/ttyACM1', 115200)
while i < 3:
print('started recording')
arduinoData.write('1')
time.sleep(2)
print('start rotating')
x = datetime.datetime.now().time()
t1 = ((float(x.hour) * 3600) + (float(x.minute) * 60) +
float(str(x.second) + '.' + str(x.microsecond)))
ti.append(t1)
time.sleep(2)
print('end')
time.sleep(2)
arduinoData.write('0')
print('over')
# script2 code for recieving data from arduino:
arduinoData = sl.Serial('/dev/ttyACM1', 115200) # Creating our
serial object named arduinoData
while (arduinoData.inWaiting() != 0):
while i < 3:
arduinoString = arduinoData.readline()
x = datetime.datetime.now().time()
if (arduinoString != '1'):
data = int(arduinoString)
val.append(data)
t1 = ((float(x.hour) * 3600) + (float(x.minute) * 60) + float(str(x.second) + '.' + str(x.microsecond)))
time.append(t1)
else:
t.append(time)
v.append(val)
print(t)
print(v)
val.clear()
time.clear()
i += 1
# arduino code:
void setup()
{
Serial.begin(115200); //turn on serial monitor
}
void loop()
{
if (Serial.available()){
if(Serial.read() == '0') {
Serial.println('1');
}
else if(Serial.read()== '1') {
raw = analogRead(analogPin);
Serial.println(raw);
}}
else
{
raw = analogRead(analogPin);
Serial.println(raw);
}
}
结果应打印出列表t和v。运行脚本时,脚本2终止,并在脚本1终止后继续运行evnen。那是它不会进入其他部分。我不知道为什么?请帮我。 有时在运行python错误时如下 无法将字符串转换为int。 请提供建议