Some common serial port properties of the Setup method.
- Set flow control
- Termios_new.c_cflag &= ~crtscts; Do not use flow control
- Termios_new.c_cflag |= crtscts; Using hardware flow control
- Termios_new.c_iflag |= ixon| Ixoff| Ixany; Using Software flow control
- Masked character size bit
- Termios_new.c_cflag &= ~csize;
- Set data bit size
- Termios_new.c_cflag |= CS8; Using 8-bit data bits
- Termios_new.c_cflag |= CS7; Using 7-bit data bits
- Termios_new.c_cflag |= CS6; Using 6-bit data bits
- Termios_new.c_cflag |= CS5; Using 5-bit data bits
- Set parity mode
- Termios_new.c_cflag &= ~parenb; No parity check
- Termios_new.c_cflag |= Parenb; Odd check
- Termios_new.c_cflag &= ~parodd;
- Termios_new.c_cflag |= Parenb; Even check
- Termios_new.c_cflag &= ~parodd;
- Stop bit
- Termios_new.c_cflag |= CSTOPB; 2-bit Stop bit
- Termios_new.c_cflag &= ~CSTOPB; 1-bit stop bit
- Output mode
- Termios_new.c_cflag &= ~opost; Raw data (raw) output
- Control characters
- Termios_new.c_cc[vmin] = 1; Minimum number of characters to read
- Termios_new.c_cc[vtime] = 1; Read the wait time for the first character
- When the terminal echo is turned off, the keyboard input characters are not displayed in the terminal window.
- #include <stdio.h>
- #include <stdlib.h>
- #include <termios.h>
- #include <unistd.h>
- int main (void)
- {
- struct Termios ts,ots;
- Char passbuf[1024];
- Tcgetattr (Stdin_fileno,&ts); /* The value of Stdin_fileno is 1, which represents the file descriptor for standard input */
- ots = ts;
- Ts.c_lflag &= ~echo; /* Turn back terminal echo function */
- Ts.c_lflag |= echonl;
- Tcsetattr (Stdin_fileno,tcsaflush,&ts); /* Apply new terminal settings */
- Fgets (Passbuf,1024,stdin); /* Input characters are not displayed in the terminal */
- printf ("you input character =%s/n", passbuf);
- Tcsetattr (stdin_fileno,tcsanow,&ots); /* Restore the old terminal equipment */
}
Example: Serial port parameter setting function
int Serial::setpara (int serialfd,int speed,int databits, int stopbits, int parity)
{
struct Termios termios_new;
Bzero (&termios_new, sizeof (termios_new));//equivalent to memset (&termios_new,sizeof (termios_new));
Cfmakeraw (&termios_new);//The terminal is set to the original mode
Termios_new.c_cflag=baudrate (speed);
Termios_new.c_cflag |= clocal | Cread; Termios_new.c_iflag = Ignpar | IGNBRK;
Termios_new.c_cflag &= ~csize;
Switch (databits)
{
Case 0:
Termios_new.c_cflag |= CS5;
Break
Case 1:
Termios_new.c_cflag |= CS6;
Break
Case 2:
Termios_new.c_cflag |= CS7;
Break
Case 3:
Termios_new.c_cflag |= CS8;
Break
Default
Termios_new.c_cflag |= CS8;
Break
}
Switch (parity)
{
Case 0://as no parity
Termios_new.c_cflag &= ~parenb; Clear parity enable
Break
Case 1:
Termios_new.c_cflag |= Parenb; Enable Parity
Termios_new.c_cflag &= ~parodd;
Break
Case 2:
Termios_new.c_cflag |= Parenb;
Termios_new.c_cflag |= ~parodd;
Break
Default
Termios_new.c_cflag &= ~parenb; Clear parity enable
Break
}
Switch (stopbits)//Set Stop Bit
{
Case 1:
Termios_new.c_cflag &= ~CSTOPB;
Break
Case 2:
Termios_new.c_cflag |= CSTOPB;
Break
Default
Termios_new.c_cflag &= ~CSTOPB;
Break
}
Tcflush (Serialfd,tciflush); Clear input Cache
Tcflush (Serialfd,tcoflush); Clear output cache
Termios_new.c_cc[vtime] = 1; The MIN and time combinations have the following four types: 1.MIN = 0, time = 0 has read immediate callback otherwise returns 0, does not read any characters
Termios_new.c_cc[vmin] = 1; 2, MIN = 0, time >0 read back to the reading of the character, or in a very second after the time if it is too late to read any characters, then return 0
Tcflush (SERIALFD, Tciflush); 3, min > 0, Time =0 read will wait until MIN character is readable
Return tcsetattr (serialfd,tcsanow,&termios_new); 4, MIN > 0, Time > 0 The timer between each lattice is started. Read reads the MIN character, returns the value, or
}
int serial::baudrate (int baudrate)
{
Switch (baudrate)
{
Case 0:
return (B2400);
Case 1:
return (B4800);
Case 2:
return (B9600);
Case 3:
return (B19200);
Case 4:
return (B38400);
Case 5:
return (B57600);
Case 6:
return (B115200);
Default
return (B9600);
}
}
Linux serial port programming record (a) serial port parameter setting