Chinaunix首页 | 论坛 | 博客
  • 博客访问: 584002
  • 博文数量: 141
  • 博客积分: 3425
  • 博客等级: 中校
  • 技术积分: 1609
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-23 15:55
文章分类

全部博文(141)

文章存档

2019年(5)

2011年(19)

2010年(36)

2009年(13)

2008年(50)

2007年(18)

分类: C/C++

2009-02-23 16:46:04

Linux Serial Program Note


open options

fd = open("/dev/ttyf1", O_RDWR | O_NOCTTY | O_NONBLOCK);
O_RDWR : read write
O_NOCTTY:not controlling terminal
O_NONBLOCK: none blocking

write 

write(fd, buffer, n);

read

read(fd, buffer, n);

close

close(fd);

Termios Structure Members

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)

Constants for the c_cflag Member

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
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
CRTSCTS  : Enable hardware flow control 

Setting the baud rate

struct termios options; 
 
/* 
 * Get the current options for the port... 
 */ 
 
tcgetattr(fd, &options); 
 
/* 
 * Set the baud rates to 19200... 
 */ 
 
cfsetispeed(&options, B19200); 
cfsetospeed(&options, B19200); 
 
/* 
 * Enable the receiver and set local mode... 
 */ 
 
options.c_cflag |= (CLOCAL | CREAD); 
 
/* 
 * Set the new options for the port... 
 */ 
 
tcsetattr(fd, TCSANOW, &options); 

Constants for tcsetattr
TCSANOW   : Make changes now without waiting for data to complete
TCSADRAIN : Wait until everything has been transmitted
TCSAFLUSH : Flush input and output buffers and make the change

Constants for the c_lflag Member

ISIG      : Enable SIGINTR, SIGSUSP, SIGDSUSP, and SIGQUIT signals
ICANON    : Enable canonical input (else raw)
XCASE     : Map uppercase \lowercase (obsolete)
ECHO      : Enable echoing of input characters
ECHOE     : Echo erase character as BS-SP-BS
ECHOK     : Echo NL after kill character
ECHONL    : Echo NL
NOFLSH    : Disable flushing of input buffers after interrupt or quit characters
IEXTEN    : Enable extended functions
ECHOCTL   : Echo control characters as ^char and delete as ~?
ECHOPRT   : Echo erased character as character erased
ECHOKE    : BS-SP-BS entire line on line kill
FLUSHO    : Output being flushed
PENDIN    : Retype pending input at next read or input char
TOSTOP    : Send SIGTTOU for background output

Choosing Canonical Input 
Canonical input is line-oriented. Input characters are put into a buffer which can be edited interactively 
by the user until a CR (carriage return) or LF (line feed) character is received.  
When selecting this mode you normally select the ICANON, ECHO, and ECHOE options:  
options.c_lflag |= (ICANON | ECHO | ECHOE); 
Choosing Raw Input 
Raw input is unprocessed. Input characters are passed through exactly as they are received, when they 
are received. Generally you'll deselect the ICANON, ECHO, ECHOE, and ISIG options when using raw input:  
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 

Constants for the c_iflag Member

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
INLCR    : Map NL to CR
IGNCR    : Ignore CR
ICRNL    : Map CR to NL
IUCLC    : Map uppercase to lowercase
IMAXBEL  : Echo BEL on input line too long

Setting Input Parity Options

You should enable input parity checking when you have enabled parity in the c_cflag member 
(PARENB). The revelant constants for input parity checking are INPCK, IGNPAR, PARMRK , and 
ISTRIP. Generally you will select INPCK and ISTRIP to enable checking and stripping of the parity bit: 

options.c_iflag |= (INPCK | ISTRIP); 

IGNPAR is a somewhat dangerous option that tells the serial driver to ignore parity errors and pass the 
incoming data through as if no errors had occurred. This can be useful for testing the quality of a 
communications link, but in general is not used for practical reasons.  

PARMRK causes parity errors to be 'marked' in the input stream using special characters. If IGNPAR is 
enabled, a NUL character (000 octal) is sent to your program before every character with a parity error.
Otherwise, a DEL (177 octal) and NUL character is sent along with the bad character.  

Constants for the c_oflag Member

Constant                   Description
OPOST    : Postprocess output (not set = raw output)
OLCUC    : Map lowercase to uppercase
ONLCR    : Map NL to CR-NL
OCRNL    : Map CR to NL
NOCR     : No CR output at column 0
ONLRET   : NL performs CR function
OFILL    : Use fill characters for delay
OFDEL    : Fill character is DEL
NLDLY    : Mask for delay time needed between lines
NL0      : No delay for NLs
NL1      : Delay further output after newline for 100 milliseconds
CRDLY    : Mask for delay time needed to return carriage to left column
CR0      : No delay for CRs
CR1      : Delay after CRs depending on current column position
CR2      : Delay 100 milliseconds after sending CRs
CR3      : Delay 150 milliseconds after sending CRs
TABDLY   : Mask for delay time needed after TABs
TAB0     : No delay for TABs
TAB1     : Delay after TABs depending on current column position
TAB2     : Delay 100 milliseconds after sending TABs
TAB3     : Expand TAB characters to spaces
BSDLY    : Mask for delay time needed after BSs
BS0      : No delay for BSs
BS1      : Delay 50 milliseconds after sending BSs
VTDLY    : Mask for delay time needed after VTs
VT0      : No delay for VTs
VT1      : Delay 2 seconds after sending VTs
FFDLY    : Mask for delay time needed after FFs
FF0      : No delay for FFs
FF1      : Delay 2 seconds after sending FFs

Choosing Raw Output 
Raw output is selected by resetting the OPOST option in the c_oflag member:  
options.c_oflag &= ~OPOST; 
When the OPOST option is disabled, all other option bits in c_oflag are ignored.  

Control Characters in the c_cc Member
Constant      Description      Key
VINTR        Interrupt        CTRL-C
VQUIT        Quit             CTRL-Z
VERASE       Erase            Backspace (BS)
VKILL        Kill-line        CTRL-U
VEOF         End-of-file      CTRL-D
VEOL         End-of-line      Carriage return (CR)
VEOL2        Second EOF       Line feed (LF)
VMIN         Minimum number of characters to read
VTIME        Time to wait for data (tenths of seconds)

Setting Read Timeouts 
UNIX serial interface drivers provide the ability to specify character and packet timeouts. Two 
elements of the c_cc array are used for timeouts: VMIN and VTIME. Timeouts are ignored in 
canonical input mode or when the NDELAY option is set on the file via open or fcntl. 
VMIN specifies the minimum number of characters to read. If it is set to 0, then the VTIME value 
specifies the time to wait for every character read. Note that this does not mean that a read call for N 
bytes will wait for N characters to come in. Rather, the timeout will apply to the first character and the 
read call will return the number of characters immediately available (up to the number you request).  
If VMIN is non-zero, VTIME specifies the time to wait for the first character read. If a character is read 
within the time given, any read will block (wait) until all VMIN characters are read. That is, once the 
first character is read, the serial interface driver expects to receive an entire packet of characters (VMIN
bytes total). If no character is read within the time allowed, then the call to read returns 0. This method 
allows you to tell the serial driver you need exactly N bytes and any read call will return 0 or N bytes. 
However, the timeout only applies to the first character read, so if for some reason the driver misses 
one character inside the N byte packet then the read call could block forever waiting for additional 
input characters.  
VTIME specifies the amount of time to wait for incoming characters in tenths of seconds. If VTIME is 
set to 0 (the default), reads will block (wait) indefinitely unless the NDELAY option is set on the port 
with open or fcntl.  

阅读(984) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~