Serial Port: serialport.h File Reference

serialport.h File Reference

Some utils of the serial port, such as open the port, close the port and setup the port. More...

Go to the source code of this file.


int open_port (int port)
int close_port (int fd)
int setup_port (int fd, int speed, int data_bits, int parity, int stop_bits)

Detailed Description

Some utils of the serial port, such as open the port, close the port and setup the port.

Copyleft (C) 2010 Late Lee This program is tested on LINUX PLATFORM, WITH GCC 4.x. The program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. Please feel free to use the program, and I feel free to ignore the related issues. Any questions or suggestions, or bugs, please contact me at <$ echo -n "aHR0cDovL3d3dy5sYXRlbGVlLm9yZwo=" | base64 -d> or e-mail to <$ echo -n "bGF0ZWxlZUAxNjMuY29tCg==" | base64 -d> if you want to do this.

Late Lee
Mon Jan 10 2011

Function Documentation

int close_port ( int  fd)

close_port - Close the port

fd: The file description returned by open_port().
Return 0 if success, return -1 with some msg if error happens.
int open_port ( int  port)

open_port - Open a serial port

port: The port number, eg, open_port(1) will open port 1.
Return fd if success, otherwise will return -1 with some msg.
Each serial port on a UNIX system has one or more device files (files in the /dev directory) associated with it:
System         Port 1      Port 2
IRIX          /dev/ttyf1   /dev/ttyf2
HP-UX         /dev/tty1p0  /dev/tty2p0
Solaris/SunOS /dev/ttya    /dev/ttyb
Linux         /dev/ttyS0   /dev/ttyS1
Linux         /dev/ttyUSB0 /dev/ttyUSB1 (usb-serial converter)
Digital UNIX  /dev/tty01   /dev/tty02
(Windows       COM1        COM2)

We open the port specified by port number, for instance, if you want to open port 1, you just call the function like this: open_port(1).

int setup_port ( int  fd,
int  speed,
int  data_bits,
int  parity,
int  stop_bits 

setup_port - Configure the port, eg. baud rate, data bits,etc.

fd: The serial port
speed: The baud rate
data_bits: The data bits
parity: The parity bits
stop_bits: The stop bits
Return 0 if everything is OK, otherwise -1 with some error msg.
Here are termios structure members:
Member      Description 
c_cflag     Control options 
c_lflag     Line options 
c_iflag     Input options 
c_oflag     Output options 
c_cc        Control characters 
c_ispeed    Input baud (new interface) 
c_ospeed    Output baud (new interface) 

The c_cflag member controls the baud rate, number of data bits, parity, stop bits, and hardware flow control. There are constants for all of the supported configurations. Constant Description

CBAUD	Bit mask for baud rate 
B0	0 baud (drop DTR) 
B50	50 baud 
B75	75 baud 
B110	110 baud 
B134	134.5 baud 
B150	150 baud 
B200	200 baud 
B300	300 baud 
B600	600 baud 
B1200	1200 baud 
B1800	1800 baud 
B2400	2400 baud 
B4800	4800 baud 
B9600	9600 baud 
B19200	19200 baud 
B38400	38400 baud 
B57600	57,600 baud 
B76800	76,800 baud 
B115200	115,200 baud 
EXTA	External rate clock 
EXTB	External rate clock 
CSIZE	Bit mask for data bits 
CS5 5	data bits 
CS6 6	data bits 
CS7 7	data bits 
CS8 8	data bits 
CSTOPB	2 stop bits (1 otherwise) 
CREAD	Enable receiver 
PARENB	Enable parity bit 
PARODD	Use odd parity instead of even 
HUPCL	Hangup (drop DTR) on last close 
CLOCAL	Local line - do not change "owner" of port 
LOBLK	Block job control output 
CNEW_RTSCTS CRTSCTS	Enable hardware flow control (not supported on all 

The input modes member c_iflag controls any input processing that is done to characters received on the port. Like the c_cflag field, the final value stored in c_iflag is the bitwise OR of the desired options.

Constant	Description 
INPCK	Enable parity check 
IGNPAR	Ignore parity errors 
PARMRK	Mark parity errors 
ISTRIP	Strip parity bits 
IXON	Enable software flow control (outgoing) 
IXOFF	Enable software flow control (incoming) 
IXANY	Allow any character to start flow again 
IGNBRK	Ignore break condition 
BRKINT	Send a SIGINT when a break condition is detected 
IGNCR	Ignore CR 
IUCLC	Map uppercase to lowercase 
IMAXBEL	Echo BEL on input line too long 

Here are some examples of setting parity checking:

No parity (8N1):

options.c_cflag &= ~PARENB
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;

Even parity (7E1):

options.c_cflag |= PARENB
options.c_cflag &= ~PARODD
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;

Odd parity (7O1):

options.c_cflag |= PARENB
options.c_cflag |= PARODD
options.c_cflag &= ~CSTOPB
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;