I recently read the linux serial port and found it quite easy.
Make some summary and record
[This article also focuses on backup and record. The Code applies to others, so it basically only lists some code, but ensures that the Code is available]
In fact, the serial port operation is just a few steps
1. Open the serial port
2. Set Parameters
3. send and receive
4. Close as needed
According to the form provided by the function,
Generally, you can set the parameters in two steps. (In fact, you can configure the parameters in several steps, but you can use the following code]
[1] set the baud rate
[2] setting the data format
Some code is listed below
Open serial port
Int OpenDev (char * Dev)
{
Int fd = open (Dev, O_RDWR );
If (-1 = fd)
{
Perror ("Can't Open Serial Port ");
Return-1;
}
Else
Return fd;
}
O_RDWR indicates the read/write ratio.
Code
Int speed_arr [] = {B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300 ,};
Int name_arr [] = {38400,192 00, 9600,480 0, 2400,120 0, 300,384 00,
19200,960 0, 4800,240 0, 1200,300 ,};
Void set_speed (int fd, int speed ){
Int I;
Int status;
Struct termios Opt;
Tcgetattr (fd, & Opt );
For (I = 0; I <sizeof (speed_arr)/sizeof (int); I ++ ){
If (speed = name_arr [I]) {
Tcflush (fd, TCIOFLUSH );
Cfsetispeed (& Opt, speed_arr [I]);
Cfsetospeed (& Opt, speed_arr [I]);
Status = tcsetattr (fd, TCSANOW, & Opt );
If (status! = 0 ){
Perror ("tcsetattr fd1 ");
Return;
}
Tcflush (fd, TCIOFLUSH );
}
}
}
The parameter setting uses a dedicated struct termios, which is used to configure serial parameters.
Pay attention to tcflush, which is used to clear the buffer. The buffer is actually quite cool in it. It is not described here, note that the buffer to be cleared is not the so-called buffer in the printf function.
The other two arrays are actually simpler, but they are too lazy to change.
It is a bit like the use of paint brush, old is generally love to save, and finally to restore.
Set Data Format
Int set_Parity (int fd, int databits, int stopbits, int parity)
{
Struct termios options;
If (tcgetattr (fd, & options )! = 0 ){
Perror ("SetupSerial 0 ");
Return-1;
}
Options. c_cflag & = ~ CSIZE;
Switch (databits)
{
Case 7:
Options. c_cflag | = CS7;
Break;
Case 8:
Options. c_cflag | = CS8;
Break;
Default:
Fprintf (stderr, "Unsupported data size \ n ");
Return-1;
}
Switch (parity)
{
Case 'N ':
Case 'N ':
Options. c_cflag & = ~ PARENB;
Options. c_iflag & = ~ INPCK;
Break;
Case 'O ':
Case 'O ':
Options. c_cflag | = (PARODD | PARENB );
Options. c_iflag | = INPCK;
Break;
Case 'E ':
Case 'E ':
Options. c_cflag | = PARENB;
Options. c_cflag & = ~ PARODD;
Options. c_iflag | = INPCK;
Break;
Case's ':
Case's ':
Options. c_cflag & = ~ PARENB;
Options. c_cflag & = ~ CSTOPB; break;
Default:
Fprintf (stderr, "Unsupported parity \ n ");
Return-1;
}
Switch (stopbits)
{
Case 1:
Options. c_cflag & = ~ CSTOPB;
Break;
Case 2:
Options. c_cflag | = CSTOPB;
Break;
Default:
Fprintf (stderr, "Unsupported stop bits \ n ");
Return-1;
}
If (parity! = 'N ')
Options. c_iflag | = INPCK;
Tcflush (fd, TCIFLUSH );
Options. c_cc [VTIME] = 150;
Options. c_cc [VMIN] = 0;
If (tcsetattr (fd, TCSANOW, & options )! = 0)
{
Perror ("SetupSerial 3 ");
Return-1;
}
Return 0;
}
Unlike the preceding function, struct termios is operated directly here, and the data bit length, check bit, stop bit, timeout and other information are configured.
Finally, a test case is listed.
# Include <stdio. h>
# Include <stdlib. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/stat. h>
# Include <fcntl. h>
# Include <termios. h>
# Include <errno. h>
Int speed_arr [] = {B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300 ,};
Int name_arr [] = {38400,192 00, 9600,480 0, 2400,120 0, 300,384 00,
19200,960 0, 4800,240 0, 1200,300 ,};
Void set_speed (int fd, int speed ){
Int I;
Int status;
Struct termios Opt;
Tcgetattr (fd, & Opt );
For (I = 0; I <sizeof (speed_arr)/sizeof (int); I ++ ){
If (speed = name_arr [I]) {
Tcflush (fd, TCIOFLUSH );
Cfsetispeed (& Opt, speed_arr [I]);
Cfsetospeed (& Opt, speed_arr [I]);
Status = tcsetattr (fd, TCSANOW, & Opt );
If (status! = 0 ){
Perror ("tcsetattr fdl ");
Return;
}
Tcflush (fd, TCIOFLUSH );
}
}
}
Int set_Parity (int fd, int databits, int stopbits, int parity)
{
Struct termios options;
If (tcgetattr (fd, & options )! = 0 ){
Perror ("SetupSerial 1 ");
Return-1;
}
Options. c_cflag & = ~ CSIZE;
Switch (databits)
{
Case 7:
Options. c_cflag | = CS7;
Break;
Case 8:
Options. c_cflag | = CS8;
Break;
Default:
Fprintf (stderr, "Unsupported data size \ n"); return 0;
}
Switch (parity)
{
Case 'N ':
Case 'N ':
Options. c_cflag & = ~ PARENB;
Options. c_iflag & = ~ INPCK;
Break;
Case 'O ':
Case 'O ':
Options. c_cflag | = (PARODD | PARENB );
Options. c_iflag | = INPCK;
Break;
Case 'E ':
Case 'E ':
Options. c_cflag | = PARENB;
Options. c_cflag & = ~ PARODD;
Options. c_iflag | = INPCK;
Break;
Case's ':
Case's ':
Options. c_cflag & = ~ PARENB;
Options. c_cflag & = ~ CSTOPB; break;
Default:
Fprintf (stderr, "Unsupported parity \ n ");
Return-1;
}
Switch (stopbits)
{
Case 1:
Options. c_cflag & = ~ CSTOPB;
Break;
Case 2:
Options. c_cflag | = CSTOPB;
Break;
Default:
Fprintf (stderr, "Unsupported stop bits \ n ");
Return-1;
}
If (parity! = 'N ')
Options. c_iflag | = INPCK;
Tcflush (fd, TCIFLUSH );
Options. c_cc [VTIME] = 150;
Options. c_cc [VMIN] = 0;
If (tcsetattr (fd, TCSANOW, & options )! = 0)
{
Perror ("SetupSerial 3 ");
Return-1;
}
Return 0;
}
Int OpenDev (char * Dev)
{
Int fd = open (Dev, O_RDWR );
If (-1 = fd)
{
Perror ("Can't Open Serial Port ");
Return-1;
}
Else
Return fd;
}
Int main (int argc, char ** argv ){
Int fd;
Int nread;
Char buff [6];
Char * dev = "/dev/ttyS0 ";
Fd = OpenDev (dev );
Set_speed (fd, 9600 );
If (set_Parity (fd, 8, 1, 'n') = FALSE ){
Printf ("Set Parity Error \ n ");
Exit (0 );
}
While (1 ){
Write (fd, "hello", 5 );
Nread = read (fd, buff, 5 );
Tcflush (fd, TCIOFLUSH );
Buff [nread + 1] = '\ 0 ';
Printf ("% s", buff );
}
// Close (fd );
// Exit (0 );
}
This test will send and accept the hello characters, mainly to test the self-reception, if there is a serial port, you can use a patch cap to connect two or three ends to this test. We can see that hellohello is constantly appearing in the terminal ...... That's right! My notebook is modern ..... Why is there a serial port in XD, so there is a whole card. It is said that it is more stable than the usb adapter, and no stability is found yet