Received UART data
Wilts
Posts: 8
I seem to be having a few problems with the receiveByte() method. I am trying to trap the case when I get 0xFF from a serial terminal connected to the UART port. The trouble is that for values less that 0x7F, I receive what I transmit. However, for values greater than this (eg 0x80), I seem to get a value with an extra FF - eg 0xFF80. This is strange since
a) I have tried to cast the received byte to a char first and
b) Why do I not get the extra FF when I use values less than 0x7F?
I am probably doing something very silly and hence I have included the code below for you to point out my obvious mistake! The Get_Reply method is the problem - it never seems to get out of the loop.
Thanks a lot
Paul
final static char END_OF_STRING = 0xFF ;// end of command/inquiry/reply byte
static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
Uart.speed9600,
Uart.stop1 );
/**
* Get_Reply
*
* Gets result of an equiry or command
* result stored in RXBuffer
*/
void Get_Reply()
{
char crc = 0x00; // clear c to zero before check
RXbuffer.clear();
while (crc != END_OF_STRING)
{
if(rxUart.byteAvailable())
{
crc = (char)rxUart.receiveByte();
RXbuffer.append(crc);
if(DEBUG) //bring out RX buffer in Debug mode
{
Format.printf("%x, ",crc);
}
}
if (crc == END_OF_STRING)
{
System.out.println("Well I got the end!!"); //This point never seems to be got to
}
}
if(DEBUG) //put new line after RX buffer in Debug mode
{
System.out.println("");
}
return;
} // end of Get_Reply
a) I have tried to cast the received byte to a char first and
b) Why do I not get the extra FF when I use values less than 0x7F?
I am probably doing something very silly and hence I have included the code below for you to point out my obvious mistake! The Get_Reply method is the problem - it never seems to get out of the loop.
Thanks a lot
Paul
final static char END_OF_STRING = 0xFF ;// end of command/inquiry/reply byte
static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
Uart.speed9600,
Uart.stop1 );
/**
* Get_Reply
*
* Gets result of an equiry or command
* result stored in RXBuffer
*/
void Get_Reply()
{
char crc = 0x00; // clear c to zero before check
RXbuffer.clear();
while (crc != END_OF_STRING)
{
if(rxUart.byteAvailable())
{
crc = (char)rxUart.receiveByte();
RXbuffer.append(crc);
if(DEBUG) //bring out RX buffer in Debug mode
{
Format.printf("%x, ",crc);
}
}
if (crc == END_OF_STRING)
{
System.out.println("Well I got the end!!"); //This point never seems to be got to
}
}
if(DEBUG) //put new line after RX buffer in Debug mode
{
System.out.println("");
}
return;
} // end of Get_Reply
Comments
byte gets sign extended into an integer.
Simple solution:
use int c = rx.receiveByte() & 0xFF;
regards peter
·
Paul