chenyc
2024-11-15 ce5e351b48a0a8bb666a579324586caeba8eba8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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('数据不够长')