zoukankan      html  css  js  c++  java
  • 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[:])
    '''
    

      

  • 相关阅读:
    Java并发容器和线程池
    CountDownLatch、CyclicBarrier
    spring data jpa
    转换时间的工具类(1)
    swagger 报错: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot h
    自动生成entry-mapper-service-controller
    Hutool工具包导出Excel文件异常 You need to add dependency of poi-ooxml to your project
    Java获取24小时制的时间方法
    查询全国城市行政区
    Java对象与Json字符串之间的转化
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/11621280.html
Copyright © 2011-2022 走看看