-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient2.py
executable file
·129 lines (108 loc) · 3.1 KB
/
client2.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
#!/usr/bin/env python
# talk to an rs485 linked "smart sensor"
# example args: /dev/ttyUSB0 9600 [-p]
# -p Sets us to decode the data as PowerInfo instead of RS485
import sys
import serial
import threading
import python.rs485_message_pb2 as rs485_message_pb2
import python.power_pb2 as power_pb2
from struct import *
from google.protobuf import message
# True if the thing connected is a powerboard, false otherwise
isPowerBoard = len(sys.argv) >= 4 and sys.argv[3] == "-p"
ser = serial.Serial(sys.argv[1], sys.argv[2], timeout=0.1)
connected = True
def processRawResponse(s):
# s = unpack('B', s[0])
hex = bytearray.fromhex(s.encode('hex'))
if (len(hex) <= 4):
print "Invalid message size (%d)" % len(hex)
return None
if (hex[0] != 0):
print "Message not for us. Dest: %x" % hex[0]
return None
if (hex[1] != 255):
print "Message not a response. Type: %d" % hex[2]
return None
crc = computeCRC(hex[:len(hex) - 2])
crclow = crc & 0xff
crchigh = (crc >> 8) & 0xff
if crclow != hex[len(hex) - 2] or crchigh != hex[len(hex) - 1]:
print "Checksum error"
return s[2:len(hex) - 2]
def readSerial():
while connected:
s = ser.read(512)
if len(s)>0:
raw = processRawResponse(s)
if raw is not None:
try:
msg = power_pb2.PowerInfo() if isPowerBoard else rs485_message_pb2.Rs485()
msg.ParseFromString(raw)
print msg
except message.DecodeError:
print raw
ser.close()
def crc16_update(crc, a):
if type(a) == str:
a = ord(a)
crc ^= a;
for i in range(0, 8):
if crc & 1:
crc = (crc >> 1) ^ 0xA001;
else:
crc = (crc >> 1);
return crc
def computeCRC(msg):
# if type(msg) == str:
# msg = bytearray.fromhex(str.encode('hex'))
crc = 0xFFFF
for a in msg:
crc = crc16_update(crc, a)
return crc
def sendEcho(id, message):
msg = (id + '00').decode('hex') + message + '00'.decode('hex')
crc = computeCRC(msg)
ser.write(msg)
crclow = crc & 0xff
crchigh = (crc >> 8) & 0xff
ser.write(chr(crclow))
ser.write(chr(crchigh))
def sendGetData(id):
msg = (id + '02').decode('hex')
crc = computeCRC(msg)
ser.write(msg)
crclow = crc & 0xff
crchigh = (crc >> 8) & 0xff
ser.write(chr(crclow))
ser.write(chr(crchigh))
def sendListSensors(id):
msg = (id + '01').decode('hex')
crc = computeCRC(msg)
ser.write(msg)
crclow = crc & 0xff
crchigh = (crc >> 8) & 0xff
ser.write(chr(crclow))
ser.write(chr(crchigh))
thread = threading.Thread(target = readSerial)
thread.start()
while True:
try:
x = raw_input('go: ')
if x == 'q':
connected = False
break
if x.startswith('e'):
p = x.partition(' ')[2].partition(' ')
sendEcho(p[0], p[2])
if x.startswith('g'):
sendGetData(x.partition(' ')[2])
if x.startswith('l'):
sendListSensors(x.partition(' ')[2])
except KeyboardInterrupt:
connected = False
break
except:
connected = False
raise