Linux under the preliminary search for serial port configuration

First, in the struct termios structure , the basic configuration of the serial port (e.g. baud rate setting, parity and stop bits set , etc.). 

( A ):

struct termios // port is provided mainly set members struct termios structure
      {
 tcflag_t the c_iflag; // the flags INPUT MODE   input mode flag.
 tcflag_t c_oflag; // output mode flags   output mode flag
 tcflag_t c_cflag; // control mode flags   control mode flag
 tcflag_t c_lflag; // local mode flags   local mode flag.

cc_t c_line; // line discipline   line procedure (rate).
 cc_t c_cc [NCCS]; // control  characters control character array
      }; 

. 1 , the c_cflag representing the control mode

CLOCAL meaning to ignore all modem status line, the aim is to ensure that the program will not take up the serial port. CREAD enable character representative of a receiver, object number can be read Yes inputted from the serial data.

CS5 / 6/7/8 indication is transmitted or received character 5/6/7/8 bits.

CSTOPB represent each character using two stop bits.

HUPCL represents modem hang off.

PARENB: Enable generation and detection parity code.

PARODD only odd without using even parity.

2 , the c_iflag represents the input pattern.

BRKINT: when detecting a termination of a state in which the input line , generates an interrupt.

TGNBRK: Ignore the termination state of the input line.

TCRNL: will receive the converted carriage return newline.

TGNCR: Ignore received newline.

INLCR: will receive the new line breaks to carriage return.

IGNPAR: characters are ignored parity check error.

INPCK: received character to perform parity check.

PARMRK: Marking of parity check error.

ISTRIP: all received characters to cut 7 bits.

IXOFF: Enable software flow control on input.

IXON: Enable software flow control on the output.

3 , c_cc special control characters.

Nonstandard mode and standard mode , c_cc array indices have different values :

( 1 ) Normal mode :

VEOF: EOF character

VEOL: EOF character

Vera: ERASE 字符

VINTR: INTR character

VKILL: KILL character

VQUIT: QUIT character

VSTART: START character

VSTOP: STOP character

(2) non-standard mode :

VINTR: INTR character

VMIN: MIN value

VQUIT: QUIT character

VSUSP: SUSP character

VTIME: TIME value

VSTART: START character

 VSTOP: STOP character

( Two ): wherein, by c_cflag assignment can set the baud rate, character size, data bits, stop bits, parity bit and hardware flow control.

Programs such as:

struct termios options; // serial array structure
tcgetattr (fd, & options); // get the current setting
bzero (& Options, the sizeof (Options));
options.c_cflag | = B115200 | the CLOCAL | CREAD,; // set the baud rate, local connection, receiving enabling
options.c_cflag & = ~ CSIZE; // bit mask data
options.c_cflag | = CS8; // data bits. 8, for CS7. 7
options.c_cflag CSTOPB is & = ~; // a stop bit , two stop is | = CSTOPB is
options.c_cflag PARENB is & = ~; // no parity
 //options.c_cflag | = PARENB; // check has
//options.c_cflag & = ~ PARODD // parity
/ /options.c_cflag | = PARODD // odd parity

options.c_cc [VTIME] = 0; // wait time in hundreds of milliseconds

//************************************************************************************

Satisfies the condition or not the read buffer in the remaining data is 0 is read out after hundreds of milliseconds. Further special attention when setting the VTIME, if the third parameter is less than VMIN read, will be modified to read VMIN third parameter, i.e. the use of read (fd, & buf, m ) ;, to the next setting becomes: options.c_cc [VMIN] = m;

************************************************** // **********************************
options.c_cc [VMIN] = 0; // byte minimum number

//************************************************************************************

( . 1 ) VMIN = 0, when the buffer byte count> = 0 read operation, the read serial operation at this time is actually not blocked, because the condition is always satisfied.

( 2 ) VMIN =. 1, when the number of buffer byte> 1 = read operation, data read port when no operation is blocked.

( . 3 ) VMIN = 4, the buffer byte count> = 4 when a read operation, or the read operation of the serial port is blocked. Maximum number of bytes on each read to read function is determined by the third parameter. The remaining data until the buffer <read and the third parameter <4 (at this time, if the third parameter is read four times a read operation until the read buffer, read as the third parameter is 2, the continuous read operation until the buffer is empty or left one character).

( 4 ) not set VTIME, the period remaining characters is not determined until the next reading to meet the conditions of the time was only read out.

***********************************************************************************//
tcflush(fd, TCIOFLUSH);

// TCIFLUSH brush clear input queue.
                                       // TCOFLUSH brush clearing the output queues. 
                                       // TCIOFLUSH brush clear input and output queues.
