繁体   English   中英

无法使用pyserial在arduino和python之间进行通信

[英]Cannot communicate between arduino and python using pyserial

我已经在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);
     }
   } 

在arduino论坛上查看这个很棒的教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM