I want to use a Mega to receive serial packets, extract one of the data bytes, and use the value as an input to a PID section that will output a 0-5v control signal to a mosfet driver. For now, I want to test the serial part by connecting it to my known serial stream and display the extracted value as it changes. The specs of the serial stream are:
The interface is async serial at 57.6 kb, 8N1.
Each packet starts with two bytes: 0x55 & 0xAA
There are 273 data bytes following.
Then a two byte checksum. Add together the two beginning of record (BORI) bytes plus the 273 data bytes.
That value (a two byte value) should match the packet check sum to indicate a good packet.
Numbering the data bytes starting at 0 (0 offset into the data area):
Coolant temperature is offset 0xE3, conversion is °C = N * .75 – 40
Is the PacketSerial library the best way to approach decoding and extracting the value?
Here is some C++ code that is being used successfully to parse that serial stream, so it makes sense to me to model it for the arduino...
// Packet Parser
unsigned short int checksum = 0;
int ProcessEblData(BYTE charval)
{
int pbyte = 0;
unsigned short int checkval;
if ((charval == 0x55) && (packetindex == -1))
{
// this looks like a start of packet, reset our packet position counter
packetindex = 0;
// reset our checksum
checksum = 0;
}
if ((charval == 0xaa) && (packetindex == 1))
{
}
else if ((charval != 0xaa) && (packetindex == 1))
{
packetindex = -1;
}
if (packetindex >= 0)
{
ebl_packet[packetindex] = charval;
checksum += charval;
++packetindex;
}
if (packetindex == PACKET_SIZE)
{
// reset our index control...
packetindex = -1;
checkval = ebl_packet[PACKET_SIZE - 2] * 0x100;
checkval += ebl_packet[PACKET_SIZE - 1];
// remove the checksum value from the calculated checksum...
checksum -= ebl_packet[PACKET_SIZE - 1];
checksum -= ebl_packet[PACKET_SIZE - 2];
if (checksum == checkval)
{
++GoodPacketsReceived;
ProcessEblPacket();
}
else
{
++BadPacketsReceived;
}
}
return TRUE;
}
// this is defined as 1 (even though it's two) since the document that
// i was referring to used '1' as byte 0.
#define EBL_DATA_OFFSET 2
void ProcessEblPacket(void)
{
if (ebl_packet[0x00 + EBL_DATA_OFFSET] == 0xE0)
{
return;
}
coolant_temp = ebl_packet[0xE3 + EBL_DATA_OFFSET];
coolant_temp= ((float)coolant_temp * 0.75f) - 40;
coolant_temp = (coolant_temp * 1.8f) + 32; // convert to degF
