LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (http://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   Reading serial Port using select api (http://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/reading-serial-port-using-select-api-4175434328/)

cdr_nitjsr@yahoo.com 10-27-2012 04:50 AM

Reading serial Port using select api
 
I am reading data using two no of serial ports. My data is nothing but count down time generated by a clock generator. After Every one second the new data arrives.

Code:

int main(int argc, char **argv)
{     
        fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY); 
                if (fd1 == -1)
                {
                perror("open_port: Unable to open /dev/ttyS1 - ");
                return 0;
                }
            else
                {
                x=fcntl(fd1,F_GETFL,0);
                fcntl(fd1, F_SETFL, 0);         
                }         
                initport(fd1, 9600);
                printf("baud=%d\n", getbaud(fd1));

             
        fd2 = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
                if (fd2 == -1)
                  {
                  perror("open_port: Unable to open /dev/ttyS2 - ");
                  return 0;
                  }
                else
                  {
                  y=fcntl(fd2,F_GETFL,0);
                  fcntl(fd2, F_SETFL, 0);                   
                  }                       
                  initport(fd2, 9600);   
                  printf("baud=%d\n", getbaud(fd2)); 
           
    while(1)
        {   
             
                FD_ZERO(&input);   
                FD_SET(fd1, &input); 
                FD_SET(fd2, &input); 
                max_fd = (fd2 > fd1 ? fd2 : fd1) + 1;               
                timeout.tv_sec = 1;
                timeout.tv_usec = 0;
               
n = select(max_fd, &input, NULL, NULL, &timeout);             
                if (n < 0)
                  perror("select failed");
                else if (n == 0)
                  {
                  puts("TIMEOUT");                   
                  }
                else
                {
                               
                 
          if (FD_ISSET(fd1, &input))   
                  {                     
                     
              if(readport(fd1,serial1 )) 
                    {
                    printf("read  Failed\n");
                    exit(0);
                    }
                    else
                      {
                      if(serial1[0]==1)
                      printf(" \n the serial 1 data %s",serial1);
                               
                      }                             
                    }
                   
                   
                                       
  if(FD_ISSET(fd2, &input)) 
                    {                 
                       
                  if(readport(fd2,serial2 ))
                      {
                        printf("read  Failed\n");
                        exit(0);
                      }
                      else
                        {
                        if(serial2[0]==1)
                        printf(" \n the serial 2 data %s",serial2);
                               
                        }                       
                    }                 

                 
      }//else  of select   
  }//while

close(fd1);
close(fd2);
 
}//main

Both of serial ports receive the same data like 01:05:11 which will be counting down.
The code works fine but when i remove input from one serial port and again put it back then it should get the time which is comming.But both serial port are not reading data simultaneously but the are reading in alternate ie if one serial port reads 01:05:11 the other reads 00:00:00 and vice versa. I want that both ports should read simultaneously.

another problem is that when i am removing one serial port the removal of serial port from fd_set is not reflecting immediately but after 20-30 seconds. e.g if two fds are there and i remove one input the only one fd should be there but it shows two fds for some time and after 20-30 second it show only one fd


Please tell where is problem in the code.

theNbomr 11-02-2012 11:52 AM

Your interesting indentation style makes your code virtually unreadable. I suggest that you acquaint yourself with the indent command.
I do note a few things:
  • You haven't shown the code that actually reads the serial data, nor the code that shows how the buffers that allegedly contain the serial data are defined.
  • You seem to test the input buffer for an integer value '1' in the first element, and then print the array in string context. That part seems very strange to me. It is impossible to divine the significance of the integer '1' in this context.
  • You don't show any code where the 'input' fd_set types used in the select() & related FD_xxx() calls is created.
--- rod.

cdr_nitjsr@yahoo.com 11-03-2012 12:47 AM

Quote:

Originally Posted by theNbomr (Post 4820744)
Your interesting indentation style makes your code virtually unreadable. I suggest that you acquaint yourself with the indent command.
I do note a few things:
  • You haven't shown the code that actually reads the serial data, nor the code that shows how the buffers that allegedly contain the serial data are defined.
  • You seem to test the input buffer for an integer value '1' in the first element, and then print the array in string context. That part seems very strange to me. It is impossible to divine the significance of the integer '1' in this context.
  • You don't show any code where the 'input' fd_set types used in the select() & related FD_xxx() calls is created.
--- rod.



You seem to test the input buffer for an integer value '1' in the first element, and then print the array in string context. That part seems very strange to me. It is


Here i am printing the serila data at every one sec interval. Because every new data is havinh SOH(Start of herader) whose integer value is 1. Thats why to reducing the printing frequency i am checking SOH


All times are GMT -5. The time now is 05:36 AM.