|
| 1 | +''' |
| 2 | +This script opens the port associated with Arduino, |
| 3 | +writes to it, reads response, and terminates once it receives |
| 4 | +the "end" message. |
| 5 | +
|
| 6 | +Usage examples: |
| 7 | +python arduino_serial.py --help |
| 8 | +python arduino_serial.py --csv hurricanes.csv |
| 9 | +python arduino_serial.py --csv hurricanes.csv --baudrate 115200 |
| 10 | +python arduino_serial.py --csv ../path_to_some_other_csv/data.csv --baudrate 9600 |
| 11 | +
|
| 12 | +The sript requires "pyserial" library to be installed, |
| 13 | +it can be installed through pip: |
| 14 | +python -m pip install pyserial |
| 15 | +''' |
| 16 | + |
| 17 | +import serial # pip install pyserial |
| 18 | +import serial.tools.list_ports as list_ports |
| 19 | +import time |
| 20 | +import argparse |
| 21 | + |
| 22 | +parser = argparse.ArgumentParser() |
| 23 | +parser.add_argument( |
| 24 | + '--csv', |
| 25 | + metavar = '', |
| 26 | + required = True, |
| 27 | + type = argparse.FileType(mode='r'), |
| 28 | + help = 'CSV file to send through serial' |
| 29 | + ) |
| 30 | + |
| 31 | +parser.add_argument( |
| 32 | + '--baudrate', |
| 33 | + type = int, |
| 34 | + default = 115200, |
| 35 | + required = False, |
| 36 | + metavar = '', |
| 37 | + help = 'What BAUD rate (communication speed) to use. This must match "Serial.begin(<BAUD>);" in Arduino code (e.g. 9600, 115200).' |
| 38 | + ) |
| 39 | + |
| 40 | +args = parser.parse_args() |
| 41 | + |
| 42 | +with open(args.csv.name, 'rb') as f: |
| 43 | + csv_bytes = f.read() |
| 44 | + |
| 45 | +arduino_ports = [] |
| 46 | +print('All ports:') |
| 47 | +for i, p in enumerate(list_ports.comports()): |
| 48 | + print(f'{i+1}. device={p.device} name={p.name} description={p.description} manufacturer={p.manufacturer}') |
| 49 | + # for k,v in vars(p).items(): |
| 50 | + # print(k,v) |
| 51 | + if "arduino" in str(p).lower(): |
| 52 | + arduino_ports.append(p) |
| 53 | + |
| 54 | +print() |
| 55 | + |
| 56 | +if not arduino_ports: |
| 57 | + raise Exception('Arduino port was not found') |
| 58 | + |
| 59 | +if len(arduino_ports) > 1: |
| 60 | + print('Multiple arduino ports were found:') |
| 61 | + for i, p in enumerate(arduino_ports): |
| 62 | + print(f' {i+1}. device={p.device} name={p.name} description={p.description} manufacturer={p.manufacturer}') |
| 63 | + |
| 64 | + i = int(input('\nSelect the port to use\n> ')) |
| 65 | + port = arduino_ports[i-1].device |
| 66 | +else: |
| 67 | + print('A single arduino port was found and will be used') |
| 68 | + print(arduino_ports[0]) |
| 69 | + port = arduino_ports[0].device |
| 70 | + |
| 71 | + |
| 72 | +s = serial.Serial() |
| 73 | +s.baudrate = 115200 |
| 74 | +s.port = port |
| 75 | +s.open() |
| 76 | + |
| 77 | +s.write(csv_bytes) |
| 78 | +s.flush() |
| 79 | + |
| 80 | +try: |
| 81 | + while True: |
| 82 | + time.sleep(0.01) |
| 83 | + if s.in_waiting: # Or: while ser.inWaiting(): |
| 84 | + in_str = "" |
| 85 | + while s.in_waiting: |
| 86 | + in_str += s.read().decode('utf-8') |
| 87 | + print('Received:') |
| 88 | + print(in_str) |
| 89 | + if in_str.endswith('end'): |
| 90 | + exit() |
| 91 | +finally: |
| 92 | + print('Closing port') |
| 93 | + s.close() |
| 94 | + |
0 commit comments