[Tutor] COM Ports

D-Man dsh8290@rit.edu
Fri, 20 Apr 2001 17:15:29 -0400


On Fri, Apr 20, 2001 at 03:59:42PM -0700, kromag@nsacom.net wrote:
| > 
| > Oh, and I don't know anything about accessing serial ports on windows...
| > I'm used to just opening the serial device as a filehandle under BSD.
| > Sorry :)
| 
| But never let a chance like this go to waste.....
| 
| I have a project in the works that requires me to read data from a multi-port 
| serial card (which I don't have yet - it should arrive in the mail soon) and 
| squirt the data into a database.
| 
| I am using (at this moment) linux for this particular job, but would have no 
| problem switching to Free/NetBSD (I don't think PostGreSQL will run on 
| OpenBSD yet...). As I understand it, one has to create a C module to read 
| from the serial port in python under unix. I have read the Linux-Serial-
| Programming-HOWTO and the C Extentions Overview in Programming Python and am 
| beginning to get some idea of how creating such a mawnstah might work, but 
| would really like to see some example code that is a little less generic. 
|  
| Can you or any of the other clueful post some examples or url's for same?

To toy with your modem, on COM3 (under linux) :

modem = open( "/dev/ttyS2" , "r+" )
modem.write( "ATZ" )
modem.write( "ATDT*80,4272000" )
print modem.readline()
print modem.readline()
print modem.readline()


<grin>.  Unix is rather orthogonal when it comes to device access.  It
treats everything as a file -- terminals, ttys, serial ports, actual
files.  The serial ports under Linux >= 2.2.x are /dev/ttySn  where n
is a single digit integer (I suppose you could have more serial ports,
but I only have at most 3 on my systems).  Simply open the device file
and read/write to your heart's content.  

The main thing to be careful of is knowing how the device will react
and interacting appropriately.  If instead I tried to 

print modem.read()

the interpreter will "hang" waiting for an EOF signal from the modem.
Of course, that isn't about to happen anytime soon.


I must confess that I too don't know how to open a Windows serial port
for reading/writing.  The following DOS command will copy a disk file
directly to a port:

copy /b Linux-Advocacy.ps LPT1

This is useful if you have, say, a postscript file and a postscript
printer on LPT1.

-D

PS.  Disclaimer :   Even though I did use the interactive interpreter
                    some when I was trying to get my modem to work, I
                    haven't done any serious serial (or parallel, or
                    USB, etc) programming and haven't read that HOWTO
                    you referred to.