樹莓派查詢串口
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[:]) '''