-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckChannels.py
executable file
·142 lines (116 loc) · 3.39 KB
/
CheckChannels.py
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
# by blues
# script for check channels
import serial
import time
TIMEOUT = 1
US = '\x1f'
ENQ = '\x05'
sr = serial.Serial('/dev/ttyUSB0')
sr.timeout = TIMEOUT
sr.baudrate = 9600
def CRC16(s):
poly = 0x1021
Init = 0xffff
# xor = 0x0001
crc = Init
for b in s:
crc ^= (ord(b) << 8)
for _ in range(8):
if (crc & 0x8000):
crc = (crc << 1) ^ poly
else:
crc = (crc << 1)
Hex = '%04x' % (crc & Init)
return crcToBytes(Hex)
def crcToBytes(mess):
return bytes.fromhex(mess[:2]) + bytes.fromhex(mess[2:])
def crcAdd(mess):
return bytes(mess, 'utf-8') + CRC16(mess)
def byteToStr(mess):
return mess.decode('utf-8', 'replace')
def checkData(data):
if len(data):
result = {}
if data[0] == 35:
result['answer'] = data[1:3]
data, crc = data[:-2], data[-2:]
if CRC16(byteToStr(data)) == crc:
result['mess'] = data[3:-1]
result['check'] = True
else:
result['answer'] = ''
data, crc = data[:-2], data[-2:]
if CRC16(byteToStr(data)) == crc:
result['mess'] = data
result['check'] = True
else:
result['mess'] = data
result['check'] = False
else:
result = False
return result
def writeMessage(mess):
result = b''
sr.write(mess)
timeout = time.time()
data = False
while time.time() - timeout <= TIMEOUT:
result += sr.read(1)
if len(result) >= 3:
try:
if result[-3] in [21, 19, 7]:
print(f'Break reading: controller return {byteToStr(result)}')
data = False
break
except Exception:
pass
if result[-3] in [6, 18]:
data = checkData(result)
break
else:
continue
# print(data)
return data
def GetHash():
result = sendCommand(f'$GH{ENQ}')
if result:
return result['mess']
else:
return False
def sendCommand(message):
message = crcAdd(message)
# print(message)
if len(message) > 20:
print([i for i in message[6:-3].split(b'\x1f')])
else:
# print(message)
pass
return writeMessage(message)
def CheckChannels():
sendCommand(f'$SM2{ENQ}')
for i in range(12):
channels = ['10000' for c in range(11)]
for n in range(10):
mess = "\x1f".join(channels[:i] + [f"{10000-n:05}"] + channels[i:])
sendCommand(f'$SC00\x1f{mess}{ENQ}')
# time.sleep(0.05)
sendCommand(f'$SM1{ENQ}')
def main():
# with open('/tmp/logs/controller_version.log', 'w') as fw:
# fw.write(repr(sendCommand(f'$GV{ENQ}')))
sendCommand(f'$SM2{ENQ}')
# mess = '\x1f'.join(['10000' for i in range(12)])
# sendCommand(f"$SCff\x1f{mess}{ENQ}")
# for n in range(100):
# sendCommand(f"$SCff\x1f{mess}{ENQ}")
print(sendCommand(f'$GV{ENQ}'))
channels = ['10000'] * 11
for i in range(12):
mess = "\x1f".join(channels[:i]+ [f"00990"] + channels[i:])
print(sendCommand(f"$SC01\x1f{mess}{ENQ}"))
print(sendCommand(f'$GC{ENQ}')['mess'].split(b'\x1f'))
time.sleep(0.1)
# CheckChannels()
if __name__ == '__main__':
main()