import kv from driver import UART import tool import app_aliyunIot import utime # 延时函数在utime库中 uart = UART() takeOver=False def getTakeOver(): global takeOver return takeOver #打开串口通讯 def uart_init(): uart.open("serial2") print('open -----------serial2') utime.sleep_ms(1000) #发送K到串口 读取返回数据 def uart_read(timeJg): global takeOver print('时间间隔---------') print(timeJg) while True: print('sund-----K-----------'+kv.get('key')) uart.write(bytearray(kv.get('key'))) #发送3秒后就读数据 utime.sleep_ms(3000) readBuf = bytearray(570) recvSize = uart.read(readBuf) # print(recvSize) # print(readBuf) # 是否收到血透机回传数据 如果没有就修改发送K if recvSize==0 or recvSize==4: if kv.get('key')=='K\r\n': kv.set('key','K0000\r\n') else: kv.set('key','K\r\n') print('-----get key---11---',kv.get('key')) utime.sleep_ms(10000) if recvSize>=268: takeOver=True #收到数据后 print('收到数据长度270---') print(readBuf.decode('utf-16-le')) strText= readBuf.decode('utf-16-le') zuobiaoBen= strText.find('K') zuobaioEnd=strText.find('$') #字符串长度 changdu=zuobaioEnd-zuobiaoBen if changdu==264: #如果有报警信息马上发送数据模型到阿里云 60秒后循环 #验证后端data print('---------------------') yzStr=strText[zuobiaoBen:zuobiaoBen+270] print(yzStr) #只要K开头的270个字符长度 yzData=readBuf[zuobiaoBen:zuobiaoBen+270] print(yzData) #解析串口回传的数据 jsonData= tool.toParams(yzData.decode('utf-16-le')) #判断是否报警 if tool.isObjectValueEqual(jsonData)==False: # app_aliyunIot.sudDataIot(yzData) app_aliyunIot.sudDataPostProps(jsonData) print('发送数据') utime.sleep_ms(60000) else: # 如果没有报警发送后就等待60秒在执行循环 print('deng dai---'+str(timeJg)) # app_aliyunIot.sudDataIot(yzData) app_aliyunIot.sudDataPostProps(jsonData) print('发送数据') utime.sleep_ms(timeJg*1000) else: print('数据格式不正确不传iot') else: print(readBuf.decode('utf-16-le')) print('数据不够长') #临时发K 更具服务事件发K def sund_key(): print('sund--service---K-----------'+kv.get('key')) uart.write(bytearray(kv.get('key'))) #发送3秒后就读数据 utime.sleep_ms(3000) readBuf = bytearray(570) recvSize = uart.read(readBuf) if recvSize>=270: #收到数据后 print('收到数据长度270---') print(readBuf.decode('utf-16-le')) strText= readBuf.decode('utf-16-le') zuobiaoBen= strText.find('K') zuobaioEnd=strText.find('$') #字符串长度 changdu=zuobaioEnd-zuobiaoBen if changdu==264: #只要K开头的270个字符长度 yzData=readBuf[zuobiaoBen:zuobiaoBen+270] print(yzData) #解析串口回传的数据 jsonData= tool.toParams(yzData.decode('utf-16-le')) app_aliyunIot.sudDataPostProps(jsonData) else: print('数据格式不正确不传iot') else: print(readBuf.decode('utf-16-le')) print('数据不够长')