我已经在arduino上开始了一个项目,我要求它与python进行通信。我已经浏览了网页并在arduino python串口通信上找到了一个示例代码,其中,当输入1时它会点亮LED。 python和arduino代码都在工作,但LED没有点亮。董事会运作正常,因为我尝试了其他基本的例子
Arduino代码:
一世
void setup()
{
pinMode(12,OUTPUT);
digitalWrite(12,LOW);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
if(Serial.read() == 1)
{
digitalWrite(12,HIGH);
delay(2000);
}
}
else
{
digitalWrite(12,LOW);
}
}
Python代码:
import serial
import time # Required to use delay functions
arduinoSerialData = serial.Serial('/dev/ttyACM0', 9600) # Create Serial port object called arduinoSerialData
time.sleep(2) # wait for 2 secounds for the communication to get established
print ("Enter 1 to turn ON LED and 0 to turn OFF LED")
while 1: # Do this forever
var =input() # get input from user
var=var.encode()
arduinoSerialData.write(var)
试试这个
if(Serial.available() > 0)
{
if((char)Serial.read() == '1')
{
digitalWrite(12,HIGH);
delay(2000);
}
}