Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Protocol sniffer between two serial port. Dump the output to a file and in the consol
'''
Protocol sniffer between two serial port. Dump the output to a file and in the consol
@Author Marc-Olivier Arsenault
@Date Jan 3rd 2017
'''
import serial
import sys
port1 = "/dev/ttyS1"
port2 = "/dev/ttyS3"
baud = 9600
current = 0
fileName = "out.txt"
target = open(fileName, 'r+')
ser1 = serial.Serial(port1, baud, timeout=1)
if ser1.isOpen():
print(ser1.name + ' is open...')
ser2 = serial.Serial(port2, baud, timeout=1)
if ser2.isOpen():
print(ser2.name + ' is open...')
while True:
if ser1.inWaiting() > 0:
if current!=1:
print("")
print("=================SER1====================")
target.write("\n")
target.write("=================SER1====================")
current = 1
a = ser1.read()
sys.stdout.write(hex(ord(a)))
sys.stdout.write(' ')
target.write(hex(ord(a)))
target.write(' ')
ser2.write(a)
if ser2.inWaiting() > 0:
if current!=2:
print("")
print("=================SER2====================")
target.write("\n")
target.write("=================SER2====================")
current = 2
a = ser2.read()
sys.stdout.write(hex(ord(a)))
sys.stdout.write(' ')
target.write(hex(ord(a)))
target.write(' ')
ser1.write(a)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.