LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices

Reply
 
LinkBack Search this Thread
Old 10-27-2012, 04:50 AM   #1
cdr_nitjsr@yahoo.com
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
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.

Last edited by cdr_nitjsr@yahoo.com; 10-27-2012 at 04:54 AM.
 
Old 11-02-2012, 11:52 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,388
Blog Entries: 2

Rep: Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900Reputation: 900
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.
 
Old 11-03-2012, 12:47 AM   #3
cdr_nitjsr@yahoo.com
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by theNbomr View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
writing to /reading from serial port abd_bela Programming 1 07-26-2012 09:09 AM
Serial port reading jayadhanesh Linux - Newbie 3 05-19-2009 06:54 PM
Reading and Writing to a Serial Port without SU Steef223 Programming 2 06-16-2008 09:16 AM
reading from serial port IC009562 Linux - Software 2 11-08-2007 11:25 PM
Having trouble reading from serial port glo Linux - Newbie 0 02-09-2007 10:42 AM


All times are GMT -5. The time now is 07:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration