Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hi, i'm porting an application in C to ARM Linux, the problem is that i found some weird code and i'm thinking in a workaround, but i would like some ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    4

    Serial communication problems in ARM Linux

    Hi, i'm porting an application in C to ARM Linux, the problem is that i found some weird code and i'm thinking in a workaround, but i would like some advices since i'm not an expert in Linux so here is the piece of code:

    Code:
    while(read(hRS232Port,&resp[i],1)>0)
    {	
    	if(resp!="\x0")
    	{		
    		copy[j]= resp[i];
    		j++;
    		printf("%d,", resp[i]);			
                    msleep(60); //sleep in milliseconds			
    	}
    }
    The problem is that this particular piece of code blocks the whole application and nothing can take'em out of this loop. I understand what is trying to do, but i got some doubts:

    a) the read() is a blocking call?
    b) i was thinking of a possible workaround using just the read function but increasing the number of bytes to read; this could work?
    c) As far as i know the condition of returning zero is in succesful reading the amount of bytes indicated and reaching the EOF. but in serial how can i reach this condition?

    Thanks in advance.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Please show the entire function this is being used in. There are a number of potential bugs here that can cause a busy loop which can suck up all your processor cycles.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2010
    Posts
    4

    Post

    Hi, i've founded a partial workaround changing the while loop with a do-while (the application does not block anymore), but still persists the problem of breaking the loop, since i don't know the lenght of the string received.

    Code:
    void vdgetInfo()
    {
    	char cmd[6];
    	char resp[100+1];   	
            char copy[100];
    	int rcvd = 0;
    	char dummy;
    	
    	//DATA COMMAND
    	memset(cmd,'\x0',sizeof(cmd));
    	memset(resp,'\x0',sizeof(resp));
    	memset(copy,'\x0',sizeof(copy));
    	strcpy(cmd,"\x87");
    	strcat(cmd,"482");   
    	strcat(cmd,"\x88");
    	
    	
    	//hRS232Port = inInitializeSerialPort("/dev/ttySAC0");
    	
    	if (hRS232Port > 0)//Already initialized
    	{
    		
    		rcvd = write(hRS232Port,cmd,strlen(cmd));
    		printf("Bytes written: %d\n", rcvd);
    		read(hRS232Port,&dummy,1);//Read ACK
    		printf("Response: %d", dummy);
    	        msleep(50);
                    rcvd = read(hRS232Port, resp, sizeof(resp));
                    do
    		{
    			rcvd= read(hRS232Port,&resp[i],1);
    			if(resp != "\x0")
    			{
    				copy[j]= resp[i];
    				j++;
    				printf("%d,", resp[i]);
    				msleep(60);
    			}
    		}while ( rcvd != 0);
    		printf("\n%s\n", resp);		
    		msleep(50);
    	}
    	else//Error on the port handler
    	{
    		printf("ERROR\n");
    		msleep(5000);
    	}
    }
    Best regards.

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I presume you didn't write this code?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Linux Newbie theNbomr's Avatar
    Join Date
    May 2007
    Location
    BC Canada
    Posts
    150
    The usual method to deal with checking for asynchronously received data in a loop is to use the select() function. When correctly configured, it will allow you to test a group of input streams, and only call read() on each one when it has data to provide.
    ---- rod.
    Stuff happens. Then stays happened.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Please show the code where the port is opened.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by theNbomr View Post
    The usual method to deal with checking for asynchronously received data in a loop is to use the select() function. When correctly configured, it will allow you to test a group of input streams, and only call read() on each one when it has data to provide.
    ---- rod.
    Generally true, but there are times when you don't want to use select(). I'm not making any judgements until I get a better idea of what is going on here.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Apr 2010
    Posts
    4
    That's right this isn't my code, this code gave me a lot of headaches
    The structures showed up here are device specific and privative functions/libraries, that's the reason i can't use termios.h

    Code:
    int inInitializeSerialPort(char* szPort)
    {
    	if((hRS232Port = open(szPort, O_RDWR)) < 0)
    		{
    			printf("Error cannot open %s errno: %d\n", szPort, errno);	
    			return -1;
    		}
    
    	stConfig.rate = 19200;
    	stConfig.format = cfg_A8N1 | cfg_2stp | cfg_DTR;
    	stConfig.protocol = Char_mode;
    	if(SetOpenBlock(hRS232Port,&stConfig) < 0) 
    			return -1;
    	printf("RS232 Port opened with no errors, handle: %d\n", hRS232Port);
    	return(hRS232Port);
    }
    All Rx/Tx in the serial port, when the amount of expected data is known works great. Also i tried to do a Flush function changing to non block with fcntl function but it doesn't work i'm investigating if it's mandatory that sleep for 60ms because the given documentation doesn't specify that .

    Best Regards

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Do you know if there is anywhere you are setting the port for non-blocking access?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Just Joined!
    Join Date
    Apr 2010
    Posts
    4
    AFAIK the default is blocking, and as i said i tried to change it with the fcntl function but it doesn't work, the fcntl gave no errors but the function was still blocking

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...