You are quite welcome to look at my code...
It received a message, validates it's checksum, and once it has a valid message, it sends a valid message back.
Also, There is nothing wrong with the LabVIEW code I am using, as I was the one who wrote it and have been using it sucsesfully for over a year.
int inByte = 0; // incoming serial byte
int x = 0;
int potentialCheck = 0;
int actualCheck = 0;
int DCPstart, DCPflag0, DCPflag1, DCPkey, DCPseq, DCPid, DCPlen, DCPdst, DCPsrc;
int DCPhdrChk1, DCPhdrChk2;
int DCPIncomingMessage[23];
int newCheckSum;
boolean DCPreading, stopReadingDCP, messageAwaitingProcessing;
int DCPbyteReadCount = 0;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
establishContact(); // send a byte to establish contact until receiver responds
DCPreading = false;
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
if (DCPreading == true) // Are we still reading the message?
{
DCPIncomingMessage[DCPbyteReadCount] = inByte; // Add next byte to array
DCPbyteReadCount = DCPbyteReadCount + 1; // Keeps check of how many bytes we have read
if (DCPbyteReadCount >= 12)
{
validateChecksum(); // is it a complete message?
}
if (stopReadingDCP == true) // have we reached the end of the message?
{
DCPreading = false; // stop next time around
messageAwaitingProcessing = true;
}
}
else
{
if (inByte == 83) // Is this the start of a DCP message
{
DCPIncomingMessage[0] = inByte; // place the first byte into the beginning of the array
DCPreading = true; // let us know that we are part way through reading a message
DCPbyteReadCount = 1; // reset the byte count
}
}
if (messageAwaitingProcessing == true)
{
DCPstart = DCPIncomingMessage[0];
DCPflag0 = DCPIncomingMessage[1];
DCPflag1 = DCPIncomingMessage[2];
DCPseq = DCPIncomingMessage[3];
DCPid = DCPIncomingMessage[4]*100+DCPIncomingMessage[5];
DCPdst = (DCPIncomingMessage[6]*1000000)+(DCPIncomingMessage[7]*10000)+(DCPIncomingMessage[8]*100)+(DCPIncomingMessage[9]);
newCheckSum = DCPstart ^ DCPflag0 ^ DCPflag1 ^ DCPseq ^ DCPid ^ DCPdst;
Serial.print(DCPstart,BYTE);
Serial.print(DCPflag0,BYTE);
Serial.print(DCPflag1,BYTE);
Serial.print(DCPseq,BYTE);
Serial.print(DCPid,BYTE);
Serial.print(DCPdst,BYTE);
Serial.print(newCheckSum,BYTE);
// PAYLOAD SHOULD BE DELIVERED HERE
//Serial.print("I'M SOMTHING NEW!",BYTE);
messageAwaitingProcessing = false;
}
}
}
void establishContact() {
while (Serial.available() <= 0) {
delay(300); // Wait until device is communicated with
}
}
void validateChecksum() {
// if the checksum is valid
x = 0;
potentialCheck = DCPIncomingMessage[0]; // Add first Value into check
do
{
x = x + 1; // Ensure the first value is XORed by the second
potentialCheck = potentialCheck ^ DCPIncomingMessage[DCPbyteReadCount]; // XOR each value
}
while (x < DCPbyteReadCount - 2);
actualCheck = DCPIncomingMessage[DCPbyteReadCount - 1]; // Add First Value
actualCheck = actualCheck *100; // Shift up 2
actualCheck = actualCheck + DCPIncomingMessage[DCPbyteReadCount]; // Add Second Value
if (actualCheck == potentialCheck)
{
stopReadingDCP = true; // This flag is only set if a valid checksum is identified
}
}