On Tuesday, May 6, 2014 at 1:42:34 PM UTC-5, Andrey wrote:
>
> Hello,
>
> I need to use an UART to communicate between beaglebone and the device. I 
> have written the C code, but it has a few problems within it. I can't 
> identify the cause of the problem or to be more precise don't know how fix 
> it.  
>
> The problem is of 
> reading in the information and displaying it on a shell. I can easily 
> write any information data set that I wish, but when I try to read in 
> response it either doesn't read in, with the perror being displayed as 
> (erno = EINTR [Interrupted function call]) meaning read () function 
> returned negative number. 
>  
>
>     fd = open(PORT, O_RDWR | O_NOCTTY | O_NONBLOCK);
>
>     if (fd<0) {perror(PORT); exit(-1);}
>
>  
>

I think Simon's post will get you on the right track.  You are sending a 
command over the UART and expecting a response back, you pretty much have 
to block or read in a loop to give the external device time to reply.

I open the port (O_RDWR | O_NOCTTY | O_NDELAY) and use 
options.c_cc[VMIN]  = 0; // wait for each character
options.c_cc[VTIME] = 10; // wait time in 1/10s of a second

Which will time out after 1 second without response (a true error for my 
device, or a signal that the response string is complete)


Here is a code snippet of the function I use to check the signal strength 
of a cell phone modem:

int check_signal(int fd){
char buffer[256];
char *bufptr;
int bytes, n=0, nbytes=0;
char tstr[64];
if (write(fd, "AT+CSQ\r", 7) <7){
printf("%s *** ERROR! writing AT+CSQ command! ***\n",getDateTime(tstr));
FONA_DEAD=1; // send text message that FONA backup messaging system not 
working
    return(-1);
}else{
    while ((bytes = read(fd, &buffer[nbytes], sizeof(buffer)-nbytes-1)) > 
0){ nbytes += bytes; }
            buffer[nbytes] = '\0'; // nul terminate the string
    if(nbytes>0){
        if(bufptr=strstr(buffer,"+CSQ:")){
    sscanf(bufptr,"+CSQ: %i", &n);
    //if(n==0) printf("sscanf fail: %s\n", buffer);  // actually was 
intermittently 0 signal strength with some antenna positions!
    //printf("Signal Strength: %i\n",n);
    return(n);
        }else{
    // getting occasional failures, try to tell if its time outs or 
unexpected characters that weren't flushed after previous function
    // if I don't see this message from the errors increase time out a bit
    printf("%s *** check_signal() Unexpected Response! 
***\n%s\n",getDateTime(tstr),buffer);
    return(-2);
        }
    }
    }
}



It sends a multi-byte command string and then waits in a loop for a 
multi-byte response than ends with a timeout after the last character is 
received.  If you always get a fixed length
reply you can make the code more responsive by exiting after the last 
character comes in, instead of waiting for the timeout. 

I generally find it easier to put blocking code in separate threads than to 
try to do non-blocking read/write in a main loop.  YMMV.

The suggestion below to use select() is also a good option.

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/8d4fc307-ef27-4137-bbb2-3fb5feb01bb8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to