0

I want to log the serial output of a device.

To do so, I need to

  1. Send a 1 second DTR pulse that resets the device
  2. Use a non-interactive serial monitor that outputs to stdout

So far I have not found any shell tools that can do any of the two in non-interactive mode. The idea is to start it from a systemd user service so the program can run persistently after user session stops.

Example

$ pulse-dtr /dev/ttyUSB0
$ serial-read /dev/ttyUSB0 |  multilog s10000000 n5 ~/logs/

PS: I'm open to simple python scripts too

AlexLoss
  • 491
  • 4
  • 13

1 Answers1

1

You can manipulate the DTR signals with Python pyserial, which is probably available as a package in your distribution under a similar name. Perhaps some usb serial devices may not implement it, however. You can use the same package to set the baud rate and read from the port:

#!/usr/bin/python3
# https://stackoverflow.com/a/75688106/5008284
import sys, time
import serial

tty = serial.Serial("/dev/ttyS0", baudrate=9600)
tty.dtr = True
time.sleep(1)
tty.dtr = False
sys.stdout = open(sys.stdout.fileno(), mode='wb', buffering=0)
while True:
    ch = tty.read()
    sys.stdout.write(ch)

You may need to set the dtr to False then True, rather than True then False. This program reads a single character and writes it unbuffered to stdout, so you can pipe this into another program.

meuh
  • 11,500
  • 2
  • 29
  • 45
  • 1
    Thank you, worked nicely ! Small fix though: reading 1 character at a time requires to pass `1` as arg in `tty.read(1)`. Others might also declare `stdout` as a separate variable instead of `sys.stdout` so you can still `print()` as expected :) – AlexLoss Mar 13 '23 at 15:25
  • The version of Serial I had said in the pydoc output `.read(self,size=1)`, so the default length should be 1, but you are right to specify it explicitly anyway in this case to make it obvious what is happening. – meuh Mar 13 '23 at 18:09