Advanced Serial Programming

Blog - Programming

User Rating: / 1
PoorBest 

This chapter covers advanced serial programming techniques using the ioctl(2) and select(2) system calls.

This is the final part of topic "Serial Programming Guide for POSIX Operation System". Source: http://www.easysw.com/~mike/serial/serial.html

 

Serial Port IOCTLs

In Chapter 2, Configuring the Serial Port we used the tcgetattr and tcsetattr functions to configure the serial port. Under UNIX these functions use the ioctl(2) system call to do their magic.

The ioctl system call takes three arguments:

    int ioctl(int fd, int request, ...);

The fd argument specifies the serial port file descriptor. The request argument is a constant defined in the <termios.h> header file and is typically one of the constants listed in Table 10.

Table 10 - IOCTL Requests for Serial Ports
RequestDescriptionPOSIX Function
TCGETS Gets the current serial port settings. tcgetattr
TCSETS Sets the serial port settings immediately. tcsetattr(fd, TCSANOW, &options)
TCSETSF Sets the serial port settings after flushing the input and output buffers. tcsetattr(fd, TCSAFLUSH, &options)
TCSETSW Sets the serial port settings after allowing the input and output buffers to drain/empty. tcsetattr(fd, TCSADRAIN, &options)
TCSBRK Sends a break for the given time. tcsendbreak, tcdrain
TCXONC Controls software flow control. tcflow
TCFLSH Flushes the input and/or output queue. tcflush
TIOCMGET Returns the state of the "MODEM" bits. None
TIOCMSET Sets the state of the "MODEM" bits. None
FIONREAD Returns the number of bytes in the input buffer. None

Getting the Control Signals

The TIOCMGET ioctl gets the current "MODEM" status bits, which consist of all of the RS-232 signal lines except RXD and TXD, listed in Table 11.

To get the status bits, call ioctl with a pointer to an integer to hold the bits, as shown in Listing 5 .

Listing 5 - Getting the MODEM status bits.

    #include <unistd.h>
#include <termios.h>

int fd;
int status;

ioctl(fd, TIOCMGET, &status);
Table 11 - Control Signal Constants
ConstantDescription
TIOCM_LE DSR (data set ready/line enable)
TIOCM_DTR DTR (data terminal ready)
TIOCM_RTS RTS (request to send)
TIOCM_ST Secondary TXD (transmit)
TIOCM_SR Secondary RXD (receive)
TIOCM_CTS CTS (clear to send)
TIOCM_CAR DCD (data carrier detect)
TIOCM_CD Synonym for TIOCM_CAR
TIOCM_RNG RNG (ring)
TIOCM_RI Synonym for TIOCM_RNG
TIOCM_DSR DSR (data set ready)

Setting the Control Signals

The TIOCMSET ioctl sets the "MODEM" status bits defined above. To drop the DTR signal you can use the code in Listing 6.

Listing 6 - Dropping DTR with the TIOCMSET ioctl.

    #include <unistd.h>
#include <termios.h>

int fd;
int status;

ioctl(fd, TIOCMGET, &status);

status &= ~TIOCM_DTR;

ioctl(fd, TIOCMSET, &status);

The bits that can be set depend on the operating system, driver, and modes in use. Consult your operating system documentation for more information.

Getting the Number of Bytes Available

The FIONREAD ioctl gets the number of bytes in the serial port input buffer. As with TIOCMGET you pass in a pointer to an integer to hold the number of bytes, as shown in Listing 7.

Listing 7 - Getting the number of bytes in the input buffer.

    #include <unistd.h>
#include <termios.h>

int fd;
int bytes;

ioctl(fd, FIONREAD, &bytes);

This can be useful when polling a serial port for data, as your program can determine the number of bytes in the input buffer before attempting a read.

Selecting Input from a Serial Port

While simple applications can poll or wait on data coming from the serial port, most applications are not simple and need to handle input from multiple sources.

