Ports for Embedded Systemsunsigned char serial_in;
while(1)
{
// Task loop.
receive_serial_data();
// Call routines to perform other tasks.
}void receive_serial_data(void)
{
unsigned char serial_in;if DataRdyUSART()
{
// A byte is available.serial_in = getcUSART();// Do something with the received byte.
}
}5 &
When expecting multiple bytes, firmware can wait for the bytes to arrive or read
and store individual bytes until all have arrived. An hserin statement can wait to receive a specified number of bytes to store in a
byte array. This example waits for 7 bytes and stores each received byte in
sequence in received_text:
received_text var byte[7]
hserin [STR received_text\7]
A receiving computer can’t guarantee that a remote computer will send the
desired number of bytes. Using a timeout will keep the code from hanging for-
ever while waiting for bytes to arrive.
received_text var byte[7]
hserin 1000, continue, [STR received_text\7]continue: