我有一台PC软件(操作系统:Win 64bit),通过物理串口RS232与机器通信,我想使用python为该端口制作一个嗅探器。请注意,我是串口的初学者。
我已经阅读了多个在线发布的文档和问题,但大多数都要求使用第三方软件,但我不能这样做,因为原始字节必须被解码为字符串消息(我有自己的方式解码/ encode方法)。
目前我的设置如下:
/////////////////// Physical COM1 /////////////
// (PC) Software // <------------------------> // Machine //
/////////////////// /////////////
我想要一个python输出通过COM1的任何字节。
所需行为图(虚拟串口有问号,因为我不确定这是否是正确的方法):
/////////////////// Physical COM1 /////////////
// (PC) Software // <------------------------> // Machine //
/////////////////// | Virtual /////////////
| serial port?
v
//////////////////
// (PC) Sniffer // (Python)
//////////////////
|
v
(output bytes)
谁知道高级串行端口监视器,它的“spymode”功能正是我想用python实现的。
我试过使用com0com和PortMon,但我找不到配置com0com来嗅探物理端口的方法(据我所知,com0com只生成虚拟端口)并且PortMon不支持Windows 64位
我已经坚持了几天......任何意见/链接/答案都表示赞赏。 谢谢,
答案 0 :(得分:4)
您应该通过pySerial
一次只能有一个功能获取串口。
对于单向通信(从机器到PC软件),我能想到从串口嗅探的唯一方法是从port1读取并写入port2,在那里你的机器写入port1和PC软件已被修改为从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)
要实现全双工(异步双向通信),您需要有两个进程,每个方向一个。您需要以某种方式同步这些进程。一种方法可以是,当一个进程从port1读取时,另一个进程写入port2,反之亦然。 阅读此question
答案 1 :(得分:2)
为什么不回应像:
PC S / W&lt; - &gt; COMn(COM0COM)COMm&lt; - &gt; python监视器&amp;前进&lt; - &gt; COM1&lt; - &gt;机
软件方面,您需要2个串行任务,一个打开COMm,一个打开COM1和一个中央记录器,COMm中的任何内容都会被记录,然后转发到COM1和副verca。
答案 2 :(得分:0)
我们可以使用上面的代码而无需花很多时间来实现半双工通信。 我们将使用一个无限循环和一个变量,该变量将指定我们正在读取的端口。
import serial
import time
baud_rate = 9600 # whatever baudrate you are listening to
com_port1 = '/dev/ttyUSB0' # replace with your first com port path
com_port2 = '/dev/ttyUSB1' # replace with your second com port path
ComRead_timeout = 0.1 # Read timeout to avoid waiting while there is no data on the buffer
ComWr_timeout = 0.1 # Write timeout to avoid waiting in case of write error on the serial port
log = open('log.txt', 'a+') # Open our log file, to put read data
From_PC_To_Device = True # this variable is used to specify which port we're gonna read from
listener = serial.Serial(port=com_port1, baudrate=baud_rate, timeout=ComRead_timeout,
write_timeout=ComWr_timeout)
forwarder = serial.Serial(port=com_port2, baudrate=baud_rate, timeout=ComRead_timeout,
write_timeout=ComWr_timeout)
while 1:
while (listener.inWaiting()) and From_PC_To_Device:
serial_out = listener.readline()
localtime = time.asctime(time.localtime(time.time()))
Msg = "PC " + localtime + " " + serial_out
Msg += "\n"
log.write(Msg)
print(serial_out) # or write it to a file
forwarder.write(serial_out)
else:
From_PC_To_Device = False
while (forwarder.inWaiting()) and not From_PC_To_Device:
serial_out = forwarder.readline()
localtime = time.asctime(time.localtime(time.time()))
Msg = "DEVICE " + localtime + " " + serial_out + "\n"
log.write(Msg)
print(serial_out) # or write it to a file
listener.write(serial_out)
else:
From_PC_To_Device = True