Serial Port Programming under Linux

I. Linux The following serial port programming and those components

 

 

1. Open the serial port

2. Serial port initialization

3. Read or write serial ports

4. Close the serial port

2. Opening of Serial Port

Since the serial port is in the linux It is regarded as a file, so it should be opened before it is operated.

1. In Linxu, serial devices are accessed through serial terminal device files, that is, by accessing / dev/ttyS0,/dev/ttyS1,/dev/ttyS2.

2. Call the open() function to replace the serial device. For the open operation of the serial port, the O_NOCTTY parameter must be used.

O_NOCTTY: This means that a terminal device is opened and the program will not become the control terminal of the port. If this flag is not used, a task input (eg: keyboard stop signal, etc.) will affect the process.

O_NDELAY: Indicates that you do not care about the state of the DCD signa l line (whether the other end of the port is activated or stopped).

3. Opening the serial module has that and some components.

1 > Call the open() function to open the serial port and get the device file descriptor of the serial port

2 > Obtain the state of serial port and judge whether it is blocked or not

3>test Is the open file descriptor a terminal device?

 

 

 

 

 

 

Procedures:

/*****************************************************************

* Name: * UART0_Open

* Function: Open the serial port and return the description of the serial device file

* Entry parameter: fd File descriptor port: serial password (ttyS0,ttyS1,ttyS2)

* Export parameter: * Correct return to 1, error return to 0

*****************************************************************/

int UART0_Open(int fd,char* port)

{

 

      fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);

      if (FALSE == fd)

             {

                    perror("Can't Open Serial Port");

                    return(FASLE);

             }

// Determine whether the state of the serial port is blocked or not.

  if(fcntl(fd, F_SETFL, 0) < 0)

      {

             printf("fcntl failed!/n");

           return(FALSE);

      }     

  else

  {

       printf("fcntl=%d/n",fcntl(fd, F_SETFL,0));

  }

  //test Is it a terminal device?

  if(0 == isatty(STDIN_FILENO))

      {

             printf("standard input is not a terminal device/n");

        return(FALSE);

      }

  else

      {

           printf("isatty success!/n");

      }       

  printf("fd->open=%d/n",fd);

  return fd;

}

 

 

 

3. Initialization of Serial Port

 

1. Serial port initialization in linux is the same as the previous serial port initialization. Serial port baud rate, data flow control, frame format (i.e. number of data bits, stop bits, check bits, data flow control) need to be set.

2. The serial initialization module consists of those parts:

Setting baud rate

2 > Setting Data Flow Control

2 > Format the frame (i.e. number of data bits, stop bits, check bits)

Brother John explained:

1 > termios structure is used when setting serial port parameters, so the function must be used first.

Tcgettattr (fd, & options) obtains the pointer of serial port to termios structure.

2 > The input/output baud rate of serial port is set by cfsetispeed function and cfsetospeed function. In general, the input and output baud rates are equal.

3 > Setting data bits can be achieved by modifying c_flag in termios mechanism. CS5, CS6, CS7 and CS8 correspond to 5, 6, 7 and 8 data bits. When setting data bits, CSIZE must be used as bit shielding.

4 > What methods are used to mark the beginning and end of data transmission in data flow control?

5 > After setting baud rate, data flow control, data bits, check bits, stop bits, stop bits, minimum waiting time and minimum receiving characters are set.

6 > After the configuration is completed, the configuration is activated through the tcsetattr() function.

3. Procedures:

 

/*******************************************************************

* Name: * UART0_Set

* Function: Set up serial data bits, stop bits and validation bits

* Entry parameters: fd Serial Port File Descriptor

* speed

* flow_ctrl Data Flow Control

* Data bits. Data bits. Value 7 or 8

* Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits

* Values of parity type are N, E, O, S

* Export parameter: * Correct return to 1, error return to 0

*******************************************************************/

int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)

{

   

      int   i;

         int   status;

         int   speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,

          B38400, B19200, B9600, B4800, B2400, B1200, B300 };

     int   name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,      38400, 19200,  9600, 4800, 2400, 1200,  300 };

         

    struct termios options;

   

