Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serial Communications in "UNIX" apps.

3 views
Skip to first unread message

Tom Morrison

unread,
Mar 9, 1995, 2:26:21 PM3/9/95
to
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.

Thanks!
Tom_Mo...@Milacron.com

Martin Ouwehand EPFL-SIC/SE

unread,
Mar 10, 1995, 5:17:47 AM3/10/95
to
In article <Hh49+...@cmoak7.milacron.com>, tom_mo...@milacron.com


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

Message has been deleted

Tim Smith

unread,
Mar 10, 1995, 1:57:49 PM3/10/95
to
Rob Janssen <pe1...@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

J Wunsch

unread,
Mar 14, 1995, 4:52:48 AM3/14/95
to
[Linux xrefs deleted, i don't have those groups here, sorry.]
Tim Smith <t...@u.washington.edu> wrote:

>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).

Complaints:

If you finally got a file descriptor, why don't you use termios control
functions but perform the rather ugly system() instead (which invokes
a shell)?

You will also have to set clocal unless your modem fakes the carrier
in command mode.

Most unices do also reset the tty settings to their defaults when
closing the device, so you must ensure that your program keeps the
file descriptor open until dialing is done.

Btw., the above example would be very simple in a 4.4BSD-based system:

stty -f /dev/tty0 -parenb raw 38400
^^^^^^^^^^^^
:-)

Re: the last sentence of your statement: ok, but why don't you at
least write ``return 0;'' then? This will keep more modern compilers
than your ancient thingy from complaining about the arbitrary value
being returned otherwise.

--
cheers, J"org private: joerg_...@uriah.heep.sax.de
http://www.sax.de/~joerg/

Never trust an operating system you don't have sources for. ;-)

Doug Stanfield

unread,
Mar 15, 1995, 5:40:50 PM3/15/95
to
Tom,
IMO this is not a "straightforward and somewhat standard task...".
Check out man termios, but good luck. Best would probably be hitting a
book like Advance UNIX System Programming (I think thats right) by Stevens.

Good luck.

-Doug-

Tom Morrison (tom_mo...@milacron.com) wrote:
<snipped>
: 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.

--

Doug Stanfield "The significant problems we face cannot be solved
Oceanic Cable at the same level of thinking we were at when we
Project Engineer created them." - Albert Einstein
do...@kalama.doe.hawaii.edu

Borje Jonsson

unread,
Mar 17, 1995, 6:54:23 AM3/17/95
to
In article <D5I70...@news.hawaii.edu>, do...@kalama.doe.Hawaii.Edu (Doug Stanfield) says:
>
>Tom,
> IMO this is not a "straightforward and somewhat standard task...".
>Check out man termios, but good luck. Best would probably be hitting a
>book like Advance UNIX System Programming (I think thats right) by Stevens.
>
>Good luck.
>
>-Doug-
>
>Tom Morrison (tom_mo...@milacron.com) wrote:
><snipped>
>: 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.
>--
>
Hi there!

I've used something like this to read chars on some sys V machines.
It should compile on linux directly, but cant find the time for testing.
Please take it as a template for your own application, eg. some parameter
might not work.

Time for some code:


#include <fcntl.h>
#include <termio.h>
#include <sys/ioctl.h>

main ()
{
int fd;

setty ("/dev/tty01", 1, &fd);
/* do something useful */
close (fd);
}

int setty (char *device, int mode, int *fd)
{
struct termio ttypar;
char ttyport [80];

if ((*fd = open (device, O_RDWR)) == -1) return (0);

if (ioctl (*fd, TCGETA, &ttypar) == -1) return (0); /* read current settings */

/* 7 bits, 9600 baud, even parity, 1 stopbit */
if (mode == 1) {
ttypar.c_iflag = 0; /* input modes */
ttypar.c_oflag = 0; /* output modes */
ttypar.c_lflag = 0; /* local modes */
ttypar.c_line = 0; /* line discipline */
ttypar.c_cflag = B9600 | CS7 | CREAD | PARENB; /* control modes */
/* c_cc == control chars */
ttypar.c_cc [VMIN] = 1; /* minimum nr chars */
ttypar.c_cc [VTIME] = 0; /* no timeout on read */
}
else if (mode == 2) {
ttypar.c_iflag = 0;
ttypar.c_oflag = 0;
ttypar.c_lflag = 0;
ttypar.c_line = 0;
ttypar.c_cflag = B9600 | CS7 | CREAD | PARENB;
ttypar.c_cc [VMIN] = 0; /*
ttypar.c_cc [VTIME] = 30; /* 3 sec timeout (tenth' seconds) */
}
/* string read, 'we break for nobody' */
else if (mode == 3) {
ttypar.c_iflag = ICRNL; /* map CR to NL on input */
/* eg. end read on CR */
ttypar.c_oflag = 0;
ttypar.c_lflag = ICANON; /* erase and kill (bug workaround), try 0 */
ttypar.c_line = 0;
ttypar.c_cflag = B9600 | CS7 | CREAD | PARENB;
ttypar.c_cc [VINTR] = 0; /* normally DEL char */
ttypar.c_cc [VQUIT] = 0; /* normally FS char */
ttypar.c_cc [VERASE] = 0; /* normally # char */
ttypar.c_cc [VKILL] = 0; /* normally @ char */
ttypar.c_cc [VEOF] = 0; /* normally EOT char */
ttypar.c_cc [VEOL] = 0; /* normally NUL char */
}
else
return (0);

return (!ioctl (*fd, TCSETA, &ttypar));
}

Borje Jonsson
System Engineer
Telub Teknik AB

0 new messages