-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx0serial.py
58 lines (42 loc) · 1.39 KB
/
x0serial.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
##
## imports
##
import serial
import serial.tools.list_ports
##
## code
##
def showSerialPorts():
ports = serial.tools.list_ports.comports()
for port, desc, hwid in sorted(ports):
print(f'{port} {desc} {hwid}')
class X0Serial:
"""Serial connectivity to HDFury and builds up full lines of data """
def __init__(self, dev, useLog, timeoutConfig):
self.log = useLog
self.ser = serial.Serial()
self.ser.baudrate = 19200
self.ser.port = dev
self.ser.timeout = timeoutConfig['hdfuryRead']
self.buffer = b''
def connect(self):
self.ser.open()
self.log.debug(f'Serial port is_open {self.ser.is_open}')
def read(self):
## read and wait for a timeout
# self.log.debug(f'Calling readline and buffer is "{self.buffer}"')
data = self.ser.readline()
# self.log.debug(f'Readline returned "{data}"')
bytes = len(data)
if bytes > 0:
if data[bytes-1] == 0x0A:
## appears we have the end of a command
result = self.buffer + data
self.buffer = b''
return result
else:
## we got data but it doesn't have 0x0A at the end
## this means there's more data needed (hopefully)
self.buffer = self.buffer + data
return b''
return b''