Peter's Blog

Redefining the Impossible

RS232 loopback testing on Windows


I am working on a project where I have to test the support of an old RS232 (serial) based communications protocol. I was interested in writing some python scripts to implement the protocol and a simple test harness. I can remember more than ten years ago doing this in C on QNX and it taking a few weeks. I did it in python in less than a day. I used the pyserial module to handle the serial. Here is a simple loopback test I did in pyserial:

   1  
   2  import serial.serialwin32
   3  import time
   4  
   5  oComms1 = serial.serialwin32.Serial( port= 'COM1', baudrate=9600, bytesize=8,
   6                                       parity='N', stopbits=1, timeout=100, xonxoff=0,
   7                                       rtscts=0, writeTimeout=None, dsrdtr=None)
   8  oComms2 = serial.serialwin32.Serial( port= 'COM2', baudrate=9600, bytesize=8,
   9                                       parity='N', stopbits=1, timeout=100, xonxoff=0,
  10                                       rtscts=0, writeTimeout=None, dsrdtr=None)
  11  
  12  #
  13  # Say hello to myself
  14  #
  15  oComms1.write( 'Hello Peter')
  16  
  17  #
  18  # Poll serial for five seconds.
  19  #
  20  nStart = time.time()
  21  while time.time() - nStart < 5:
  22      nRx = oComms2.inWaiting()
  23      if nRx > 0:
  24          #
  25          # Print what is received.
  26          #
  27          print oComms2.read( nRx)

I tested this by using a loopback cable, connecting COM1 to COM2.

After doing this I wondered whether there was a way to do the loopback in software, a COM driver that implemented loopback. A quick google and I found HW VSP which can do what I want and more as well. This program creates a new virtual COM port, COM5 by default, and relays any data sent to this port to an IP socket somewhere, i.e. it can send it to another computer over the network, or it could connect back to the computer it is running on. By running another instance on COM6 and running it as a server, the two virtual COM ports can communicate with each other, equivalent to a loopback. Or, should I so desire, I could have the test harness run on a different computer.

I ran the two instances like this:

First Instance:

Port
COM5
IP Address
IP address of computer it was running on
Port
5000
Server Port
5001
HW VSP Works as the server only (on settings page)
On

Second Instance:

Port
COM6
IP Address
IP address of computer it was running on
Port
5001
Server Port
5000
HW VSP Works as the server only (on settings page)
Off

Could this be the end of the need for a pile of serial cables, gender benders, swapper boxes and soldering irons?


Filed under: python windows

Peter Says:

over 3 years ago

The HW VSP processes do appear to gobble as much cpu time as they can.

Peter

Have Your Say

I welcome constructive comments or questions but I reserve the right to delete any comments that displease me.

Who are you?

(Optional) If you enter an email address here I might email you back. Your email address will not be sold to spammers or shown anywhere

What do you have to say?