Congratulations mtiberia, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Uart Receive Query. Buffer Flush Command?

Assistance Requested

Help Richard R with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

Richard R claims to have checked these sources:

I am have a little issue with the below code.... I am attempting to read 3 different single byte return sensors on my new Create 2.... Now, it works perfectly if I read only one sensor in the repeatuntil loop. However if I do more than one read (like the code below).... the readings report back false... i.e. the bump sensor will indicate a bump when none occurred or the IR detect will indicate a bump instead of a indicating a close proximity of an object....

So am I daft and my code is wrong or does maybe the UART buffer need to be flushed after each read?

I am stumped....

Thanks guys



uartWrite(0,1,142,45) # Read Roomba IR sensors single byte

IF (UARTAvailable(0,1)>0)


uartWrite(0,1,142,7) #Roomba Bump and wheel drop sensor single byte

IF (UARTAvailable(0,1)>0)


uartWrite(0,1,142,58) # Stasis (Rommba moving forward) single byte
IF (UARTAvailable(0,1)>0)

print($x+" IR light object detected")
print($y+" bump detected")
print($z+" Is moving forward")


I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.

Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


You can try checking to see how many bytes are in the buffer or what I would do do is send the commands to read the sensor one after the other and then read the three bytes received.

$x = UartAvailable(0, 1)

print("Bytes in buffer: " + $x) # checks how many bytes are in the buffer

$RX_DATA = UARTRead(0, 1, $x) # places all the bytes RX into $RX_DATA

# read 3 data bytes



@mtiberia .... Thanks man your code worked with slight modification... Had to put a condition (if statement) to check on the # of bytes in the buffer... If not every 20 or so loops would produce an data out of bounds error... So now, although the bump and IR sensor spit back the correct readings the Stasis (whether the Create is moving forward or not) reports 2 for stopped and 3 for forward movement... According to the Create 2 ROI manual it is supposed to be 0 for not moving and 1 for moving forward... Hmmmm ... Nevertheless it seems to work better with your code....

***Edit*** I am testing this on a Roomba 500 series and not my Create 2 yet.... The Create 2 is a 600 series so that may be the difference in the Stasis reporting different numbers that it is supposed to....


@mtiberia.... Thanks for your help.... Now if you can only help me read 2 byte data (like Roomba voltage values), that would be awesome... It is returned as a value between 0 – 65535 mV ... The two bytes are read as high byte and low byte...


I'm looking at the Create 2 as well, it would make a great mobile platform.





The above formula is if you RX the LSB first and MSB second.
Just change positions if its the other way around.


@mtiberia Thanks I'll give it a try...

By the way my Create 2 came with only the brushes and brush cage retainer missing (in order to make it a full blown Roomba vacuum.... )... For $200 bucks, not bad.... I would buy a second one if it weren't for the fact the Canadian $ is so weak compared to the US dollar....


@mtiberia ... Here is my code below... It doesn't work... Every loop of the repeatuntil just adds 2 bytes to the buffer (2,4,6,8,10...etc) until it crashes as an out of bounds error.... That's why I was wondering if a flush buffer command was needed... Could be my code still though... High byte is first....



uartWrite(0,1,142,22) # Volatge check 2 byte receive
$rx = UartAvailable(0, 1)

print($rx+" bytes in the buffer")
$RX_DATA = UARTRead(0, 1, $rx)



Ok, it doesn't work at all (thought it did)... although the first read reports 2 bytes in the buffer I get an error at this line (see below)..." Index was outside the bounds of the array" error... Each successive read puts 2 more bytes in the buffer (2,4,6,8,10, etc...)




Interesting try placing the initiate UART inside the loop. That should flush the buffer.