Thanks!
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
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
>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. ;-)
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
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