/* tcgetattr (fd, & options) obtains the parameters related to the FD pointing object and saves them in options. This function can also test whether the configuration is correct, whether the serial port is available, etc. If the call is successful, the return value of the function is 0, and if the call fails, the return value of the function is 1.

    */

    if  ( tcgetattr( fd,&options)  !=  0)

       {

          perror("SetupSerial 1");    

          return(FALSE); 

       }

  

// Setting Serial Port Input and Output Baud Rates

    for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)

                {

              if  (speed == name_arr[i])

              {       

                          cfsetispeed(&Options, speed_arr[i]); 

                          cfsetospeed(&Options, speed_arr[i]);  

              }

       }     

   

// Modify the control mode to ensure that the program does not occupy the serial port

    options.c_cflag |= CLOCAL;

// Modify the control mode so that input data can be read from serial port

    options.c_cflag |= CREAD;

  

// Setting up data flow control

    switch(flow_ctrl)

    {

      

case 0: // No Flow Control

              options.c_cflag &= ~CRTSCTS;

              break;   

      

case 1: // Use Hardware Flow Control

              options.c_cflag |= CRTSCTS;

              break;

case 2: // Using Software Flow Control

              options.c_cflag |= IXON | IXOFF | IXANY;

              break;

    }

// Setting data bits

Options.c_cflag &=~CSIZE; //Shield other flags

    switch (databits)

    {  

       case 5    :

                     options.c_cflag |= CS5;

                     break;

       case 6    :

                     options.c_cflag |= CS6;

                     break;

       case 7    :    

                 options.c_cflag |= CS7;

                 break;

       case 8:    

                 options.c_cflag |= CS8;

                 break;  

       default:   

                 fprintf(stderr,"Unsupported data size/n");

                 return (FALSE); 

    }

// Setting Check Bit

    switch (parity)

    {  

       case 'n':

Case'N': // No parity bit.

                 options.c_cflag &= ~PARENB; 

                 options.c_iflag &= ~INPCK;    

                 break; 

       case 'o':  

Case'O': // Set to Odd Check

                 options.c_cflag |= (PARODD | PARENB); 

                 options.c_iflag |= INPCK;             

                 break; 

       case 'e': 

Case'E': // Set to Dual Check

                 options.c_cflag |= PARENB;       

                 options.c_cflag &= ~PARODD;       

                 options.c_iflag |= INPCK;       

                 break;

       case 's':

Case'S': // Set to Space

                 options.c_cflag &= ~PARENB;

                 options.c_cflag &= ~CSTOPB;

                 break; 

        default:  

                 fprintf(stderr,"Unsupported parity/n");   

                 return (FALSE); 

    } 

// Setting stop bits

    switch (stopbits)

    {  

       case 1:   

                 options.c_cflag &= ~CSTOPB; 

                 break; 

       case 2:   

                 options.c_cflag |= CSTOPB; 

                         break;

       default:   

                       fprintf(stderr,"Unsupported stop bits/n"); 

                       return (FALSE);

    }

   

// Modify the output mode to output raw data

    options.c_oflag &= ~OPOST;

   

// Set waiting time and minimum receive character

options.c_cc[VTIME] = 1; /* Read a character and wait for 1*(1/10)s*/

options.c_cc[VMIN] = 1; /* The minimum number of characters read is 1*/

   

// If a data overflow occurs, the data is received, but no longer read.

    tcflush(fd,TCIFLUSH);

   

// Activation configuration (set the modified termios data to the serial port)

    if (tcsetattr(fd,TCSANOW,&options) != 0)  

    {

               perror("com set error!/n");  

       return (FALSE); 

    }

    return (TRUE); 

}

/*******************************************************************

* Name: * UART0_Init()

* Function: Serial Initialization

* Entry parameters: fd file descriptor

* speed

* flow_ctrl Data Flow Control

* Data bits. Data bits. Value 7 or 8

* Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits

* Values of parity test type are N, E, O, S.

* Export parameters:) Correct return to 1, error return to 0

*******************************************************************/

int UART0_Init(int fd, int speed,int flow_ctrlint databits,int stopbits,int parity)

{

    int err;

// Setting Serial Data Frame Format

    if (UART0_Set(fd,115200,0,8,1,'N') == FALSE)

    {                                                     

        return FALSE;

    }

    else

    {

               return  TRUE;

        }

}

 

 

 

 

Note:

If it's not a development terminal or something like that, but a serial port to transmit data, without the need for serial port to process, then use Raw Mode mode to communicate, the settings are as follows:

 

options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/

options.c_oflag  &= ~OPOST;   /*Output*/

 

 

 

4. Read-write function of serial port:

1. Read-write serial port is realized by using read function and write function.

2. Procedures

 

/*******************************************************************

* Name: * UART0_Recv

* Function: Receiving Serial Data

* Entry parameter: fd File descriptor

* rcv_buf: Data in receiving serial port is stored in rcv_buf buffer

* data_len: The length of a frame of data

* Export parameters:) Correct return to 1, error return to 0

*******************************************************************/

int UART0_Recv(int fd, char *rcv_buf,int data_len)

{

    int len,fs_sel;

    fd_set fs_read;

   

    struct timeval time;

   

    FD_ZERO(&fs_read);

    FD_SET(fd,&fs_read);

   

    time.tv_sec = 10;

    time.tv_usec = 0;

   

// Using select to realize multi-channel communication of serial port

    fs_sel = select(fd+1,&fs_read,NULL,NULL,&time);

    if(fs_sel)

       {

              len = read(fd,data,data_len);

              return len;

       }

    else

       {

              return FALSE;

       }     

}

/*******************************************************************

* Name: UART0_Send

* Function: Send data

* Entry parameter: fd File descriptor

* send_buf: Store serial port to send data

* data_len: Number of data in a frame

* Export parameters:) Correct return to 1, error return to 0

*******************************************************************/

int UART0_Send(int fd, char *send_buf,int data_len)

{

    int len = 0;

   

    len = write(fd,send_buf,data_len);

    if (len == data_len )

       {

              return len;

       }     

    else   

        {

               

                tcflush(fd,TCOFLUSH);

                return FALSE;

        }

    }

 

V. Close the serial port

After completing the operation on the serial device, the close function is called to close the file descriptor.

Procedures:

 

/******************************************************

* Name: * UART0_Close

* Function: Close the serial port and return the description of the serial device file

* Entry parameter: [fd]: File descriptor

* Export parameters: void

*******************************************************************/

 

void UART0_Close(int fd)

{

    close(fd);

}

