shellloggingserial-portnon-interactive

Pulse DTR and log serial output in non-interactive mode


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


Solution

  • 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.