UNIX provides this capability through the select(2) system call. This system call allows your program to check for input, output, or error conditions on one or more file descriptors. The file descriptors can point to serial ports, regular files, other devices, pipes, or sockets. You can poll to check for pending input, wait for input indefinitely, or timeout after a specific amount of time, making the select system call extremely flexible.

Most GUI Toolkits provide an interface to select; we will discuss the X Intrinsics ("Xt") library later in this chapter.

The SELECT System Call

The select system call accepts 5 arguments:

    int select(int max_fd, fd_set *input, fd_set *output, fd_set *error,
struct timeval *timeout);

The max_fd argument specifies the highest numbered file descriptor in the input, output, and error sets. The input, output, and error arguments specify sets of file descriptors for pending input, output, or error conditions; specify NULL to disable monitoring for the corresponding condition. These sets are initialized using three macros:

    FD_ZERO(&fd_set);
FD_SET(fd, &fd_set);
FD_CLR(fd, &fd_set);

The FD_ZERO macro clears the set entirely. The FD_SET and FD_CLR macros add and remove a file descriptor from the set, respectively.

The timeout argument specifies a timeout value which consists of seconds (timeout.tv_sec) and microseconds (timeout.tv_usec ). To poll one or more file descriptors, set the seconds and microseconds to zero. To wait indefinitely specify NULL for the timeout pointer.

The select system call returns the number of file descriptors that have a pending condition, or -1 if there was an error.

Using the SELECT System Call

Suppose we are reading data from a serial port and a socket. We want to check for input from either file descriptor, but want to notify the user if no data is seen within 10 seconds. To do this we'll need to use the select system call, as shown in Listing 8.

Listing 8 - Using SELECT to process input from more than one source.

    #include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>

int n;
int socket;
int fd;
int max_fd;
fd_set input;
struct timeval timeout;

/* Initialize the input set */
FD_ZERO(&input);
FD_SET(fd, &input);
FD_SET(sock, &input);

max_fd = (sock > fd ? sock : fd) + 1;

/* Initialize the timeout structure */
timeout.tv_sec = 10;
timeout.tv_usec = 0;

/* Do the select */
n = select(max_fd, &input, NULL, NULL, &timeout);

/* See if there was an error */
if (n < 0)
perror("select failed");
else if (n == 0)
puts("TIMEOUT");
else
{
/* We have input */
if (FD_ISSET(fd, &input))
process_fd();
if (FD_ISSET(sock, &input))
process_socket();
}

You'll notice that we first check the return value of the select system call. Values of 0 and -1 yield the appropriate warning and error messages. Values greater than 0 mean that we have data pending on one or more file descriptors.

To determine which file descriptor(s) have pending input, we use the FD_ISSET macro to test the input set for each file descriptor. If the file descriptor flag is set then the condition exists (input pending in this case) and we need to do something.

Using SELECT with the X Intrinsics Library

The X Intrinsics library provides an interface to the select system call via the XtAppAddInput(3x) and XtAppRemoveInput(3x) functions:

int XtAppAddInput(XtAppContext context, int fd, int mask,
XtInputProc proc, XtPointer data);
void XtAppRemoveInput(XtAppContext context, int input);

The select system call is used internally to implement timeouts, work procedures, and check for input from the X server. These functions can be used with any Xt-based toolkit including Xaw, Lesstif, and Motif.

The proc argument to XtAppAddInput specifies the function to call when the selected condition (e.g. input available) exists on the file descriptor. In the previous example you could specify the process_fd or process_socket functions.

Because Xt limits your access to the select system call, you'll need to implement timeouts through another mechanism, probably via XtAppAddTimeout(3x).



Add this page to your favorite Social Bookmarking websites

Add comment


Security code
Refresh

Idioms

  • Grasp all, lose all. (Tham thì thâm)
  • Many men, many minds (Chín người, mười ý)

Who's online

We have 29 guests online

Location

 38.107.179.216
 38.107.179.216
Search Bot
 unknown unkno

Relax

Site Ranking


Increase your website traffic with Attracta.com

Quick Search

This Day in History

Poll

What is your current operating system?
 

Weather

Newsletter

Copyright © 2012 Thong D. Nguyen. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.