Hi everyone,
i am in need of ideas and want a bit of advise...i want to make a serial port sniffer which will be able to read the data goin in and out of the serial port between two other programs.....

im not sure how i will be able to access a port when its already open....:confused:

all advises are heartily welcome
Thank you....

ok let me put it this way.....

import os
import sys
import time

sys.path.append ( os.path.join( os.getcwd(),
                                'common',
                                'uspp')
                )

from uspp import *

DEFAULT_COM_PORT   = "COM1"
DEFAULT_BAUD_RATE  = 57600
DEFAULT_TIME_OUT   = 10000
class MySerialPort():    
     def __init__(self,
                 port      = DEFAULT_COM_PORT,
                 timeout   = DEFAULT_TIME_OUT,
                 baud      = DEFAULT_BAUD_RATE):
        #initialize and configure serial port class
        self.tty = SerialPort(port, timeout, baud)          
        self.port = port
        self.baud = baud
        self.timeout = timeout
    
    def getCurrentSerialPort(self):
        return self.port
    
    def getCurrentBaudRate(self):
        return self.baud

    def getCurrentTimeout(self):
        return self.timeout   
    
    def write(self,str):
        self.tty.write(str)
        
    def read(self):
        time.sleep(3)
        return self.tty.read(self.tty.inWaiting())

    def flush(self):
        self.tty.flush()
        
    def __del__(self):
        self.tty.__del__()
 
if __name__ == '__main__' :
    #initialise and open port
    SerialPort1 = MySerialPort(DEFAULT_COM_PORT,0,DEFAULT_BAUD_RATE)
    while 1:
        SerialPort1.write(str(raw_input('enter a letter')))
        st=SerialPort1.read()
        print st
        if st=='q':
            break

i have this code as a serial port analyzer....where my this code becomes one end and the target the other and we can send command through my script and see the response....but what i want is a program where two other programs would be communicating through a serial port and my program can monitor it...so basically i want to read a serial port which is already open by some other port...is this possible in python....

self.__handle=CreateFile (dev,                                  win32con.GENERIC_READ|win32con.GENERIC_WRITE,
                                  0, # exclusive access
                                  None, # no security
                                  win32con.OPEN_EXISTING,
                                  win32con.FILE_ATTRIBUTE_NORMAL,
                                  None)

this was the method that creates or opens the serial port....i noticed an attribute which was given value here 0...(3rd attribute...)
the values i can give there are.

win32con.FILE_SHARE_READ
win32con.FILE_SHARE_WRITE
win32con.FILE_SHARE_DELETE

i tried the combination but it does not seem to work.....

GENERIC_READ|win32con.GENERIC_WRITE will not help. Serial Port Sniffers install a special driver that help to intercept all data exchange. Therefore I would suggest to try Serial Port Monitor . There's a demo period, which may be enough time to do what you want to do.
-
HD

Edited 6 Years Ago by HDowns: n/a

Hello,

has there been any update on this one?
I am also doing the same application that monitors an existing and used port. Say below:

[ COMPUTER/DESKTOP SIDE ] ============ [ EXTERNAL HARDWARE ]
MyDesktopApplication <-> COM1 <== SERIAL CABLE ==> COM2 <> MyDevice


The setup is basically as stated above. What the desired application, programatically, is to monitor or sniff to the activities between the ports.

Thanks,
Eljun

This article has been dead for over six months. Start a new discussion instead.