36 树莓派串口通信

树莓派查询串口

ls /dev/tty*

 通过  拔插发现多了一个

 就是他了。

arudnio代码

 
void setup()
{
   Serial.begin(9600); //打开串口
}
 
void loop()
{

  Serial.println("Hello Raspberry,I am Arduino.");
  delay(1000);
   if ( Serial.available())
     {
      if('s' == Serial.read())
        Serial.println("Hello Raspberry,I am Arduino.");
     }
    
}

  

 单串口

# -*- coding: utf-8 -*
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 115200)

if ser.isOpen == False:
    ser.open()                # 打开串口
ser.write(b"Raspberry pi is ready")
try:
    while True:
        response = ser.readline()
        print(response)     
        time.sleep(0.1)                  # 软件延时
except KeyboardInterrupt:
    ser.close()

  

 双串口共享内存

# -*- coding: utf-8 -*
import serial
import time
from multiprocessing import Process, Value, Array


class Class_sensor:
    def __init__(self):
        pass    
    
    #读取温度和湿度
    def serial_th(self,num,arr):
        ser = serial.Serial('/dev/ttyUSB1', 115200)

        if ser.isOpen == False:
            ser.open()                # 打开串口
        #ser.write(b"Raspberry pi is ready")
        try:
            while True:
                line = str(ser.readline())
                fengefu='-'
                a=line.strip().split(fengefu)    # x.strip()#除去每行的换行符 按照:分割
        
                tv = "".join(a[1:2] ).strip()  # 去除空格
                hv = "".join(a[3:4]).strip()  # 去除空格
                arr[0]=int(tv)
                arr[1]=int(hv)
                #print('t-'+str(arr[0])+"-h-"+str(arr[1]))
              
                #time.sleep(0.1)                  # 软件延时
        except KeyboardInterrupt:
            ser.close()


    #读取温度和湿度
    def serial_lmq29(self,num,arr):
        ser = serial.Serial('/dev/ttyUSB0', 115200)

        if ser.isOpen == False:
            ser.open()                # 打开串口
        #ser.write(b"Raspberry pi is ready")
        try:
            while True:
                line = str(ser.readline())
                fengefu='-'
                a=line.strip().split(fengefu)    # x.strip()#除去每行的换行符 按照:分割
        
                mq2 = "".join(a[1:2] ).strip()  # 去除空格
                light = "".join(a[3:4]).strip()  # 去除空格
                mq9 = "".join(a[5:6]).strip()  # 去除空格
                #print(mq9)
             
                arr[2]=int(mq2)
                arr[3]=int(light)
                arr[4]=int(mq9)
                #print('mq2-'+ str(arr[2]) +'-lihgt-'+str(arr[3])+'-mq9-'+str(arr[4]))
             
                #time.sleep(0.1)                  # 软件延时
        except KeyboardInterrupt:
            ser.close()        
            
    def class_int(self):        

        self.num_share = Value('d', 0.0)
        self.arr_share = Array('i', range(5))

        p_wh = Process(target=self.serial_th, args=(self.num_share,self.arr_share))
        p_wh.deamon=True  #伴随主进程关闭而关闭
        p_wh.start()


        p_l29 = Process(target=self.serial_lmq29, args=(self.num_share,self.arr_share))
        p_l29.deamon=True
        p_l29.start()
'''
t = Class_sensor()
t.class_int()
while 1:
    # 打印共享内存数据
     print(t.arr_share[:])
'''

  

原文地址:https://www.cnblogs.com/kekeoutlook/p/11621280.html