如何在 Linux 中监视串行端口上的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/940374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I monitor data on a serial port in Linux?
提问by divegeek
I'm debugging communications with a serial device, and I need to see all the data flowing both directions.
我正在调试与串行设备的通信,我需要查看双向流动的所有数据。
It seems like this should be easy on Linux, where the serial port is represented by a file. Is there some way that I can do a sort of "bi-directional tee", where I tell my program to connect to a pipe that copies the data to a file and also shuffles it to/from the actual serial port device?
这似乎在 Linux 上应该很容易,其中串行端口由文件表示。有什么方法可以做一种“双向 tee”,在那里我告诉我的程序连接到一个管道,将数据复制到一个文件,并将它的混洗到/从实际的串行端口设备?
I think I might even know how to write such a beast, but it seems non-trivial, especially to get all of the ioctls passed through for port configuration, etc.
我想我什至可能知道如何编写这样的一个野兽,但这似乎很重要,尤其是让所有 ioctl 都通过以进行端口配置等。
Has anyone already built such a thing? It seems too useful (for people debugging serial device drivers) not to exist already.
有没有人已经建立了这样的东西?它似乎太有用了(对于调试串行设备驱动程序的人)已经不存在了。
采纳答案by shodanex
straceis very useful for this. You have a visualisation of all ioctl calls, with the corresponding structure decoded. The following options seems particularly useful in your case:
strace对此非常有用。您可以查看所有 ioctl 调用,并解码相应的结构。以下选项在您的情况下似乎特别有用:
-e read=set
Perform a full hexadecimal and ASCII dump of all the data read from file descriptors listed in the specified set. For example, to see all input activity on file descriptors 3 and 5 use -e read=3,5. Note that this is independent from the normal tracing of the read(2) system call which is controlled by the option -e trace=read.
-e write=set
Perform a full hexadecimal and ASCII dump of all the data written to file descriptors listed in the specified set. For example, to see all output activity on file descriptors 3 and 5 use -e write=3,5. Note that this is independent from the normal tracing of the write(2) system call which is controlled by the option -e trace=write.
-e 读取=设置
对从指定集合中列出的文件描述符读取的所有数据执行完整的十六进制和 ASCII 转储。例如,要查看文件描述符 3 和 5 上的所有输入活动,请使用 -e read=3,5。请注意,这与 read(2) 系统调用的正常跟踪无关,后者由选项 -e trace=read 控制。
-e 写=设置
对写入指定集合中列出的文件描述符的所有数据执行完整的十六进制和 ASCII 转储。例如,要查看文件描述符 3 和 5 上的所有输出活动,请使用 -e write=3,5。请注意,这与由选项 -e trace=write 控制的 write(2) 系统调用的正常跟踪无关。
回答by balpha
回答by codeDr
A simple method would be to write an application which opened the master side of a pty and the tty under test. You would then pass your tty application the slave side of the pty as the 'tty device'.
一个简单的方法是编写一个应用程序,它打开一个 pty 的主端和被测 tty。然后,您将您的 tty 应用程序作为“tty 设备”传递到 pty 的从属端。
You would have to monitor the pty attributes with tcgetattr()
on the pty
master and call tcsetattr()
on the real tty, if the attributes changed.
如果属性发生变化,您将不得不tcgetattr()
在 pty 主控器上监视 pty 属性并调用tcsetattr()
真正的 tty。
The rest would be a simple select()
on both fd's copying data bi-directionally and copying it to a log.
其余的将是select()
fd 双向复制数据并将其复制到日志的简单操作。
回答by MBR
I looked at a lot of serial sniffers. All of them are based on the idea of making a virtual serial port and sniff data from that port. However, any baud/parity/flow changes will break connection.
我看了很多串行嗅探器。所有这些都基于创建虚拟串行端口并从该端口嗅探数据的想法。但是,任何波特率/奇偶校验/流量更改都会中断连接。
So, I wrote my own sniffer :). Most of the serial ports now are just USB-to-serial converters. My sniffer collects data from USB through debugfs, parse it and output to the console. Also any baudrate changes, flow control, line events, and serial errors are also recorded. The project is in the early stage of development and for now, only FTDI is supported.
所以,我写了我自己的嗅探器:)。现在大多数串口只是USB转串口转换器。我的嗅探器通过 debugfs 从 USB 收集数据,解析它并输出到控制台。此外,还会记录任何波特率变化、流量控制、线路事件和串行错误。该项目处于开发的早期阶段,目前仅支持 FTDI。