Society of Robots - Robot Forum

Software => Software => Topic started by: klims on November 16, 2010, 03:21:26 PM

Title: Webbotlib uartGetByte(UART) difference to Axon Source Code
Post by: klims on November 16, 2010, 03:21:26 PM
Hey guys,

I have had some strange behaviour when using the webbotlib and uartGetGyte(UART) with a buffer. Check out the code below:
Code: [Select]
if(!uartReceiveBufferIsEmpty(blueUART))
{
rprintf("i:%d ", i);
remoteCommand[i] = uartGetByte(blueUART);

if(remoteCommand[i] != -1)
{
rprintf("i:%d ", i);
while( (remoteCommand[i] != -1) )
{
rprintf("i:%d ", i);
i++;
remoteCommand[i] = uartGetByte(blueUART);
//remoteCommand[i] = uart1GetByte();
}
rprintf("i:%d ", i);
}

else
uartFlushReceiveBuffer(blueUART);

rprintf("i:%d ", i);
}
I found that my program would get stuck in an endless while loop because uartGetByte(UART) will never return -1.

After a while of stuffing around I came up with this working bit of code:
Code: [Select]
if(!uartReceiveBufferIsEmpty(blueUART))
{
rprintf("i:%d ", i);
remoteCommand[i] = uartGetByte(blueUART);

if(remoteCommand[i] != -1)
{
rprintf("i:%d ", i);
while( !uartReceiveBufferIsEmpty(blueUART) )
{
rprintf("i:%d ", i);
i++;
remoteCommand[i] = uartGetByte(blueUART);
//remoteCommand[i] = uart1GetByte();
}
rprintf("i:%d ", i);
}

else
uartFlushReceiveBuffer(blueUART);

rprintf("i:%d ", i);
}

As you can see I tried both uartGetByte(UART) and uart1GetByte() and neither returns -1. Has anyone else had this problem?
Title: Re: Webbotlib uartGetByte(UART) bug?
Post by: Webbot on November 17, 2010, 04:39:49 AM
I'm guessing your problem is that you are storing the result of uartGetByte straight into the remoteCommand arrary. You are then testing the value from the array rather than the return from uartGetByte. eg if 'remoteCommand' is defined as an array of 'uint8_t' or 'char' then thats the problem as they are both unsigned. So if uartGetByte returns -1 then you are storing 0xFF in the remoteCommand array. This will never equal -1 as its now an unsigned number ie 255.

So I would change the code to check the return value from uartGetByte:-
Code: [Select]
int nextByte = uartGetByte(blueUART);
while(nextByte != -1){
    remoteCommand[i] = nextByte;
    rprintf("i:%d ",i);
    i++;
    nextByte = uartGetByte(blueUART);
}
Title: Re: Webbotlib uartGetByte(UART) bug?
Post by: klims on November 18, 2010, 03:25:20 AM
Right as usual Webbot  :)

I had taken my bit of code from something written using Admin's original libraries a while back. I'll try to change the thread description to match.


thanks guys