当前位置: 首页 > 知识库问答 >
问题:

树莓派串行通信与Arduino与Python

桑睿识
2023-03-14

我想用Python在我的树莓派和Arduino之间进行交流。到目前为止,Arduino成功地向Raspberry Pi发送了一条串行消息,并使用Python中的ser.readline()函数读取消息。但是当我想用IF语句闪烁连接到树莓派的led时,它就不起作用了

这是我的Arduino代码:

 String data="hello";

 void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 }

 void loop() {
 // put your main code here, to run repeatedly:
 Serial.println(data);//data that is being Sent
 delay(5000);
 }

这是在我的Raspberry Pi上运行的Python代码:

 import serial
 import RPi.GPIO as GPIO
 import time

 LedPin = 11    # pin11
 ser=serial.Serial("/dev/ttyUSB0",9600)  #change ACM number as found from ls /dev/tty/ACM*
 ser.baudrate=9600
 def setup():
    GPIO.setmode(GPIO.BOARD)       # Set the board mode to numbers pins by physical location
    GPIO.setup(LedPin, GPIO.OUT)   # Set pin mode as output
    GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led

 def blink():
            print 'led on'
            GPIO.output(LedPin, GPIO.LOW)   # led on
            time.sleep(1.0)                 # wait 1 sec
            print 'led off'
            GPIO.output(LedPin, GPIO.HIGH)  # led off
            time.sleep(1.0)                 # wait 1 sec


 setup()
 while True:
    serialmessage = ser.readline()
    print("serial message is " + serialmessage)

if serialmessage == "hello":
    print("message recieved")
    blink()

这是我在终端中看到的:终端图像

我已经找了几个小时试图找到解决办法,但运气不好。我也刚刚开始用Python编程。谢谢你的时间。

共有1个答案

滑令
2023-03-14
import serial                                                                                          
import RPi.GPIO as GPIO                                                                                
import time                                                                                            

LedPin = 11    # pin11                                                                                 
ser=serial.Serial("/dev/ttyUSB0",9600)  #change ACM number as found from ls /dev/tty/ACM*              
ser.baudrate=9600                                                                                      
def setup():                                                                                           
   GPIO.setmode(GPIO.BOARD)       # Set the board mode to numbers pins by physical location            
   GPIO.setup(LedPin, GPIO.OUT)   # Set pin mode as output                                             
   GPIO.output(LedPin, GPIO.HIGH) # Set pin to high(+3.3V) to off the led                              

def blink():                                                                                           
           print 'led on'                                                                              
           GPIO.output(LedPin, GPIO.LOW)   # led on                                                    
           time.sleep(1.0)                 # wait 1 sec                                                
           print 'led off'                                                                             
           GPIO.output(LedPin, GPIO.HIGH)  # led off                                                   
           time.sleep(1.0)                 # wait 1 sec                                                


setup()                                                                                                
while True:                                                                                            
   serialmessage = ser.readline()                                                                      
   print("serial message is " + serialmessage)                                                         

   if serialmessage == "hello":                                                                        
       print("message recieved")                                                                       
       blink()

Python使用行间距来检测代码块。

您需要将一个if语句与对blink方法的调用放在一个time循环中。

你也可以通过在树莓pi上运行命令来检查传入的串行数据

sudo cat /dev/ttyUSB0
 类似资料:
  • 我已经设法从我的arduino(Uno)到我的Raspberry Pi 3,通过串口进行了写操作。 如果我在pi端使用相同的python脚本,在arduino端使用相同的Sketch,但使用Teensy,我无法从Arduino读取任何输出。 根据串行通信,arduino Uno和teensy之间有什么区别吗? Arduino草图: 我的Pi上的Python脚本: 此代码适用于我的Arduino U

  • 我正在尝试通过串行通信来溜冰树莓皮和阿杜伊诺。我的目的是用户控制Raspberry Pi的Arduino LED。 我找到了一个串行通信的示例代码,它每2秒自动向Arduino发送一个字符串。但我想做两件事: 更改发送的值,而不是Hello。 用户可以随时发送值,而不是自动发送。 你能帮我吗?我不擅长节点。js。

  • Linux 是使用最广泛的开源操作系统,而树莓派是新兴的开放硬件平台。两者相互结合,能激起很多有趣的火花。这一部分中,我将介绍树莓派的基本使用方法。

  • 我正在尝试使用arduino通过串行通信从泼妇软件获取传入数据。并将数据移动到其他12个arduino,如果我将泼妇的通道号保留为43,但我需要控制480个通道,它就可以工作。所以第一个arduino控制43个通道,下一个是44-87,依此类推。 这是我的主代码:'ulusetup(){Serial.begin(9600); }}//输出

  • 我在Raspberry Pi(Python脚本)和Arduino nano/uno之间的串行通信方面遇到一些问题。我的两个设备都通过USB端口连接,当我直接从arduino IDE串行监视器发送命令时,arduino草图始终正确响应: 但是,当我运行python脚本,他发送相同的命令时,草图以随机方式响应。 覆盆子终端: Arduino串行监视器: Mi代码为: Arduino草图: Python

  • 我试图建立RaspberryPI B(主)和Arduino Uno(从)之间的全双工SPI通信。 主机侧代码: 从机端码 所以,简单地说,我希望如果我向throw MOSI行发送一个输入(在本例中为7),Arduino会增加var x,并用x的值回复我。 但是我的输出看起来像这样: 换句话说,如果我发送7,Arduino增量x,则在while行中循环并返回相同的输入值。如果我再次发送一个值,Ard