forked from fossasia/pslab-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserial_handler.py
More file actions
executable file
·556 lines (456 loc) · 16 KB
/
serial_handler.py
File metadata and controls
executable file
·556 lines (456 loc) · 16 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
"""Low-level communication for PSLab.
Example
-------
>>> from pslab.serial_handler import SerialHandler
>>> device = SerialHandler()
>>> version = device.get_version()
>>> device.disconnect()
"""
try:
import grp
except ImportError:
pass
import logging
import os
import platform
import struct
import time
from functools import partial, update_wrapper
from typing import List, Union
import serial
from serial.tools import list_ports
import pslab
import pslab.protocol as CP
logger = logging.getLogger(__name__)
def detect():
"""Detect connected PSLab devices.
Returns
-------
devices : dict of str: str
Dictionary containing port name as keys and device version on that
port as values.
"""
regex = []
for vid, pid in zip(SerialHandler._USB_VID, SerialHandler._USB_PID):
regex.append(f"{vid:04x}:{pid:04x}")
regex = "(" + "|".join(regex) + ")"
port_info_generator = list_ports.grep(regex)
pslab_devices = {}
for port_info in port_info_generator:
version = _get_version(port_info.device)
if any(expected in version for expected in ["PSLab", "CSpark"]):
pslab_devices[port_info.device] = version
return pslab_devices
def _get_version(port: str) -> str:
interface = serial.Serial(port=port, baudrate=1e6, timeout=1)
interface.write(CP.COMMON)
interface.write(CP.GET_VERSION)
version = interface.readline()
return version.decode("utf-8")
class SerialHandler:
"""Provides methods for communicating with the PSLab hardware.
When instantiated, SerialHandler tries to connect to the PSLab. A port can
optionally be specified; otherwise Handler will try to find the correct
port automatically.
Parameters
----------
See :meth:`connect`.
"""
# V5 V6
_USB_VID = [0x04D8, 0x10C4]
_USB_PID = [0x00DF, 0xEA60]
def __init__(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
self.version = ""
self._log = b""
self._logging = False
self.interface = serial.Serial()
self.send_byte = partial(self._send, size=1)
update_wrapper(self.send_byte, self._send)
self.send_int = partial(self._send, size=2)
update_wrapper(self.send_int, self._send)
self.get_byte = partial(self._receive, size=1)
update_wrapper(self.get_byte, self._receive)
self.get_int = partial(self._receive, size=2)
update_wrapper(self.get_int, self._receive)
self.get_long = partial(self._receive, size=4)
update_wrapper(self.get_long, self._receive)
self.check_serial_access_permission()
self.connect(port=port, baudrate=baudrate, timeout=timeout)
self.connected = self.interface.is_open
@staticmethod
def check_serial_access_permission():
"""Check that we have permission to use the tty on Linux."""
if platform.system() == "Linux":
if os.geteuid() == 0: # Running as root?
return
for group in os.getgroups():
if grp.getgrgid(group).gr_name in (
"dialout",
"uucp",
):
return
udev_paths = [
"/run/udev/rules.d/",
"/etc/udev/rules.d/",
"/lib/udev/rules.d/",
]
for p in udev_paths:
udev_rules = os.path.join(p, "99-pslab.rules")
if os.path.isfile(udev_rules):
return
else:
raise PermissionError(
"The current user does not have permission to access "
"the PSLab device. To solve this, either:"
"\n\n"
"1. Add the user to the 'dialout' (on Debian-based "
"systems) or 'uucp' (on Arch-based systems) group."
"\n"
"2. Install a udev rule to allow any user access to the "
"device by running 'pslab install' as root, or by "
"manually copying "
f"{pslab.__path__[0]}/99-pslab.rules into {udev_paths[1]}."
"\n\n"
"You may also need to reboot the system for the "
"permission changes to take effect."
)
@staticmethod
def _list_ports() -> List[str]:
"""Return a list of serial port names."""
return [p.device for p in list_ports.comports()]
def connect(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
"""Connect to PSLab.
Parameters
----------
port : str, optional
The name of the port to which the PSLab is connected as a string.
On Posix this is a path, e.g. "/dev/ttyACM0". On Windows, it's a
numbered COM port, e.g. "COM5". Will be autodetected if not
specified. If multiple PSLab devices are connected, port must be
specified.
baudrate : int, optional
Symbol rate in bit/s. The default value is 1000000.
timeout : float, optional
Time in seconds to wait before cancelling a read or write command. The
default value is 1.0.
Raises
------
SerialException
If connection could not be established.
RuntimeError
If ultiple devices are connected and no port was specified.
"""
# serial.Serial opens automatically if port is not None.
self.interface = serial.Serial(
port=port,
baudrate=baudrate,
timeout=timeout,
write_timeout=timeout,
)
pslab_devices = detect()
if self.interface.is_open:
# User specified a port.
version = self.get_version()
else:
if len(pslab_devices) == 1:
self.interface.port = list(pslab_devices.keys())[0]
self.interface.open()
version = self.get_version()
elif len(pslab_devices) > 1:
found = ""
for port, version in pslab_devices.items():
found += f"{port}: {version}"
raise RuntimeError(
"Multiple PSLab devices found:\n"
f"{found}"
"Please choose a device by specifying a port."
)
else:
version = ""
if self.interface.port in pslab_devices:
self.version = version
logger.info(f"Connected to {self.version} on {self.interface.port}.")
else:
self.interface.close()
self.version = ""
raise serial.SerialException("Device not found.")
def disconnect(self):
"""Disconnect from PSLab."""
self.interface.close()
def reconnect(
self,
port: str = None,
baudrate: int = None,
timeout: float = None,
):
"""Reconnect to PSLab.
Will reuse previous settings (port, baudrate, timeout) unless new ones are
provided.
Parameters
----------
See :meth:`connect`.
"""
self.disconnect()
# Reuse previous settings unless user provided new ones.
baudrate = self.interface.baudrate if baudrate is None else baudrate
port = self.interface.port if port is None else port
timeout = self.interface.timeout if timeout is None else timeout
self.connect(
port=port,
baudrate=baudrate,
timeout=timeout,
)
def get_version(self) -> str:
"""Query PSLab for its version and return it as a decoded string.
Returns
-------
str
Version string.
"""
self.send_byte(CP.COMMON)
self.send_byte(CP.GET_VERSION)
version = self.interface.readline()
self._write_log(version, "RX")
return version.decode("utf-8")
def get_ack(self) -> int:
"""Get response code from PSLab.
Returns
-------
int
Response code. Meanings:
0x01 ACK
0x10 I2C ACK
0x20 I2C bus collision
0x10 Radio max retransmits
0x20 Radio not present
0x40 Radio reply timout
"""
response = self.read(1)
if not response:
raise serial.SerialException("Timeout while waiting for ACK.")
ack = CP.Byte.unpack(response)[0]
if not (ack & 0x01):
raise serial.SerialException("Received non ACK byte while waiting for ACK.")
return ack
@staticmethod
def _get_integer_type(size: int) -> struct.Struct:
if size == 1:
return CP.Byte
elif size == 2:
return CP.ShortInt
elif size == 4:
return CP.Integer
else:
raise ValueError("size must be 1, 2, or 4.")
def _send(self, value: Union[bytes, int], size: int):
"""Send a value to the PSLab.
Parameters
----------
value : int
Value to send to PSLab. Must fit in four bytes.
"""
if isinstance(value, bytes):
packet = value
else:
packer = self._get_integer_type(size)
packet = packer.pack(value)
self.write(packet)
def _receive(self, size: int) -> int:
"""Read and unpack data from the serial port.
Returns
-------
int
Unpacked data.
Raises
------
SerialException if too few bytes received.
"""
received = self.read(size)
if len(received) == size:
unpacker = self._get_integer_type(size)
retval = unpacker.unpack(received)[0]
else:
raise serial.SerialException(
f"Requested {size} bytes, got {len(received)}."
)
return retval
def read(self, number_of_bytes: int) -> bytes:
"""Log incoming bytes.
Wrapper for Serial.read().
Parameters
----------
number_of_bytes : int
Number of bytes to read from the serial port.
Returns
-------
bytes
Bytes read from the serial port.
"""
data = self.interface.read(number_of_bytes)
self._write_log(data, "RX")
return data
def write(self, data: bytes):
"""Log outgoing bytes.
Wrapper for Serial.write().
Parameters
----------
data : int
Bytes to write to the serial port.
"""
self.interface.write(data)
self._write_log(data, "TX")
def _write_log(self, data: bytes, direction: str):
if self._logging:
self._log += direction.encode() + data + "STOP".encode()
def wait_for_data(self, timeout: float = 0.2) -> bool:
"""Wait for :timeout: seconds or until there is data in the input buffer.
Parameters
----------
timeout : float, optional
Time in seconds to wait. The default is 0.2.
Returns
-------
bool
True iff the input buffer is not empty.
"""
start_time = time.time()
while time.time() - start_time < timeout:
if self.interface.in_waiting:
return True
time.sleep(0.02)
return False
RECORDED_TRAFFIC = iter([])
"""An iterator returning (request, response) pairs.
The request is checked against data written to the dummy serial port, and if it matches
the response can be read back. Both request and response should be bytes-like.
Intended to be monkey-patched by the calling test module.
"""
class MockHandler(SerialHandler):
"""Mock implementation of :class:`SerialHandler` for testing.
Parameters
----------
Same as :class:`SerialHandler`.
"""
VERSION = "PSLab vMOCK"
def __init__(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
self._in_buffer = b""
super().__init__(port, baudrate, timeout)
@staticmethod
def check_serial_access_permission():
"""See :meth:`SerialHandler.check_serial_access_permission`."""
pass
def connect(
self,
port: str = None,
baudrate: int = 1000000,
timeout: float = 1.0,
):
"""See :meth:`SerialHandler.connect`."""
self.version = self.get_version()
def disconnect(self):
"""See :meth:`SerialHandler.disconnect`."""
pass
def reconnect(
self,
port: str = None,
baudrate: int = None,
timeout: float = None,
):
"""See :meth:`SerialHandler.reconnect`."""
pass
def get_version(self) -> str:
"""Return mock version."""
return self.VERSION
def read(self, number_of_bytes: int) -> bytes:
"""Mimic the behavior of the serial bus by returning recorded RX traffic.
The returned data depends on how :meth:`write` was called prior to calling
:meth:`read`.
See also :meth:`SerialHandler.read`.
"""
read_bytes = self._in_buffer[:number_of_bytes]
self._in_buffer = self._in_buffer[number_of_bytes:]
return read_bytes
def write(self, data: bytes):
"""Add recorded RX data to buffer if written data equals recorded TX data.
See also :meth:`SerialHandler.write`.
"""
tx, rx = next(RECORDED_TRAFFIC)
if tx == data:
self._in_buffer += rx
def wait_for_data(self, timeout: float = 0.2) -> bool:
"""Return True if there is data in buffer, or return False after timeout."""
if self._in_buffer:
return True
else:
time.sleep(timeout)
return False
class ADCBufferMixin:
"""Mixin for classes that need to read or write to the ADC buffer."""
def fetch_buffer(self, samples: int, starting_position: int = 0):
"""Fetch a section of the ADC buffer.
Parameters
----------
samples : int
Number of samples to fetch.
starting_position : int, optional
Location in the ADC buffer to start from. By default samples will
be fetched from the beginning of the buffer.
Returns
-------
received : list of int
List of received samples.
"""
self._device.send_byte(CP.COMMON)
self._device.send_byte(CP.RETRIEVE_BUFFER)
self._device.send_int(starting_position)
self._device.send_int(samples)
received = [self._device.get_int() for i in range(samples)]
self._device.get_ack()
return received
def clear_buffer(self, samples: int, starting_position: int = 0):
"""Clear a section of the ADC buffer.
Parameters
----------
samples : int
Number of samples to clear from the buffer.
starting_position : int, optional
Location in the ADC buffer to start from. By default samples will
be cleared from the beginning of the buffer.
"""
self._device.send_byte(CP.COMMON)
self._device.send_byte(CP.CLEAR_BUFFER)
self._device.send_int(starting_position)
self._device.send_int(samples)
self._device.get_ack()
def fill_buffer(self, data: List[int], starting_position: int = 0):
"""Fill a section of the ADC buffer with data.
Parameters
----------
data : list of int
Values to write to the ADC buffer.
starting_position : int, optional
Location in the ADC buffer to start from. By default writing will
start at the beginning of the buffer.
"""
self._device.send_byte(CP.COMMON)
self._device.send_byte(CP.FILL_BUFFER)
self._device.send_int(starting_position)
self._device.send_int(len(data))
for value in data:
self._device.send_int(value)
self._device.get_ack()