forked from fossasia/pslab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_handler.py
More file actions
executable file
·255 lines (226 loc) · 8.01 KB
/
packet_handler.py
File metadata and controls
executable file
·255 lines (226 loc) · 8.01 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
from __future__ import print_function
import time
import inspect
import serial
# Handle namespace conflict between packages 'pyserial' and 'serial'.
try:
serial.Serial
except AttributeError:
e = "import serial failed; PSL requires 'pyserial' but conflicting package 'serial' was found."
raise ImportError(e)
import PSL.commands_proto as CP
class Handler():
def __init__(self, timeout=1.0, **kwargs):
self.burstBuffer = b''
self.loadBurst = False
self.inputQueueSize = 0
self.BAUD = 1000000
self.timeout = timeout
self.version_string = b''
self.connected = False
self.fd = None
self.expected_version1 = b'CS'
self.expected_version2 = b'PS'
self.occupiedPorts = set()
self.blockingSocket = None
if 'port' in kwargs:
self.portname = kwargs.get('port', None)
try:
self.fd, self.version_string, self.connected = self.connectToPort(self.portname)
if self.connected: return
except Exception as ex:
print('Failed to connect to ', self.portname, ex.message)
else: # Scan and pick a port
L = self.listPorts()
for a in L:
try:
self.portname = a
self.fd, self.version_string, self.connected = self.connectToPort(self.portname)
if self.connected:
print(a + ' .yes.', self.version_string)
return
except:
pass
if not self.connected:
if len(self.occupiedPorts): print('Device not found. Programs already using :', self.occupiedPorts)
def listPorts(self):
import platform, glob
system_name = platform.system()
if system_name == "Windows":
# Scan for available ports.
available = []
for i in range(256):
try:
portname = 'COM' + str(i)
s = serial.Serial(portname)
available.append(portname)
s.close()
except serial.SerialException:
pass
return available
elif system_name == "Darwin":
# Mac
return glob.glob('/dev/tty*') + glob.glob('/dev/cu*')
else:
# Assume Linux or something else
return glob.glob('/dev/ttyACM*') + glob.glob('/dev/ttyUSB*')
def connectToPort(self, portname):
import platform
if platform.system() not in ["Windows", "Darwin"]:
import socket
try:
self.blockingSocket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.blockingSocket.bind('\0PSLab%s' % portname)
self.blockingSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error as e:
self.occupiedPorts.add(portname)
raise RuntimeError("Another program is using %s (%d)" % (portname))
fd = serial.Serial(portname, 9600, stopbits=1, timeout=0.02)
fd.read(100)
fd.close()
fd = serial.Serial(portname, self.BAUD, stopbits=1, timeout=1.0)
if (fd.inWaiting()):
fd.setTimeout(0.1)
fd.read(1000)
fd.flush()
fd.setTimeout(1.0)
version = self.get_version(fd)
if version[:len(self.expected_version1)] == self.expected_version1 or version[:len(
self.expected_version2)] == self.expected_version2:
return fd, str(version)[1:], True
return None, '', False
def disconnect(self):
if self.connected:
self.fd.close()
if self.blockingSocket:
print('Releasing port')
self.blockingSocket.shutdown(1)
self.blockingSocket.close()
self.blockingSocket = None
def get_version(self, fd):
fd.write(CP.COMMON)
fd.write(CP.GET_VERSION)
x = fd.readline()
# print('remaining',[ord(a) for a in fd.read(10)])
if len(x):
x = x[:-1]
return x
def reconnect(self, **kwargs):
if 'port' in kwargs:
self.portname = kwargs.get('port', None)
try:
self.fd, self.version_string, self.connected = self.connectToPort(self.portname)
except serial.SerialException as ex:
msg = "failed to reconnect. Check device connections."
print(msg)
raise RuntimeError(msg)
def __del__(self):
# print('closing port')
try:
self.fd.close()
except:
pass
def __get_ack__(self):
"""
fetches the response byte
1 SUCCESS
2 ARGUMENT_ERROR
3 FAILED
used as a handshake
"""
if not self.loadBurst:
x = self.fd.read(1)
else:
self.inputQueueSize += 1
return 1
try:
return CP.Byte.unpack(x)[0]
except:
return 3
def __sendInt__(self, val):
"""
transmits an integer packaged as two characters
:params int val: int to send
"""
if not self.loadBurst:
self.fd.write(CP.ShortInt.pack(int(val)))
else:
self.burstBuffer += CP.ShortInt.pack(int(val))
def __sendByte__(self, val):
"""
transmits a BYTE
val - byte to send
"""
# print (val)
if (type(val) == int):
if not self.loadBurst:
self.fd.write(CP.Byte.pack(val))
else:
self.burstBuffer += CP.Byte.pack(val)
else:
if not self.loadBurst:
self.fd.write(val)
else:
self.burstBuffer += val
def __getByte__(self):
"""
reads a byte from the serial port and returns it
"""
ss = self.fd.read(1)
try:
if len(ss):
return CP.Byte.unpack(ss)[0]
except Exception as ex:
print('byte communication error.', time.ctime())
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
# sys.exit(1)
def __getInt__(self):
"""
reads two bytes from the serial port and
returns an integer after combining them
"""
ss = self.fd.read(2)
try:
if len(ss) == 2:
return CP.ShortInt.unpack(ss)[0]
except Exception as ex:
print('int communication error.', time.ctime())
self.raiseException(ex, "Communication Error , Function : " + inspect.currentframe().f_code.co_name)
# sys.exit(1)
def __getLong__(self):
"""
reads four bytes.
returns long
"""
ss = self.fd.read(4)
if len(ss) == 4:
return CP.Integer.unpack(ss)[0]
else:
# print('.')
return -1
def waitForData(self, timeout=0.2):
start_time = time.time()
while time.time() - start_time < timeout:
time.sleep(0.02)
if self.fd.inWaiting(): return True
return False
def sendBurst(self):
"""
Transmits the commands stored in the burstBuffer.
empties input buffer
empties the burstBuffer.
The following example initiates the capture routine and sets OD1 HIGH immediately.
It is used by the Transient response experiment where the input needs to be toggled soon
after the oscilloscope has been started.
>>> I.loadBurst=True
>>> I.capture_traces(4,800,2)
>>> I.set_state(I.OD1,I.HIGH)
>>> I.sendBurst()
"""
# print([Byte.unpack(a)[0] for a in self.burstBuffer],self.inputQueueSize)
self.fd.write(self.burstBuffer)
self.burstBuffer = ''
self.loadBurst = False
acks = self.fd.read(self.inputQueueSize)
self.inputQueueSize = 0
return [CP.Byte.unpack(a)[0] for a in acks]