makomi / uart2csv

Python script for reading a line via UART and appending it to a CSV file along with a timestamp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UART to CSV

Project Status: Active – The project has reached a stable, usable state and is being actively developed.

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.

Typical workflow

  1. Get a device's unique ID via UART.
  2. Display device ID to the operator.
  3. Save device ID, timestamp, and operator's initials to a CSV file.
  4. Print device ID via a label printer.
                                                             .-> display
device <-> bus bridge <-> UART/USB adapter <-> Python script --> CSV file
                                                             `-> label printer

Details

  • 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 (serial_baud_rate)
  • Default read timeout: 2 seconds (serial_timeout_read)
  • Default characters for requesting the device ID from the bus bridge: \r (serial_cmd). Using merely \r allows 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.

Milestones

  • Straightforward user interaction.
  • Write CSV file.
  • Working serial communication (Linux and Windows).
  • Print the device ID via a label printer.
  • Take care of the TODO and FIXME code annotations.

Getting started

Python v2.7 or later is required.

Linux

$ sudo apt-get install python2.7 python-serial
$ ./read.py

Windows
Download the latest Python 2 version.

> pip install -r requirements.txt
> read.py

Typical output

Command line

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.

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

Error handling

Nothing read from serial port after x number of seconds (serial_timeout_read):

2018-11-23 19:35:18  --READ-TIMEOUT--

The read string is shorter than expected (length_device_id):

2018-11-23 20:01:20  --ADDR-TOO-SHORT: 'DEADBEEFCAFE'

License

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.

Comparable projects

Please add them.

About

Python script for reading a line via UART and appending it to a CSV file along with a timestamp

License:GNU General Public License v3.0


Languages

Language:Python 100.0%