Analysis of simple program code of Linux serial port Testing Tool

Source: Internet
Author: User

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.

  1. Learning notes how to build an SVN server in Linux
  2. Development and Application of linux Network Monitoring System
  3. 17 alternatives to the Windows software environment in Linux
  4. System Monitoring: linux System Monitoring command details
  5. Introduction to installing and using the Linux stress testing tool webbench

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.