-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.py
63 lines (53 loc) · 1.67 KB
/
test.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
import serial
from collections import namedtuple
import time
ScaleProtocol = namedtuple(
'ScaleProtocol',
"name baudrate bytesize stopbits parity timeout writeTimeout weightRegexp statusRegexp "
"statusParse commandTerminator commandDelay weightDelay newWeightDelay "
"weightCommand zeroCommand tareCommand clearCommand emptyAnswerValid autoResetWeight")
HelmacProtocol = ScaleProtocol(
name='Helmac',
baudrate=9600,
bytesize=serial.SEVENBITS,
stopbits=serial.STOPBITS_ONE,
parity=serial.PARITY_EVEN,
timeout=1,
writeTimeout=1,
weightRegexp="\x02\\s*([0-9.]+)N?\\r",
statusRegexp=None,
statusParse=None,
commandDelay=0.2,
weightDelay=0.5,
newWeightDelay=0.2,
commandTerminator='\r',
weightCommand='2',
zeroCommand='Z',
tareCommand='T',
clearCommand='C',
emptyAnswerValid=False,
autoResetWeight=False,
)
protocol = HelmacProtocol
def _get_raw_response(connection):
answer = []
while True:
char = connection.read(1) # may return `bytes` or `str`
if not char:
break
else:
answer.append(char)
return ''.join(answer)
connection = serial.Serial('/dev/ttyUSB0',
baudrate=protocol.baudrate,
bytesize=protocol.bytesize,
stopbits=protocol.stopbits,
parity=protocol.parity,
timeout=1, # longer timeouts for probing
writeTimeout=1) # longer timeouts for probing
while True:
print connection.readline()
#connection.write(protocol.weightCommand + protocol.commandTerminator)
#time.sleep(protocol.commandDelay)
#answer = _get_raw_response(connection)
#weight, weight_info, status = self._parse_weight_answer(protocol, answer)