How to make a serial port sniffer sniffing physical port using a python

Harry Cho

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.

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 //
///////////////////                            /////////////

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)

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

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,

shshank

You should go through pySerial

Only one function can acquire the serial port at a time.

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)

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Delay from serial port using Python

From Dev

Delay from serial port using Python

From Dev

sniffing serial port - jpnevulator sniffs only some bytes

From Dev

How to Create virtual serial port using java

From Dev

How to find processes using serial port

From Dev

login using serial port in Linux

From Dev

How to check if serial port is already open (by another process) in Linux, using Python 2.7 (and possibly pyserial)?

From Dev

Sending a character to the Arduino serial port using Python's Pyserial

From Dev

how to write characters to serial port

From Dev

how to write characters to serial port

From Dev

How to communicate with ups with a serial port?

From Dev

How to call a method using the serial port DataReceived event

From Dev

How do I find out if something is using the serial port?

From Dev

how to assign serial port name using rules.d?

From Dev

How to write on serial port in python that ttyUSB0 will be interpreted commands?

From Dev

Python : How to know serial port device events like keybord events

From Dev

Serial port sniffer / monitor / logger software for Windows 7, 64-bit?

From Dev

Using DMA to access High Speed Serial Port

From Dev

Reading and writing to a serial port using shell and Java

From Dev

Can't connect to SERIAL PORT using Putty

From Dev

Using DartLang to comunicate via Serial port

From Dev

Using asyncio to read the output of a serial port

From Dev

Can't connect to SERIAL PORT using Putty

From Dev

Reading a serial port in ruby using winddows

From Dev

Find and kill the process that is using a serial port

From Dev

ReadExisting serial port to bytes using C#

From Dev

Waiting for a serial port response using timercallback

From Dev

Send file over serial port from Python

From Dev

Python: Writing to and Reading from serial port

Related Related

  1. 1

    Delay from serial port using Python

  2. 2

    Delay from serial port using Python

  3. 3

    sniffing serial port - jpnevulator sniffs only some bytes

  4. 4

    How to Create virtual serial port using java

  5. 5

    How to find processes using serial port

  6. 6

    login using serial port in Linux

  7. 7

    How to check if serial port is already open (by another process) in Linux, using Python 2.7 (and possibly pyserial)?

  8. 8

    Sending a character to the Arduino serial port using Python's Pyserial

  9. 9

    how to write characters to serial port

  10. 10

    how to write characters to serial port

  11. 11

    How to communicate with ups with a serial port?

  12. 12

    How to call a method using the serial port DataReceived event

  13. 13

    How do I find out if something is using the serial port?

  14. 14

    how to assign serial port name using rules.d?

  15. 15

    How to write on serial port in python that ttyUSB0 will be interpreted commands?

  16. 16

    Python : How to know serial port device events like keybord events

  17. 17

    Serial port sniffer / monitor / logger software for Windows 7, 64-bit?

  18. 18

    Using DMA to access High Speed Serial Port

  19. 19

    Reading and writing to a serial port using shell and Java

  20. 20

    Can't connect to SERIAL PORT using Putty

  21. 21

    Using DartLang to comunicate via Serial port

  22. 22

    Using asyncio to read the output of a serial port

  23. 23

    Can't connect to SERIAL PORT using Putty

  24. 24

    Reading a serial port in ruby using winddows

  25. 25

    Find and kill the process that is using a serial port

  26. 26

    ReadExisting serial port to bytes using C#

  27. 27

    Waiting for a serial port response using timercallback

  28. 28

    Send file over serial port from Python

  29. 29

    Python: Writing to and Reading from serial port

HotTag

Archive