Pulse DTR和记录串行输出在非交互模式下

huangapple go评论125阅读模式
英文:

Pulse DTR and log serial output in non-interactive mode

问题

要记录设备的串行输出,需要执行以下步骤:

  1. 发送一个1秒的DTR脉冲来复位设备。
  2. 使用一个非交互式串行监视器,将输出发送到标准输出。

到目前为止,我还没有找到任何可以在非交互模式下执行这两个操作的Shell工具。想法是从systemd用户服务中启动它,以便程序在用户会话结束后可以持续运行。

示例

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

PS:我也可以考虑使用简单的Python脚本。

英文:

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

答案1

得分: 1

你可以使用Python pyserial 来操纵 DTR 信号,这个库可能以类似名称的包在你的发行版中可用。然而,某些 USB 串口设备可能不支持它。你可以使用相同的包来设置波特率并从端口读取数据:

#!/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)

可能需要先将 dtr 设置为 False,然后再设置为 True,而不是相反。此程序读取一个字符并将其无缓冲地写入标准输出,因此你可以将其传递给另一个程序。

英文:

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.

huangapple
  • 本文由 发表于 2023年3月9日 21:27:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定