UART to CSV
Python script for reading a line via UART and appending it to a CSV file along with a timestamp
What it does
This script reads a device's unique ID via UART aka serial port, displays it, logs it in a CSV file (along with a timestamp and the operator's initials), and prints it via a label printer. This is a typical step of a device's production process. The device's unique ID is stuck on the device to help an installation technician know which device is which.
If the device is a 1-Wire sensor, the device ID would be the sensor's unique 1-Wire bus address.
Tested to work on Windows and Linux.
- Get a device's unique ID via UART.
- Display device ID to the operator.
- Save device ID, timestamp, and operator's initials to a CSV file.
- Print device ID via a label printer.
.-> display device <-> bus bridge <-> UART/USB adapter <-> Python script --> CSV file `-> label printer
- UART. Acts as a generic interface to any kind of bus in case the device does not provide its ID via UART.
- CSV file. Serves as a production log.
- Label. Put on the device so the installation technician can identify each device based on device ID.
Interaction with the bus bridge
The bus bridge is used to access a bus other than what the UART/USB adapter supports. In case of the 1-Wire the bus bridge would allow us to get a 1-Wire sensor's unique bus address. In many cases an Arduino can be used to get a working prototype.
- Default serial port configuration: 115200 8N1 (
- Default read timeout: 2 seconds (
- Default characters for requesting the device ID from the bus bridge:
serial_cmd). Using merely
\rallows one to easily get the device ID without having to type a command each time.
- This script expects the first 16 characters of the bus bridge's response to be the device ID. Everything that follows the first 16 characters is ignored.
- Straightforward user interaction.
- Write CSV file.
- Working serial communication (Linux and Windows).
- Print the device ID via a label printer.
- Take care of the
Python v2.7 or later is required.
$ sudo apt-get install python2.7 python-serial $ ./read.py
Download the latest Python 2 version.
> pip install -r requirements.txt > read.py
No serial port:
$ ./read.py [!] No suitable serial port found.
One serial port (on Linux):
$ ./read.py [+] Using only available serial port: /dev/ttyUSB0 [+] Successfully connected. [+] Operator's initials: >>> mkm Press ENTER to read a line from the serial port. Press 'q' and ENTER to exit. 2018-11-19 13:15:34 DEADBEEFCAFED00D 2018-11-19 13:15:42 FEEDFACEC00010FF 2018-11-19 13:15:50 BADDCAFED15EA5E0 2018-11-19 13:15:58 FEE1DEADFEEEFEEE q [+] Closed /dev/ttyUSB1. [+] Closed CSV file.
Two serial ports (on Windows):
C:\Users\mkm>read.py [+] Select one of the available serial ports: (1) COM15 "Silicon Labs CP210x USB to UART Bridge (COM15)" (2) COM4 "Silicon Labs CP210x USB to UART Bridge (COM4)" >>> 3 [!] Invalid serial port. [+] Select one of the available serial ports: (1) COM15 "Silicon Labs CP210x USB to UART Bridge (COM15)" (2) COM4 "Silicon Labs CP210x USB to UART Bridge (COM4)" >>> 2 [+] Successfully connected. [+] Operator's initials: >>> mkm Press ENTER to read a line from the serial port. Press q and ENTER to exit. 2018-11-19 13:15:34 DEADBEEFCAFED00D 2018-11-19 13:15:42 FEEDFACEC00010FF 2018-11-19 13:15:50 BADDCAFED15EA5E0 2018-11-19 13:15:58 FEE1DEADFEEEFEEE q [+] Closed COM4. [+] Closed CSV file.
2018-11-19 13:15:34,DEADBEEFCAFED00D,mkm 2018-11-19 13:15:42,FEEDFACEC00010FF,mkm 2018-11-19 13:15:50,BADDCAFED15EA5E0,mkm 2018-11-19 13:15:58,FEE1DEADFEEEFEEE,mkm
Nothing read from serial port after
x number of seconds (
2018-11-23 19:35:18 --READ-TIMEOUT--
The read string is shorter than expected (
2018-11-23 20:01:20 --ADDR-TOO-SHORT: 'DEADBEEFCAFE'
GNU General Public License v3.0
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 3, as published by the Free Software Foundation.
See LICENSE for details.
Please add them.