Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 阿德鲁尼奥LED控制_Python_Arduino - Fatal编程技术网

Python 阿德鲁尼奥LED控制

Python 阿德鲁尼奥LED控制,python,arduino,Python,Arduino,我试图编写一个代码,使一个LED灯闪烁3次,该LED灯连接到Python中的Arduino。我能够做到这一点,但即使在python代码停止运行后,LED仍会持续闪烁。附上代码,有什么建议吗 Python代码 import serial #import the pyserial library connected = False #this will represent whether or not ardunio is connected to the system ser = serial.S

我试图编写一个代码,使一个LED灯闪烁3次,该LED灯连接到Python中的Arduino。我能够做到这一点,但即使在python代码停止运行后,LED仍会持续闪烁。附上代码,有什么建议吗

Python代码

import serial #import the pyserial library
connected = False #this will represent whether or not ardunio is connected to the system
ser = serial.Serial("COM3",9600) #open the serial port on which Ardunio is connected to, this will coommunicate to the serial port where ardunio is connected, the number which is there is called baud rate, this will provide the speed at which the communication happens
while not connected: #you have to loop until the ardunio gets ready, now when the ardunio gets ready, blink the led in the ardunio
    serin = ser.read()
    connected = True
    ser.write('1') #this will blink the led in the ardunio
    while ser.read() == '1': #now once the led blinks, the ardunio gets    message back from the serial port and it get freed from the loop!
ser.read()
ser.close()
Ardunio代码

void setup() {
 Serial.begin(9600);
 pinMode(10,OUTPUT);
 Serial.write('1');
}
void loop() {
if(Serial.available()>0){
  digitalWrite(10,HIGH);
  delay(500);
  digitalWrite(10,LOW);
  delay(500);
  digitalWrite(10,HIGH);
  delay(500);
  digitalWrite(10,LOW);
  delay(500);
  digitalWrite(10,HIGH);
  delay(500);
  digitalWrite(10,LOW);
  Serial.write('0');
}
}

的条件始终为真,因为始终存在相同的旧数据可用。您需要读取数据以将其从缓冲区中删除。

Arduino不应在您的示例中执行任何串行写入。它应该持续执行Serial.read。如果该值为1,则打开LED,如果为0,则关闭LED。Python应该写入1和0来打开/关闭led@Ramast为什么呢?我不明白为什么设备不能响应命令。这是一个愚蠢的建议,然后有一天你会像我一样,被迫和一个对任何事情都没有反应的中国工业设备交谈。没有错误,没有确认,什么都没有。这真的很有趣…@Pieget,因为连续发送零不是响应,而是阻止代码按预期工作的原因。这不是有效的Python代码。@marcosidaho Greu_gor可能指出您的代码示例没有正确缩进。您的意思是刷新内存吗?flush的名称不正确,不能用当前的Arduino版本请参见文档中的注释。试试拉马斯特对你问题的第一个评论中的建议。从串行数据读取将从输入缓冲区获取数据。一旦为空,available将返回0,条件的计算结果为false:您的闪烁代码将不会执行。@flush我稍微修改了audrino代码,我从Serial.write'1'更改为Serial.read。但是现在python中的进程并没有断开连接!