Reading from Serial Ports
posted by Allan@TechCrammer @ 2:35pm, Friday 5 December 2008.
The "old" style serial ports (DB9 or DB25) do not seem to be included with many of the newer computers; mostly just a bunch of USB ports. However, if you are like me, you still have the need to interface legacy systems such as Timeclocks, PBX systems, Caller ID, UPS and other equipment.
I was happy to see that when I opened up one of our servers here at work to do some maintenance, that I saw there was a place on the motherboard where I could attach a ribbon cable (special trip to MicroCenter) for a native DB9 port.
From my personal experience the USB to Serial converters may not always play nice with some. We have 4 different legacy hardware devices and use two different USB-Serial adapters since one may work for one device, but not the other. I bought one of the more expensive ones just to see if this makes a difference and found that one of the cheapest ones I bought was the only to work on this particular device. (I guess in this case it isn't "You get what you pay for.") Very odd, and when you unplug them and put them back they may change COM ports in your Operating System which creates trouble for the emulation software we use.
I have recently started using some of the Moxa (PCI multi-port serial expander) products which have worked pretty well for us and about the most cost effective solutions I could find without a bunch of USB to Serial adapters.
I have used Open Source Python code in both Windows and Linux to send and receive serial data. I have used two different Python modules to accomplish this, both with equal success. I used to use the PySerial module, though have recently started using the USPP (Universal Serial Port Python) library just because I can take the files and include the code, versus installing a module; however they both worked well for what I was doing.
Below is some Python code that I use at its simplest form. Just taking data from a Whozz Calling caller-id system and logging it to a flat file. I have been meaning to insert this into a SQL database to allow for Adhoc queries, though for now I just tail the file or do a grep or pattern search for what I am looking for. Easy stuff!
#!/usr/bin/python
from uspp import *
import time
import systty0 = SerialPort("/dev/ttyS0",0,9600)
#OPEN FILE HANDLE
FH = open("/var/log/caller-id.log", 'a')
#print "Starting Process..."
FH.write("Starting Process...\n")
FH.flush()while 1:
count0 = int(tty0.inWaiting())
if count0 > 0:
mychar0 = tty0.read(count0)
FH.write(mychar0)
FH.flush()
else:
time.sleep(3)
So, after including the USPP package, you simply open up the serial port at the baud rate you need. I open a file handle and loop through the data continuously. I also have a short sleep(3) just to make sure the load doesn't get crazy. The only odd thing that took me a few hours to final figure out, was I needed the "flush" statement which I hadn't used with PySerial.
You won't believe it, though it is running on an old Pentium II with 64MB or memory and a 4GB hard drive. Can't remember the exact distro, running uname -a shows "Linux debian 2.6.18-6-486". I don't think it has been rebooted or had issues for at least 6 months and counting.
I use very similar code on my laptop running Windows XP to to intercept data that is destined for a dot matrix serial printer. Just a little fancier and allows you to change baud rates and output files on the fly. If anyone wants that code I will post
it also.
Happy Serial Sniffing,
-Allan








Comments
Submit Your Comment
You are not logged in.