Shane wrote:
> Hi, If anyone can provide me with g++ code to configure a serial
> port...buad, bits etc..no handshaking required, and how I can switch
> echo on and off. Simple code please..no buffers, or streams need to
> be handled purely capture and autoecho a single byte.
a single byte implies you want binary (raw) mode.
So using Ronan's code to config the serial port should be
all you need to setup the communications parameters.
You may also want to set the CLOCAL bit on options.c_cflag
so that you don't have to worry about DCD.
Note read and write is quite tricky for serial ports,
as one usually has to handle timeouts yourself.
Your app seems simple so perhaps just the following will work.
while(;;) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
if (select(fd+1,&rfds,(fd_set*)NULL,(fd_set*)NULL,NULL) > 0) {
unsigned char character;
read(fd,&character,1);
write(fd,&character,1);
}
}
For completeness to handle read timeouts
I generally use select rather using the termios support
which one can set up like:
options.c_cc[VTIME]=100; //reads timeout after 10s
options.c_cc[VMIN]=0; //above timeout is absolute
http://www.unixwiz.net/techtips/termios-vmin-vtime.html
Write timeouts are even more problematic,
and I usually handle them with signals.
So after the following setup I wrap every
write() and tcdrain() call with alarm(10); ... alarm(0);
static void write_timeout(int sig) { return; }
struct sigaction action;
action.sa_handler = write_timeout;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
sigaction (SIGALRM, &action, NULL);
It's also worth noting that on error paths for example
it is worth doing a tcflush() as tcdrain() is called automatically by
close().
Pádraig.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!