tcsetattr (fd, TCSANOW, & options );

// TCSANOW立即生效;
                                    //TCSADRAIN:Wait until everything has been transmitted;
                                  //TCSAFLUSH:Flush input and output buffers and make the change

In the above, when setting the baud rate. A prefix number 'B' . Example of program:

cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);

Second, the serial port operation. ( Serial port is a terminal device ) .

(A) Use the following command:

Open the serial port: fd = Open ( "/ dev / ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

O_RDWR Open for reading and writing;
O_NOCTTY process management does not allow the serial port; notification linix system, this program will not be a port of the control system terminals.
 O_NDELAY non-blocking (blocking by default, after opening can also use fcntl () reset) . Notify Linux system does not care about the DCD signal line in which the state (the other end of the port is activated or stopped).

Write serial : n-Write = (FD, "Linux",. 5);
                 n-number of bytes actually written;

Reading the serial : res = Read (FD, buf, len);
                 the number of bytes read res;

Set Serial : the fcntl (FD, the F_SETFL, FNDELAY); // nonblocking
                 fcntl (fd, F_SETFL, 0) ; // blocked

Close Serial : Close (FD);

(B) general:

1, in the creation of a function open_port () function to be achieved:

Programs such as:

(1) open ( "/ dev / ttys0", O_RDWR | O_NOCTTY | O_NDELAY); / * open the port 0 * /
(2) the fcntl (FD, the F_SETFL, 0) / * restore the serial block state * /
(. 3) isatty (STDIN_FILENO) / * test whether the interrupt device that is non-zero interrupting device * /

2 , serial configuration parameters function set_opt () function to be achieved:
(1) Save the original serial port configuration
tcgetattr (fd, & oldtio);

(2) first cleared new serial port 0
bzore (& newtio, the sizeof (newito));

(3) activation option CLOCAL CREAD and for receiving and using the local connection ;

newtio.c_cflag |=CLOCAL | CREAD;

(4) and sets the data bit size , need to use a mask provided
newtio.c_cflag & = ~ CSIZE;
newtio.c_cflag | = CS8;

(5) The parity
Odd :
newtio.c_cflag | = PARENB is;
newtio.c_cflag | =, PARODD;
CREAD | = (the INPCK | ISTRIP is);
parity :
CREAD | = (the INPCK | ISTRIP is) ;
newtio.c_cflag | = PAREND;
newtio.c_cflag & = ~, PARODD;
without parity:
newtio.c_cflag PARENB is & = ~;

(6) setting the stop bit , by activating achieved in c_cflag CSTOPB. If the stop bit is 1, then remove CSTOPB, if the stop bit is 2, then activate CSTOPB.
newtio.c_cflag & = ~ CSTOPB; / * * stop bits. 1 /
newtio.c_cflag | = CSTOPB is; / stop bit is 0 * * /

(7) set the baud rate:
cfsetispeed (& newtio, B115200);
cfsetospeed (& newtio, B115200);

(8) set the wait time and minimum acceptable character . There is no particular requirement for the received character and waiting time can be set to 0:
newtio.c_cc [VTIME] = 0;
newtio.c_cc [VMIN] = 0;

(9) to receive a character processing ( processing reference object to be written ). tcflush brush cleaning function (discarded) input buffer (terminal driver has been received, not yet been read from a user program) or the output buffer (user program has been written, but not yet sent).
tcflush (FD, TCIFLUSH);

// TCIFLUSH brush clear input queue . That data is refreshed received but not read

   // TCOFLUSH brush clearing the output queues . That refresh the data written but not transmitted

   // TCIOFLUSH brush clear input and output queues . That is, at the same time to refresh the data received but not read, write and refresh the number of data but not transmit.

(10) to activate the new configuration:
the tcsetattr (FD, TCSANOW, & newtio);
. 3 , serial read and write
Write (FD, BUFF,. 8);
Read (FD, BUFF,. 8);

(C) In addition:

1, obtain the file the flags, i.e., open the second parameter of the function:

       flags = fcntl(fd,F_GETFL,0);

2, set the file's flags:

      fcntl(fd,F_SETFL,flags);

3, increase the flags of a file, such as the file is blocked, I would like to set up a non-blocking:

       flags = fcntl(fd,F_GETFL,0);

       flags |= O_NONBLOCK;

      fcntl(fd,F_SETFL,flags);

4 to cancel a file flags, such as the file is non-blocking, want to set to be blocked:

      flags = fcntl(fd,F_GETFL,0);

      flags &= ~O_NONBLOCK;

      fcntl(fd,F_SETFL,flags);

 

Guess you like

Origin www.cnblogs.com/L-102/p/11404706.html