簡體   English   中英

Raspberry Pi與Arduino和Python的串行通信

[英]Raspberry Pi Serial Communication with Arduino with Python

我想使用Python在Raspberry Pi和Arduino之間進行通信。 到目前為止,Arduino已成功向Raspberry Pi發送了一條串行消息,並使用Python中的ser.readline()函數讀取了該消息。 但是當我想用IF語句閃爍連接到Raspberry Pi的LED時將不起作用

blink()函數及其他所有功能均有效,但代碼不會進入使用字符串變量檢查ser.readline()值的IF語句中

這是我的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()

這是我在終端中看到的: terminal_image

我一直在尋找試圖尋找解決方案的時間,但是沒有運氣。 我也剛剛開始用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使用間距來檢測代碼塊。

您需要在while循環中放置一條帶if語句的調用眨眼方法。

您也可以通過在Raspberry pi上運行命令來檢查傳入的串行數據

sudo cat /dev/ttyUSB0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM