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 ...
- 04-06-2010 #1Just 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:
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: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 } }
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.
- 04-07-2010 #2Linux Guru
- 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!
- 04-07-2010 #3Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
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.
Best regards.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); } }
- 04-07-2010 #4Linux Guru
- 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!
- 04-07-2010 #5
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.
- 04-07-2010 #6Linux Guru
- 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!
- 04-07-2010 #7
- 04-07-2010 #8Just 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
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 thatCode: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); }
.
Best Regards
- 04-07-2010 #9Linux Guru
- 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!
- 04-07-2010 #10Just 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


Reply With Quote
