<송신부>
#define headerData 0xff
unsigned char checkSum = 0;
Transmit() {
checkSum = (char)(data1 + data2 + data3);
printf(headerData);
printf(data1);
printf(data2);
printf(data3);
printf(checkSum);
}
<수신부>
unsigned char rxdDataBuffer = 0;
unsigned char rxdDataState = 0;
unsigned char rxdDataPacketCount = 0;
unsigned char rxdDataPacket[6];
unsigned char rxdDataError = 0;
unsigned char rxdDataBufferCheckSum = 0;
unsigned char rxdDataCheckSum = 0;
Receive() {
rxdDataBuffer = UDR1;
switch(rxdDataState) {
case 0:
if(rxdDataBuffer == 0xff) {
rxdDataState = 1; rxdDataError = 0; rxdDataBufferCheckSum = 0; rxdDataCheckSum = 0;
} break;
case 1:
if(rxdDataPacketCount == 0) {
rxdDataPacket[0] = rxdDataBuffer; rxdDataPacketCount = 1;
} else if(rxdDataPacketCount == 1) {
rxdDataPacket[1] = rxdDataBuffer; rxdDataPacketCount = 2;
} else if(rxdDataPacketCount == 2) {
rxdDataPacket[2] = rxdDataBuffer; rxdDataPacketCount = 3;
} else if(rxdDataPacketCount == 3) {
rxdDataBufferCheckSum = rxdDataBuffer;
rxdDataCheckSum = (char)(rxdDataPacket[0] + rxdDataPacket[1] + rxdDataPacket[2]);
if(rxdDataCheckSum != rxdDataBufferCheckSum) {
rxdDataError = 1; rxdTestCheck = 0;
} else { rxdDataError = 0;
if(rxdDataPacket[2] == 0xcc) rxdTestCheck = 1;
else rxdTestCheck = 0;
} rxdDataState = 0; rxdDataPacketCount = 0;
} break;
default:
break;
} UDR1 = 0;
}