如何使用Python使串行端口嗅探器嗅探物理端口 [英] How to make a serial port sniffer sniffing physical port using a python

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

问题描述

我有一个PC软件(OS: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.

我已经阅读了多个在线发布的文档和问题,但是大多数都要求只使用第三方软件,但是我不能这样做,因为原始字节必须解码为字符串消息(我有自己的解码方式)/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).

目前,我已经按照以下步骤进行了设置:

Currently I have setup like this:

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

我想让python输出通过COM1的所有字节.

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

所需的行为图(虚拟串行端口带有问号,因为我不确定这是否是正确的方法):

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)

谁知道高级串行端口监视器,它的间谍模式"功能正是我要使用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使串行端口嗅探器嗅探物理端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