码迷,mamicode.com
首页 > 其他好文 > 详细

树莓派点亮小灯泡

时间:2019-12-09 21:30:20      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:bytes   write   type   count   warning   recv   NPU   signed   ttyu   


import serial
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)

GPIO.output(23, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
GPIO.output(25, GPIO.LOW)


ser = serial.Serial("/dev/ttyUSB0",9600)
ser.flushInput()
# ser.write("play,001,$")

def main():
    while True:
        count = ser.inWaiting()
        if count !=0:
            print(count)
            recv = int.from_bytes(ser.read(count), byteorder='big', signed=False)
            print(recv)
            print(type(recv))
            if(recv == 2):
                GPIO.output(23, GPIO.HIGH) 
                GPIO.output(24, GPIO.LOW)
                GPIO.output(25, GPIO.LOW)
                print("hong deng")
            if(recv == 3):
                GPIO.output(23, GPIO.LOW)
                GPIO.output(24, GPIO.LOW)
                GPIO.output(25, GPIO.HIGH)
                print("lv deng")
            if(recv == 4):
                GPIO.output(23, GPIO.LOW)
                GPIO.output(24, GPIO.HIGH)
                GPIO.output(25, GPIO.LOW) 
                print("lan deng")
            ser.flushInput()
        time.sleep(0.1)
        

if __name__ == '__main__':
    main()

树莓派点亮小灯泡

标签:bytes   write   type   count   warning   recv   NPU   signed   ttyu   

原文地址:https://www.cnblogs.com/wjw1014/p/12013395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!