Manual-25Example Packet Expansion Code for RW 232 Messages// 09-10-96 - Devin Cook (Derived from RW232.CPP code)// This code only deals with the Body of an RW 232 message (Command/Data)// The steps needed to fully communicate with an RW 232 device are as follows:// 1. Send the Address: [FB xx FB xx]// 2. Get the returned Device Type and Device ID flags// 3. Send the FB expanded Body// 4. Get and check the returned ComStat byte// Take a simple command and expand it into a full packet.// Input:// Buff - BYTE array with the unexpanded message and lots of extra room// MsgLen - Unexpanded message length// Steps required are:// 1. Add Packet size. This is simply the Command length + 1 for the checksum// 2. Duplicate 0xFBs// 3. Calculate Checksum// 4. Add Checksum to packet (Check for a 0xFB Checksum!)// 5. Copy Packet back to the buffer// 6. Return the new Packet Size// Note: The buffer must be large enough to accept the expanded data. No checking is done to verify it is, so be careful!// A packet into this routine consists of the one byte Command and any Dataint CmdToPacket(BYTE Buff[ ], int MsgLen){BYTE L_MSB = ((MsgLen+1) >> 8) & 0xFF; // Grab MSB of SizeBYTE L_LSB = (MsgLen+1) & 0xFF; // Grab LSB of Size// FBs is the number of 0xFB bytes in the messagesint FBs = 0;// Don’t forget to check message length for FBsif (L_MSB == 0xFB)FBs ++;if (L_LSB == 0xFB)FBs ++;// Calculate Checksum of Message Length bytes along with bytes in the packetint CheckSum = L_MSB + L_LSB;for (int x=0;x{CheckSum += Buff[x];if (Buff[x] == 0xFB)FBs ++;}CheckSum = (256-CheckSum) & 0xFF;// Don’t forget to up the FB count for a FB checksum!if (CheckSum == 0xFB)FBs ++;// New Length is: 2 (For 2 length bytes) + L (Old message Length) + Repeated FBs count + 1 (For CheckSum)int NewLen = 2 + MsgLen + FBs + 1;// Create a temporary holding tank for Packet ExpansionBYTE Packet[MAX_CMD_BUF];BYTE *Ptr = Packet;// Stick message length in the packet (Watching for FBs of course)*(Ptr++) = L_MSB ;if (L_MSB == 0xFB)*(Ptr++) = 0xFB;*(Ptr++) = L_LSB ;if (L_LSB == 0xFB)*(Ptr++) = 0xFB;// Expand the original packet into the new bufferfor (x=0;x{*(Ptr++) = Buff[x];if (Buff[x] == 0xFB)*(Ptr++) = 0xFB;}// Add the Checksum byte (or Bytes if Checksum == FB)*(Ptr++) = CheckSum ;if (CheckSum == 0xFB)*(Ptr++) = 0xFB;// Copy the expanded packet back into the original buffermemcpy(Buff,Packet,NewLen);return NewLen;}