
In article <Hh49+4NpLja@cmoak7.milacron.com>, tom_morrison@milacron.com
(Tom Morrison) writes:
    Please don't flame me for asking a neophyte question, but I have a system 
    running a flavor of UNIX System V on which I need to write a program that 
    communicates via serial communications.  I've written a bit of C code on UNIX 
    systems in the past but never involving communications.  I need some quick 
    pointers on what it takes to setup a serial port (baud rate, stop bit, 
    parity, etc.), send data out the port, watch for data to come in the port, 
    and close the port when I'm finished.  Is this a straightforward and somewhat 
    standard task in UNIX?  Are there particular man pages I should check for the 
    functions required?  I'd greatly appreciate any short pointers someone might 
    give me on the subject.


There is an example of a communication program with a printer over a serial
line in Richard Stevens' "Advanced Programming in The Unix Environment"
(Addison-Wesley, 1992, ISBN 0-201-56317-7). The source code is available
at <ftp://ftp.uu.net//published/books/stevens.advprog.tar.Z> (there is also
an errata file).

I hope this helps,

			Martin



Rob Janssen <pe1chl@wab-tis.rabobank.nl> wrote:
>>systems in the past but never involving communications.  I need some quick 
>>pointers on what it takes to setup a serial port (baud rate, stop bit, 
>>parity, etc.), send data out the port, watch for data to come in the port, 
>>and close the port when I'm finished.  Is this a straightforward and somewhat 
...
>See the manual pages for tcxxxxxx, tty, open, read, write, fcntl.
>It is straightforward, but it will require some study.  It may be better
>to look at some piece of example code, but unfortunately most programs
>that include this are quite messy in this area, either because they want
>to do a lot or because they want to be running on many incompatible systems.
>(or both)

Or, you can cheat.  I wrote a simple program to dial up my SLIP provider
and start a connection.  Here's how I open and setup the serial port:

	int     OpenModem()
	{
	    mfd = open("/dev/tty0",O_RDWR|O_NDELAY);
	    system("stty -parity raw 38400 < /dev/tty0");
	}

The above is for A/UX, but should be really easy to port to other Unix or
Unix-like systems.  (The reason it is declared as "int" but actually does
not return anything is that pre-ANSI C doesn't have void, and I wanted it
to compile on pre-ANSI systems, too).

--Tim Smith


