如何使用python制作串口嗅探器嗅探物理端口

How to make a serial port sniffer sniffing physical port using a python(如何使用python制作串口嗅探器嗅探物理端口)

本文介绍了如何使用python制作串口嗅探器嗅探物理端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PC 软件(操作系统:Win 64 位),它通过物理串行端口 RS232 与机器通信,我想使用 python 为该端口制作一个嗅探器.请注意,我是串口的初学者.

I have a PC Software (OS: Win 64bit) that communicates with a machine via physical serial port RS232 and I want to make a sniffer for that port using a python. Please note that I am beginner to serial ports.

我已经阅读了多个在线发布的文档和问题,但其中大多数要求只使用 3rd-party 软件,但我不能这样做,因为必须将原始字节解码为字符串消息(我有自己的解码方式/encode 方法).

I've read multiple documents and questions posted online but most of them asks to just use 3rd-party software, but I cannot do this way because raw bytes have to be decoded into string message (I have my way own of decode/encode method).

目前我有这样的设置:

///////////////////       Physical COM1        /////////////
// (PC) Software // <------------------------> // Machine //
///////////////////                            /////////////

我希望 python 输出通过 COM1 的任何字节.

And I want a python to output any bytes that went through COM1.

Desired Behavior 图(虚拟串口有一个问号,因为我不确定这是否正确):

Desired Behavior diagram (Virtual serial port has a question mark because I'm not sure if that is the right approach):

///////////////////       Physical COM1        /////////////
// (PC) Software // <------------------------> // Machine //
///////////////////            | Virtual       /////////////
                               | serial port?
                               v
                        //////////////////
                        // (PC) Sniffer // (Python)
                        //////////////////
                               | 
                               v
                         (output bytes)

那些知道 Advanced Serial Port Monitor 的人,它的spymode"功能正是我想要使用 python 实现的.

Those of who knows Advanced Serial Port Monitor, its "spymode" functionality is exactly what I am trying to achieve using python.

我尝试使用 com0com 和 PortMon,但我找不到配置 com0com 以嗅探物理端口的方法(据我观察,com0com 仅生成虚拟端口)并且 PortMon 不支持 Windows 64 位.

I've tried to use com0com and PortMon but I can't find a way to configure com0com to sniff physical port (as far as my observation goes, com0com only makes virtual ports) and PortMon does not support Windows 64-bit.

我已经被困了好几天了...感谢任何评论/链接/答案.谢谢,

I've been stuck at this for days... any comments/links/answers are appreciated. Thank you,

推荐答案

你应该通过pySerial

一次只能获取一个函数.

对于单向通信(从机器到 PC 软件),我能想到的从串行端口嗅探的唯一方法是从端口 1 读取并写入端口 2,您的机器正在写入端口 1 和 PC 软件已修改为从端口 2 读取.

For one-way communication(from machine to PC software), the only way I can think of to sniff from a serial port is to read from a port1 and write to port2, where your machine is writing to port1 and PC software has been modified to read from port2.

import serial

baud_rate = 4800 #whatever baudrate you are listening to
com_port1 = '/dev/tty1' #replace with your first com port path
com_port2 = '/dev/tty2' #replace with your second com port path

listener = serial.Serial(com_port1, baudrate)
forwarder = serial.Serial(com_port2, baudrate)

while 1:
    serial_out = listener.read(size=1)
    print serial_out #or write it to a file 
    forwarder.write(serial_out)

要实现全双工(异步双向通信),您需要有两个进程,每个方向一个.您需要以某种方式同步这些进程.一种方法是,一个进程从端口 1 读取,另一个进程写入端口 2,反之亦然.阅读这个问题

To achieve full duplex(asynchronous two way communication), you need to have a two processes, one for each direction. You will need to synchronize these process in some way. One way to do it could be, while one process reads from port1, the other writes to port2, and vice-versa. Read this question

这篇关于如何使用python制作串口嗅探器嗅探物理端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用python制作串口嗅探器嗅探物理端口

基础教程推荐