A complete procedure

  1. /************************Copyright(c)******************************* 
  2. **                       Xi'an University of Posts and Telecommunications 
  3. **                       graduate school 
  4. **                                     XNMS project team 
  5. **                       WebSite :blog.csdn.net/tigerjb 
  6. **------------------------------------------FileInfo------------------------------------------------------- 
  7. ** File name:                 main.c 
  8. ** Last modified Date:  2011-01-31 
  9. ** Last Version:              1.0 
  10. ** Descriptions:             
  11. **------------------------------------------------------------------------------------------------------ 
  12. ** Created by:               Ji Bo 
  13. ** Created date:            2011-01-31 
  14. ** Version:                            1.0 
  15. ** Descriptions:             The original version 
  16. **------------------------------------------------------------------------------------------------------ 
  17. ** Modified by: 
  18. ** Modified date: 
  19. ** Version: 
  20. ** Descriptions: 
  21. *******************************************************************/  
  22.    
  23.    
  24. //Header files related to serial ports  
  25. #Include <stdio.h> /* Standard I/O Definition*/  
  26. #Include <stdlib.h> /* Standard Function Library Definition*/  
  27. #Include <unistd.h> /*Unix Standard Function Definition*/  
  28. #include<sys/types.h>   
  29. #include<sys/stat.h>     
  30. #Include <fcntl.h> /* File Control Definition*/  
  31. #Include <termios.h> /*PPSIX Terminal Control Definition*/  
  32. #Include <errno.h> /* Error Number Definition*/  
  33. #include<string.h>  
  34.    
  35.    
  36. //Macro Definition  
  37. #define FALSE  -1  
  38. #define TRUE   0  
  39.    
  40. /******************************************************************* 
  41. * Name: * UART0_Open 
  42. * Function: Open the serial port and return the description of the serial device file 
  43. * Entry parameter: fd File descriptor port: serial password (ttyS0,ttyS1,ttyS2) 
  44. * Export parameters:) Correct return to 1, error return to 0 
  45. *******************************************************************/  
  46. int UART0_Open(int fd,char* port)  
  47. {  
  48.      
  49.          fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);  
  50.          if (FALSE == fd)  
  51.                 {  
  52.                        perror("Can't Open Serial Port");  
  53.                        return(FALSE);  
  54.                 }  
  55.      //Restore the serial port to blocked state.  
  56.      if(fcntl(fd, F_SETFL, 0) < 0)  
  57.                 {  
  58.                        printf("fcntl failed!\n");  
  59.                      return(FALSE);  
  60.                 }       
  61.          else  
  62.                 {  
  63.                   printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));  
  64.                 }  
  65.       //Test whether it is a terminal device or not.  
  66.       if(0 == isatty(STDIN_FILENO))  
  67.                 {  
  68.                        printf("standard input is not a terminal device\n");  
  69.                   return(FALSE);  
  70.                 }  
  71.   else  
  72.                 {  
  73.                      printf("isatty success!\n");  
  74.                 }                
  75.   printf("fd->open=%d\n",fd);  
  76.   return fd;  
  77. }  
  78. /******************************************************************* 
  79. * Name: * UART0_Close 
  80. * Function: Close the serial port and return the description of the serial device file 
  81. * Entry parameter: fd File descriptor port: serial password (ttyS0,ttyS1,ttyS2) 
  82. * Export parameters: void 
  83. *******************************************************************/  
  84.    
  85. void UART0_Close(int fd)  
  86. {  
  87.     close(fd);  
  88. }  
  89.    
  90. /******************************************************************* 
  91. * Name: * UART0_Set 
  92. * Function: Set up serial data bits, stop bits and validation bits 
  93. * Entry parameters: fd Serial port file descriptor 
  94. *                              speed     Serial port speed 
  95. *                              flow_ctrl   Data flow control 
  96. *                           databits   Data Bit: Value 7 or 8 
  97. *                           stopbits   Stop Bit: Value 1 or 2 
  98. *                           parity     Value of validation type N, E, O, S 
  99. *Export parameters:) Correct return to 1, error return to 0 
  100. *******************************************************************/  
  101. int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)  
  102. {  
  103.      
  104.       int   i;  
  105.          int   status;  
  106.          int   speed_arr[] = { B115200, B19200, B9600, B4800, B2400, B1200, B300};  
  107.      int   name_arr[] = {115200,  19200,  9600,  4800,  2400,  1200,  300};  
  108.            
  109.     struct termios options;  
  110.      
  111.     /*tcgetattr(fd,&options)The function can also test whether the configuration is correct, whether the serial port is available and so on. If the call is successful, the return value of the function is 0, and if the call fails, the return value of the function is 1. 
  112.     */  
  113.     if  ( tcgetattr( fd,&options)  !=  0)  
  114.        {  
  115.           perror("SetupSerial 1");      
  116.           return(FALSE);   
  117.        }  
  118.     
  119.     //Setting Serial Port Input and Output Baud Rates  
  120.     for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)  
  121.                 {  
  122.                      if  (speed == name_arr[i])  
  123.                             {               
  124.                                  cfsetispeed(&options, speed_arr[i]);   
  125.                                  cfsetospeed(&options, speed_arr[i]);    
  126.                             }  
  127.               }       
  128.      
  129.     //Modify the control mode to ensure that the program does not occupy the serial port  
  130.     options.c_cflag |= CLOCAL;  
  131.     //Modify the control mode so that input data can be read from serial port  
  132.     options.c_cflag |= CREAD;  
  133.     
  134.     //Setting up data flow control  
  135.     switch(flow_ctrl)  
  136.     {  
  137.         
  138.        case 0 ://No flow control  
  139.               options.c_cflag &= ~CRTSCTS;  
  140.               break;     
  141.         
  142.        case 1 ://Using Hardware Flow Control  
  143.               options.c_cflag |= CRTSCTS;  
  144.               break;  
  145.        case 2 ://Using Software Flow Control  
  146.               options.c_cflag |= IXON | IXOFF | IXANY;  
  147.               break;  
  148.     }  
  149.     //set data bit  
  150.     //Shielding other markers  
  151.     options.c_cflag &= ~CSIZE;  
  152.     switch (databits)  
  153.     {    
  154.        case 5    :  
  155.                      options.c_cflag |= CS5;  
  156.                      break;  
  157.        case 6    :  
  158.                      options.c_cflag |= CS6;  
  159.                      break;  
  160.        case 7    :      
  161.                  options.c_cflag |= CS7;  
  162.                  break;  
  163.        case 8:      
  164.                  options.c_cflag |= CS8;  
  165.                  break;    
  166.        default:     
  167.                  fprintf(stderr,"Unsupported data size\n");  
  168.                  return (FALSE);   
  169.     }  
  170.     //Setting Check Bit  
  171.     switch (parity)  
  172.     {    
  173.        case 'n':  
  174.        case 'N'//No parity bits.  
  175.                  options.c_cflag &= ~PARENB;   
  176.                  options.c_iflag &= ~INPCK;      
  177.                  break;   
  178.        case 'o':    
  179.        case 'O'://Set it to odd check.  
  180.                  options.c_cflag |= (PARODD | PARENB);   
  181.                  options.c_iflag |= INPCK;               
  182.                  break;   
  183.        case 'e':   
  184.        case 'E'://Set to Dual Check  
  185.                  options.c_cflag |= PARENB;         
  186.                  options.c_cflag &= ~PARODD;         
  187.                  options.c_iflag |= INPCK;        
  188.                  break;  
  189.        case 's':  
  190.        case 'S'//Set to blank  
  191.                  options.c_cflag &= ~PARENB;  
  192.                  options.c_cflag &= ~CSTOPB;  
  193.                  break;   
  194.         default:    
  195.                  fprintf(stderr,"Unsupported parity\n");      
  196.                  return (FALSE);   
  197.     }   
  198.     //Setting stop bits  
  199.     switch (stopbits)  
  200.     {    
  201.        case 1:     
  202.                  options.c_cflag &= ~CSTOPB; break;   
  203.        case 2:     
  204.                  options.c_cflag |= CSTOPB; break;  
  205.        default:     
  206.                        fprintf(stderr,"Unsupported stop bits\n");   
  207.                        return (FALSE);  
  208.     }  
  209.      
  210.   //Modify the output mode to output raw data  
  211.   options.c_oflag &= ~OPOST;  
  212.     
  213.   options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);//I added  
  214. //options.c_lflag &= ~(ISIG | ICANON);  
  215.      
  216.     //Set waiting time and minimum receive character  
  217.     options.c_cc[VTIME] = 1; /* Read a character and wait for 1*(1/10)s*/    
  218.     options.c_cc[VMIN] = 1; /* The minimum number of characters read is 1.*/  
  219.      
  220.     //If data overflow occurs, receive data, but no longer read, refresh received data, but do not read  
  221.     tcflush(fd,TCIFLUSH);  
  222.      
  223.     //Activation configuration (set the modified termios data to the serial port)  
  224.     if (tcsetattr(fd,TCSANOW,&options) != 0)    
  225.            {  
  226.                perror("com set error!\n");    
  227.               return (FALSE);   
  228.            }  
  229.     return (TRUE);   
  230. }  
  231. /******************************************************************* 
  232. * Name: * UART0_Init() 
  233. * Function: Serial Initialization 
  234. * Entry parameter: fd File descriptor 
  235. *               speed  :  Serial port speed 
  236. *                              flow_ctrl  Data flow control 
  237. *               databits   Data Bit: Value 7 or 8 
  238. *                           stopbits   Stop Bit: Value 1 or 2 
  239. *                           parity     Value of validation type N, E, O, S 
  240. *                       
  241. * Export parameters:) Correct return to 1, error return to 0 
  242. *******************************************************************/  
  243. int UART0_Init(int fd, int speed,int flow_ctrl,int databits,int stopbits,int parity)  
  244. {  
  245.     int err;  
  246.     //Setting Serial Data Frame Format  
  247.     if (UART0_Set(fd,19200,0,8,1,'N') == FALSE)  
  248.        {                                                           
  249.         return FALSE;  
  250.        }  
  251.     else  
  252.        {  
  253.                return  TRUE;  
  254.         }  
  255. }  
  256.    
  257. /******************************************************************* 
  258. * Name: * UART0_Recv 
  259. * Function: Receiving Serial Data 
  260. * Entry parameter: fd File descriptor 
  261. *                              rcv_buf     :Data stored in rcv_buf buffer in receiving serial port 
  262. *                              data_len    :Length of a frame of data 
  263. * Export parameters:) Correct return to 1, error return to 0 
  264. *******************************************************************/  
  265. int UART0_Recv(int fd, char *rcv_buf,int data_len)  
  266. {  
  267.     int len,fs_sel;  
  268.     fd_set fs_read;  
  269.      
  270.     struct timeval time;  
  271.      
  272.     FD_ZERO(&fs_read);  
  273.     FD_SET(fd,&fs_read);  
  274.      
  275.     time.tv_sec = 10;  
  276.     time.tv_usec = 0;  
  277.      
  278.     //Using select to realize multi-channel communication of serial port  
  279.     fs_sel = select(fd+1,&fs_read,NULL,NULL,&time);  
  280.     if(fs_sel)  
  281.        {  
  282.               len = read(fd,rcv_buf,data_len);  
  283.           printf("I am right!(version1.2) len = %d fs_sel = %d\n",len,fs_sel);  
  284.               return len;  
  285.        }  
  286.     else  
  287.        {  
  288.           printf("Sorry,I am wrong!");  
  289.               return FALSE;  
  290.        }       
  291. }  
  292. /******************************************************************** 
  293. * Name: UART0_Send 
  294. * Function: Send data 
  295. * Entry parameter: fd File descriptor 
  296. *                              send_buf    :Store serial port to send data 
  297. *                              data_len    :Number of data per frame 
  298. * Export parameters:) Correct return to 1, error return to 0 
  299. *******************************************************************/  
  300. int UART0_Send(int fd, char *send_buf,int data_len)  
  301. {  
  302.     int len = 0;  
  303.      
  304.     len = write(fd,send_buf,data_len);  
  305.     if (len == data_len )  
  306.               {  
  307.                      return len;  
  308.               }       
  309.     else     
  310.         {  
  311.                  
  312.                 tcflush(fd,TCOFLUSH);  
  313.                 return FALSE;  
  314.         }  
  315.      
  316. }  
  317.    
  318.    
  319. int main(int argc, char **argv)  
  320. {  
  321.     int fd;                            //File descriptor  
  322.     int err;                           //Returns the state of the calling function  
  323.     int len;                          
  324.     int i;  
  325.     char rcv_buf[100];         
  326.     char send_buf[20]="tiger john";  
  327.     if(argc != 3)  
  328.        {  
  329.               printf("Usage: %s /dev/ttySn 0(send data)/1 (receive data) \n",argv[0]);  
  330.               return FALSE;  
  331.        }  
  332.     fd = UART0_Open(fd,argv[1]); //Open the serial port and return the file descriptor  
  333.     do{  
  334.                   err = UART0_Init(fd,19200,0,8,1,'N');  
  335.                   printf("Set Port Exactly!\n");  
  336.        }while(FALSE == err || FALSE == fd);  
  337.      
  338.     if(0 == strcmp(argv[2],"0"))  
  339.            {  
  340.                   for(i = 0;i < 10;i++)  
  341.                          {  
  342.                                 len = UART0_Send(fd,send_buf,10);  
  343.                                 if(len > 0)  
  344.                                        printf(" %d send data successful\n",i);  
  345.                                 else  
  346.                                        printf("send data failed!\n");  
  347.                             
  348.                                 sleep(2);  
  349.                          }  
  350.                   UART0_Close(fd);               
  351.            }  
  352.     else  
  353.            {  
  354.                                         
  355.            while (1) //Loop read data  
  356.                   {    
  357.                      len = UART0_Recv(fd, rcv_buf,9);  
  358.                      if(len > 0)  
  359.                             {  
  360.                        rcv_buf[len] = '\0';  
  361.                                    printf("receive data is %s\n",rcv_buf);  
  362.                        printf("len = %d\n",len);  
  363.                             }  
  364.                      else  
  365.                             {  
  366.                                    printf("cannot receive data\n");  
  367.                             }  
  368.                      sleep(2);  
  369.               }              
  370.        UART0_Close(fd);   
  371.            }  
  372. }  
  373.     
  374. /*********************************************************************                            End Of File                          ** 
  375. *******************************************************************/  

Keywords: Linux Programming Unix

Added by The Bat on Fri, 21 Jun 2019 00:00:02 +0300