Hi,
Probably a very basic question, regarding the Pololu Library serial communication example. Been using it pretty much as is for years, just copying/pasting it and adjusting slightly.
Now I’m wondering a couple of things - again very basic stuff, but helpful for my understanding. About this example functions:
void check_for_new_bytes_received()
{
while(serial_get_received_bytes() != receive_buffer_position)
{
// Process the new byte that has just been received.
process_received_byte(receive_buffer[receive_buffer_position]);
// Increment receive_buffer_position, but wrap around when it gets to
// the end of the buffer.
if (receive_buffer_position == sizeof(receive_buffer)-1)
{
receive_buffer_position = 0;
}
else
{
receive_buffer_position++;
}
}
}
- Is there a reason why the while loop couldn’t be an if statement here?
- Couldn’t receive_buffer_position simply be set to serial_get_receivedIbytes() here, instead of the if statement?
Basically, any reason why this wouldn’t work:
void check_for_new_bytes_received()
{
unsigned char current_buffer_byte = serial_get_received_bytes();
if(current_buffer_byte != receive_buffer_position)
{
process_received_byte(receive_buffer[receive_buffer_position]);
receive_buffer_position = current_buffer_byte;
}
}
Thanks,
-A