-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb-receiver.py
68 lines (52 loc) · 1.94 KB
/
usb-receiver.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
"""
usb-receiver.py
This is sample code from Stephen Simmons' PyCon UK 2016 talk
"Where am? What am I doing? Motion Sensor Data Fusion with Python".
It receives serial data from a microbit connected to a USB port and
prints it to stdout. From here it can be redirected to a text file, etc.
Usage: [so far, only tested under Linux]
$ python usb-receiver.py
Time,AccX,AccY,AccZ,MagX,MagY,MagZ,Heading,Gesture
630894,-144,-144,-992,5025,2712,38849,255,face up
630948,-160,-112,-912,3725,1612,38049,254,face up
...
or redirecting to a file:
$ python usb-receiver.py > data.csv
The accompanying script microbit-logger.py runs on the microbit.
Stephen Simmons - 5 Sep 2016
https://github.com/stevesimmons/pyconuk-motion-sensor-data-fusion
"""
import logging
import serial
import serial.tools.list_ports
import sys
def main():
port = find_microbit()
if port:
print_from_usb(port)
else:
sys.stderr.write("Unable to find a microbit on any USB port\n")
def find_microbit(serial_number=None):
"Name of the first matching micro:bit's serial port, otherwise None."
for port in serial.tools.list_ports.comports():
sys.stderr.write("Checking %s" % port.description)
if "CMSIS-DAP" in port.product:
if serial_number is None or serial_number == port.serial_number:
sys.stderr.write("Found microbit %s on %s\n" % (port.serial_number, port.device))
return port.device
return None
def print_from_usb(port, out=sys.stdout):
"Write serial data from USB port to a file, by default sys.stdout."
with serial.Serial(port, baudrate=115200, timeout=20) as ser:
try:
while True:
line = ser.readline().decode()
out.write(line)
except KeyboardInterrupt:
pass
except:
logging.exception("Error")
finally:
ser.close()
if __name__ == '__main__':
main()