-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial_comm.py
223 lines (143 loc) · 3.96 KB
/
serial_comm.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import serial
import time
import argparse
from vars import *
def pack_channel(gain=3,
filt=3,
dcOffset=0,
testEnable=0,
outputCoupling=0,
outputBuffer=0):
'''
Contructs the word to set a certain channel
'''
ret = \
( ( ( gain & SG_M ) << SG_S ) |
( ( filt & ST_M ) << ST_S ) |
( ( dcOffset & SNC_M ) << SNC_S ) |
( ( testEnable & STS_M ) << STS_S ) |
( ( outputCoupling & SDC_M ) << SDC_S ) |
( ( outputBuffer & SDF_M ) << SDF_S ) )
return ret
def get_word(plane='induction'):
'''
Returns the channel word for the induction or collection plane
'''
word = pack_channel(dcOffset=0 if plane == 'induction' else 1)
word = hex(word)
word = word.upper()[2:]
print('Word is', word)
return word
def set_channels(ser):
'''
Sets all the 480 channels.
Use this if all 10 boards are connected.
'''
print('Setting induction plane')
word = get_word(plane='induction')
cmd = f'edit {word} 0 {480/2-1:.0f}\r'
ser.write(cmd.encode())
printlines(ser)
print('Setting collection plane')
word = get_word(plane='collection')
cmd = f'edit {word} {480/2:.0f} {480-1:.0f}\r'
ser.write(cmd.encode())
printlines(ser)
def set_channels_oneboard(ser):
'''
Sets 48 channels in a single board.
Use this if only one board is connected.
'''
print('Setting channels for a single board')
word = get_word(plane='collection')
cmd = f'edit {word} 0 {48-1:.0f}\r'
ser.write(cmd.encode())
printlines(ser)
def set_test_channel(ser, ch):
'''
Sets channel ch to be a test channel
'''
# Get default word
word = get_word(plane='collection')
# str to int
word = int(word, 16)
# Set test bit to 1
word |= (1 << STS_S)
# To hex str
word = hex(word)
word = word.upper()[2:]
print('Updated word is', word)
cmd = f'edit {word} {ch} {ch}\r'
ser.write(cmd.encode())
printlines(ser)
def set_all_test_channels(ser):
'''
Sets all channels to be test channels
'''
# Get default word
word = get_word(plane='collection')
# str to int
word = int(word, 16)
# Set test bit to 1
word |= (1 << STS_S)
# To hex str
word = hex(word)
word = word.upper()[2:]
print('Updated word is', word)
cmd = f'edit {word} 0 {48-1:.0f}\r'
ser.write(cmd.encode())
printlines(ser)
def printlines(ser):
'''
Prints all lines from the serial chain
'''
lines = ser.readlines()
for line in lines:
print(str(line))
def serial_test():
'''
Runs a simple test
'''
print('Opening serial...')
# ser = serial.Serial('/dev/tty.usbmodem123451', 19200, timeout=1)
ser = serial.Serial('/dev/ttyACM0', 19200, timeout=1)
print('...serial is open.')
print('HELP:')
ser.write(b'help\r')
printlines(ser)
print()
print('Is TEST on?')
ser.write(b'test\r')
printlines(ser)
def test_all_channels():
print('Opening serial...')
# ser = serial.Serial('/dev/tty.usbmodem123451', 19200, timeout=1)
ser = serial.Serial('/dev/ttyACM0', 19200, timeout=1)
print('...serial is open.')
print('HELP:')
ser.write(b'help\r')
printlines(ser)
print()
print('Is TEST on?')
ser.write(b'test\r')
printlines(ser)
print()
print('Current configuration:')
ser.write(b'print\r')
printlines(ser)
set_channels_oneboard(ser)
set_all_test_channels(ser)
# Have to shift twice to make it work
ser.write(b'shift\r')
printlines(ser)
ser.write(b'shift\r')
printlines(ser)
print('Reading back from the serial chain:')
ser.write(b'readback\r')
printlines(ser)
print('Starting test pulser')
ser.write(b'teston 200\r')
printlines(ser)
if __name__ == "__main__":
# serial_test()
test_all_channels()