-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy_serial.py
82 lines (67 loc) · 2.37 KB
/
dummy_serial.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
def list_ports():
return [dummy_device()]
class dummy_device():
device = "DUMMY DEVICE"
vid = 'VENDOR ID'
pid = 'PRODUCT ID'
class Serial():
def __init__(self, portname, exclusive=True):
super().__init__()
self.portname = portname
self.sampling = 400
self.NUM_CHANNEL = 8
self.gains = [0 for x in range(self.NUM_CHANNEL)]
self.identifier = "ADC-8x FAUX DEVICE\nNO SERIAL NUMBER"
def reset_input_buffer(self):
self.buffer = ""
def write(self,msg):
msg = msg.decode()
if msg.startswith('c'):
self.buffer = self.show_current_settings()
elif msg.startswith('*'):
self.buffer = self.print_id()
elif msg.startswith('s'):
msg = msg.split(' ')
self.sampling = float(msg[1])
elif msg.startswith('g'):
msg = msg.split(' ')
channels = int(msg[1])
gain = int(msg[2])
if channels == 0:
self.gains = [gain for x in range(self.NUM_CHANNEL)]
else:
self.gains[channels-1] = gain
print( self.gains)
elif msg.startswith('b'):
self.begin_data_collection()
def show_current_settings(self):
msg_out = "\nCurrent settings: Sampling rate %.2f\n" % self.sampling
msg_out += "\nExtra on-board channel-4 input enabled\n"
for i in range(self.NUM_CHANNEL):
msg_out += "ADC %d " % (i+1)
if self.gains[i] == 0:
msg_out += "disabled\n"
else:
msg_out += "gain %d, bipolar, unbuffered\n" % self.gains[i]
return msg_out
def print_id(self):
msg_id = "ADC-8x driver version FAUX\n"
msg_id += "SAMD21 ID: XXXX-XXXX-XXXX-XXXX\n"
msg_id += "Flash ID: XXXXXXXX-XXXX\n"
return msg_id
def close(self):
print('CLOSED')
def read(self, n):
if n < len(self.buffer):
msg_out = self.buffer[:n]
self.buffer = self.buffer[n:]
else:
msg_out = self.buffer
self.buffer = ""
return msg_out.encode()
def read_until(self, size):
if size < len(self.buffer):
msg_out = self.buffer[:size]
else:
msg_out = self.buffer
return msg_out.encode()