• 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[:])
    '''
    

      

  • 相关阅读:
    BZOJ 3261 最大异或和(可持久化Trie)
    模板 普通平衡树
    HDU4825 Xor Sum(贪心+Trie树)
    二维LIS(CDQ分治)
    IOIOI卡片占卜(Atcoder-IOIOI カード占い)(最短路)
    USACO 2009 Dec cow toll paths 过路费-floyd
    [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948
    lightoj 1038 Race to 1 Again 期望
    lightoj 1030 Discovering Gold 期望
    lightoj 1027 A Dangerous Maze 期望
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/11621280.html
Copyright © 2020-2023  润新知