Since the first HELLO program has been completed, it indicates that the entire compilation environment is no longer correct. Now, let's prepare a serial port testing program. As the serial port driver development board has been completed, let's take a look at a simple Linux serial port testing tool for data sending and receiving.
Common online versions of Linux serial port testing tools seem cumbersome. The following is a simple one. This program function sends the first seven bytes after receiving 10 bytes, exit if the first byte of the sent data is 9.
- /* Rs232_send.c */
- # Include
- # Include
- # Include
- # Include
- # Include
- # Include
- # Include
- # Include
- # Include
- # DefineBAUDRATEB115200 // 38400
- # DefineMODEMDEVICE "/dev/ttyS1"
- Int main ()
- {
- Int fd, c = 0, res;
- Struct termios oldtio, newtio;
- Intch;
- Static char s1 [20], buf [19];
- Printf ("start... \ n ");
- /* Open the COM1 port of the PC */
- Fd = open (MODEMDEVICE, O_RDWR | O_NOCTTY );
- If (fd
- {
- Perror (MODEMDEVICE );
- Exit (1 );
- }
- Printf ("open... \ n ");
- /* Store old communication parameters in the oldtio structure */
- Tcgetattr (fd, & oldtio );
- /* Initialize the new newtio */
- Bzero (& newtio, sizeof (newtio ));
- /* 8N1 */
- Newtio. c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
- Newtio. c_iflag = IGNPAR;
- Newtio. c_oflag = 0;
-
- /* Normal mode */
- /* Newtio. c_lflag = ICANON ;*/
- /* Abnormal mode */
- Newtio. c_lflag = 0;
- Newtio. c_cc [VTIME] = 0;
- Newtio. c_cc [VMIN] = 10;
-
- Tcflush (fd, TCIFLUSH );
- /* The New temios serves as the communication port parameter */
- Tcsetattr (fd, TCSANOW, & newtio );
- Printf ("writing... \ n ");
-
- While (1)
- {
- Res = read (fd, buf, 10 );
- Res = write (fd, buf, 7 );
- If (buf [0] = 9) break;
- }
-
- Printf ("close... \ n ");
- Close (fd );
- /* Restore the old parameter */
- Tcsetattr (fd, TCSANOW, & oldtio );
- Return 0;
- }
Note that there are two working modes for the serial port of the Linux serial port testing tool: regular mode and informal mode. If you are used to sending in hexadecimal mode in the serial port debugger, at this time, the serial port should be in the informal mode.
The following describes the two modes.
Linux serial port testing tool in standard mode CANONICAL or COOKED)
In this mode, the terminal device processes special characters and transmits data in one row. It starts to send and receive data only after pressing enter. For example, linux shell.
Linux serial port testing tool informal mode NON-CANONICAL or RAW)
In this mode, the terminal device does not process special characters, and the data transmission mode is one character at a time. You do not need to press enter to wrap the line. For example, VIM in LINUX.