Changeset - 4b4d7ead8c5a
[Not reviewed]
default
0 1 0
mkanning@CL-SEC241-09.cedarville.edu - 13 years ago 2012-10-25 22:23:40
mkanning@CL-SEC241-09.cedarville.edu
added checksum calculations to parser
1 file changed with 7 insertions and 2 deletions:
0 comments (0 inline, 0 general)
master/master/lib/serparser.c
Show inline comments
 
@@ -30,39 +30,40 @@ uint8_t buffer[MAX_CMD_LEN+2];
 
// Current buffer location
 
uint8_t bufferPosition = 0;
 
 
//current length of the data transmission (and checksum)
 
int dataLength;
 
 
//data (and checksum) of most recent transmission
 
char data[16];
 
 
//ID of the sensor of most recent transmission
 
char sensorID;
 
 
//checksum to be calculated and then compared to the received checksum
 
char checksumCalc;
 
 
/* return char from UART (h/w buffer) */
 
uint8_t uart_getchar(void)
 
{
 
	// Wait for chars
 
	/*	while (!(UCSRA & (1<<RXC)))
 
	{
 
		idle();
 
	}
 
	return UDR;
 
	*/
 
	return 0;
 
}
 
 
 
 
/* Process incoming serial data */
 
int uart_Parser(void)
 
{
 
 
	char byte;
 
 
	if(SERIAL_RX_HASBYTES) // Only process data if buffer has a byte
 
	{
 
		
 
		// Pull byte off of the buffer
 
		byte = uart_getchar();
 
		buffer[bufferPosition] = byte;
 
@@ -75,48 +76,52 @@ int uart_Parser(void)
 
				bufferPosition++;
 
			}
 
			else // Not a start byte that we're expecting. Reset to initial state.
 
			{
 
				bufferPosition = 0;
 
			}
 
		}
 
		else if(bufferPosition == 1)
 
		{
 
			if(byte == MODULE_ID) //this transmission is intended for the master; continue parsing
 
			{
 
				bufferPosition++;
 
				checksumCalc += byte;
 
				dataLength = 0; //reset dataLength here to protect from bad inputs
 
			}
 
			else //this transmission is intended for another module; stop parsing and reset
 
			{
 
				bufferPosition = 0;
 
			}
 
		}
 
		else if(bufferPosition == 2)
 
		{
 
			sensorID = byte; //store the type of data receiving
 
			bufferPosition++;
 
			checksumCalc += byte;
 
		}
 
		else if (bufferPosition == MAX_CMD_LEN) //command is too long and bad data has been recieved; reset
 
		{
 
			bufferPosition = 0;
 
		}
 
		else
 
		{
 
			if (byte == ']') //this is the end of the transmission
 
			{
 
				bufferPosition = 0;
 
			}
 
			else //still receiving data
 
			{
 
				data[dataLength] = byte;
 
				dataLength++;
 
				bufferPosition++;
 
				checksumCalc += byte;
 
			}
 
 
		}
 
		
 
	}
 
	else
 
	{
 
		return 0; // serial data not available
 
	}
 
}
 
\ No newline at end of file
0 comments (0 inline, 0 general)