//grab commands from keyboardif(uartGetByte() == '1')do_something=1;if(uartGetByte() == '2')do_something=2;if(uartGetByte() == '3')do_something=3;if(uartGetByte() == '4')do_something=4;uartFlushReceiveBuffer();//flushes out entire recieve bufferSo if say I type '1' into the keyboard, do_something becomes 1. This works. Same if I press 2. But if I press 3 or 4 it doesnt work And is there a nicer way to write this code? For example:

hey Admin,so my programming sucks as well but i think i see your problem.in the first code section you are repeatedly calling uartGetByte().every time you call it you are getting the next character from the buffer.

Almost. What you type in is an ASCII character, which means if you are looking for 0-9, then you need to subtract '0' (32) from the character in order to get a number out of it.

doSomething = uartGetByte() - 32;

assuming doSomething is declared as an int, should work.

This doesn't do any range checking, so you'll want to make sure the result value is between 1 and whatever your upper limit is. If you need more than 1-9, then it gets more complicated, because you have to input multiple characters, and do a smarter ascii-to-integer conversion.

The nice thing about this is that it executes faster, you could call a subroutine instead of setting a variable, and it even gives you the ability to set a "failsafe" value or call a failsafe subroutine. Something that, I suspect, is useful when programming them robot contraptions. Now, if you're just going to print the data out, or assign a calculatable value to do_something, I agree with the subsequent posts that it can be and should be, done in a single statement.

Also, you say the stream will be fixed size, but the goal is to make the entire output a fixed size, so you'll need to pad fields if they might have numbers of different lengths so that the entire buffer will always be of a fixed size (e.g. 001, rather than 1). This can be done efficiently using the printf statement you are using to output the values (%3u rather than just %u, I think). It wastes bytes in the transmission, but makes processing much easier and faster.

Finally, range check and convert your character based values into actual hex values.

As an aside: You could also pass the values in true hex values rather than as character strings. With one 16 bit word per grouping, plus a control byte, that's 11 bytes and there's no printf overhead at all.