没头脑很久没有处理串口数据,今日测试一个测距传感模块,用简单的serial.read没十几秒就屏幕冻住了,不往下print了。后来反应过来是不停访问串口,导致串口数据缓存区溢出了。有一个比较保险的方式如下,简单而言是成块读取缓存区所有的数据,处理完数据以后,再重新读缓存区所有的数据。
import serial
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.OUT)
GPIO.output(17,GPIO.HIGH)
ser = serial.Serial('/dev/ttyS0', baudrate=115200)
try:
while 1:
reading = ser.read(ser.in_waiting) # 一次缓存区大概有210组,每组10 bytes的数据
now = int(round(time.time()*1000))
now_format = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(now/1000))
startdata = reading[0:2]
if startdata == b'\xa5\x5a': #正常的侦头被读到了,于是开始处理缓存区数据。特定对于UETCH101DSB这个模块而已
reading_arr = reading.split(startdata)
flag = 0 #这测到距离就mark
sample_arr = []
for item in reading_arr:
if len(item) != 0:
if item[2] != 255: #即不是16进制中的ff
sample_arr.append(item[2])
flag = flag + 1
if flag > 10: #很多组数据都检测到有动的物体
ser.close()
GPIO.output(17,GPIO.LOW) #尝试给gpio断电,彻底重置一下那个传感器。
time.sleep(3)
no_dup_arr = list(set(sample_arr))
if len(no_dup_arr) > 4:
print("log_time: " +now_format+" " + str(no_dup_arr))
GPIO.output(17,GPIO.HIGH)
time.sleep(5) #其实这个位置就可以拍片子
ser.open()
else:
continue
time.sleep(5)
except(KeyboardInterrupt, SystemExit):
ser.close()
GPIO.output(17,GPIO.LOW)