This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
forked from karioja/vedirect
-
Notifications
You must be signed in to change notification settings - Fork 4
/
vedirect.py
96 lines (78 loc) · 2.6 KB
/
vedirect.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, serial
class vedirect:
def __init__(self, serialport):
self.serialport = serialport
self.ser = serial.Serial(serialport, 19200, timeout=10)
self.header1 = '\r'
self.header2 = '\n'
self.delimiter = '\t'
self.key = ''
self.value = ''
self.bytes_sum = 0;
self.state = self.WAIT_HEADER
self.dict = {}
(WAIT_HEADER, IN_KEY, IN_VALUE, IN_CHECKSUM) = range(4)
def input(self, byte):
if self.state == self.WAIT_HEADER:
self.bytes_sum += ord(byte)
if byte == self.header1:
self.state = self.WAIT_HEADER
elif byte == self.header2:
self.state = self.IN_KEY
return None
elif self.state == self.IN_KEY:
self.bytes_sum += ord(byte)
if byte == self.delimiter:
if (self.key == 'Checksum'):
self.state = self.IN_CHECKSUM
else:
self.state = self.IN_VALUE
else:
self.key += byte
return None
elif self.state == self.IN_VALUE:
self.bytes_sum += ord(byte)
if byte == self.header1:
self.state = self.WAIT_HEADER
self.dict[self.key] = self.value;
self.key = '';
self.value = '';
else:
self.value += byte
return None
elif self.state == self.IN_CHECKSUM:
self.bytes_sum += ord(byte)
self.key = ''
self.value = ''
self.state = self.WAIT_HEADER
if (self.bytes_sum % 256 == 0):
self.bytes_sum = 0
return self.dict
else:
self.bytes_sum = 0
else:
raise AssertionError()
def read_data(self):
while True:
byte = self.ser.read(1)
packet = self.input(byte)
def read_data_single(self):
while True:
byte = self.ser.read(1)
packet = self.input(byte)
if (packet != None):
return packet
def read_data_callback(self, callbackFunction):
while True:
byte = self.ser.read(1)
packet = self.input(byte)
if (packet != None):
callbackFunction(packet)
def print_data_callback(data):
print data
if __name__ == '__main__':
ve = vedirect('/tmp/vmodem1')
ve.read_data_callback(print_data_callback)
#print(ve.read_data_single())