void ReadSerialInput() { #define MAX_SERIAL_COMMAND_LENGTH 8 static char rxBuf[MAX_SERIAL_COMMAND_LENGTH + 2]; static uint8 rxLength = 0; while ((rxBuf[rxLength] = UART_1_UartGetChar()) > 0) { if (rxBuf[rxLength] > 0 && rxBuf[rxLength] != '\r') { // Received a character (ignore \r) rxLength++; if (rxBuf[rxLength - 1] == '\n') { // Received a command - ensure the buffer is null-terminated, // then process the command rxBuf[rxLength] = 0; ProcessSerialCommand(rxBuf); // Reset the buffer rxLength = 0; } else if (rxLength >= MAX_SERIAL_COMMAND_LENGTH) { // This line is too long; ignore it // (Set the first byte to zero and keep overwriting the // remaining bytes for as long as necessary) rxBuf[0] = 0; rxLength = 1; } } } }
int ReadInt() { uint32 rxData; int result = 0; int digitCount; for(;;) { rxData = UART_1_UartGetChar(); if(rxData){ if(rxData == '\r') break; UART_1_UartPutChar(rxData); result *= 10; result += rxData - '0'; } } return result; }