socat - 监控 USB 端口到标准 CP2102 串行适配器

socat - 监控 USB 端口到标准 CP2102 串行适配器

我一直在尝试获取发送/接收到 CP2102 串行转换器芯片的内容的十六进制转储。我可以找到人员监控的例子硬件串口 /dev/TTYS0 等。

socat -d -d pty,link=/dev/ttyUSB0,raw,echo=0 pty,link=/dev/ttyUSB1,raw,echo=0

有谁知道有资源可以告诉我如何监控这样的 USB 端口吗?到目前为止还没有任何效果。也许我只是不明白串行监控的重新路由方面或其他什么?

答案1

如果可能的话,最简单的方法可能是调整软件以发出十六进制。大多数软件可能已经具有sdio.h(或等效的)并且十六进制串行数据仅需要printf对进出串行文件描述符的数据进行调用(或等效)。没有复杂的额外过程来回传送数据,并且几乎没有额外的延迟。

如果你很赶时间,可以使用类似strace(或sysdig SystemTap等)的东西来记录通信,但这strace会大大减慢进程的速度,并且输出将需要后期处理;另外两个是内核模块,因此可能不合适。

strace -xx -y -e trace=read,write -p $pid_of_your_program_here

在硬件层面,总线海盗或类似的可能是另一种利用通信的方式。

同时,socat(1)似乎确实有一个方便的-x十六进制选项:

   -x     Writes  the  transferred  data not only to their target streams,
          but also to stderr. The output format is  hexadecimal,  prefixed
          with  ">  "  or "< " indicating flow directions. Can be combined
          with -v .

经过一些实验后,我能够通过以下方式监听随机的 Arduino:

socat -x PTY,link=/dev/blah,raw,wait-slave /dev/serial/by-id/usb-Arduino...

然后你的软件就可以打开/dev/blah(或者可以通过运行它EXEC?)。请注意,在路径raw之后没有指定任何选项/dev/serial/by-id/usb-Arduino...,因为在尝试配置它时会出现tcgetattr(6, ...): Inappropriate ioctl for device错误。socat输出socat可能还需要后处理,因为它看起来像:

--
2017/10/09 16:32:20 socat[30806] I transferred 1 bytes from 6 to 5
< 2017/10/09 16:32:20.475916  length=31 from=2042 to=2072
 52 65 71 75 65 73 74 69 6e 67 20 74 65 6d 70 65  Requesting tempe
 72 61 74 75 72 65 73 2e 2e 2e 44 4f 4e 45 0a     ratures...DONE.
--

相关内容