Code Implementation 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.
 

 
 
  1. /* Rs232_send.c */
  2. # Include
  3. # Include
  4. # Include
  5. # Include
  6. # Include
  7. # Include
  8. # Include
  9. # Include
  10. # Include
  11. # DefineBAUDRATEB115200 // 38400
  12. # DefineMODEMDEVICE "/dev/ttyS1"
  13. Int main ()
  14. {
  15. Int fd, c = 0, res;
  16. Struct termios oldtio, newtio;
  17. Intch;
  18. Static char s1 [20], buf [19];
  19. Printf ("start... \ n ");
  20. /* Open the COM1 port of the PC */
  21. Fd = open (MODEMDEVICE, O_RDWR | O_NOCTTY );
  22. If (fd
  23. {
  24. Perror (MODEMDEVICE );
  25. Exit (1 );
  26. }
  27. Printf ("open... \ n ");
  28. /* Store old communication parameters in the oldtio structure */
  29. Tcgetattr (fd, & oldtio );
  30. /* Initialize the new newtio */
  31. Bzero (& newtio, sizeof (newtio ));
  32. /* 8N1 */
  33. Newtio. c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
  34. Newtio. c_iflag = IGNPAR;
  35. Newtio. c_oflag = 0;
  36.  
  37. /* Normal mode */
  38. /* Newtio. c_lflag = ICANON ;*/
  39. /* Abnormal mode */
  40. Newtio. c_lflag = 0;
  41. Newtio. c_cc [VTIME] = 0;
  42. Newtio. c_cc [VMIN] = 10;
  43.  
  44. Tcflush (fd, TCIFLUSH );
  45. /* The New temios serves as the communication port parameter */
  46. Tcsetattr (fd, TCSANOW, & newtio );
  47. Printf ("writing... \ n ");
  48.  
  49. While (1)
  50. {
  51. Res = read (fd, buf, 10 );
  52. Res = write (fd, buf, 7 );
  53. If (buf [0] = 9) break;
  54. }
  55.  
  56. Printf ("close... \ n ");
  57. Close (fd );
  58. /* Restore the old parameter */
  59. Tcsetattr (fd, TCSANOW, & oldtio );
  60. Return 0;
  61. }


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.

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.