C# Code Example for CheckSum Calcualtion

Well here is an example of C#.net code to format API command to send. This also shows how to calculate the checksum in C#. I could not find a flat example on here that worked, so here it is. hopefully this will start getting more .net development examples on here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XBEEComms
{
public class XbeeLocalCommands
{
#region Command constants
const byte cmd_StartDelimiter = 0x7E;
const byte cmd_Zero = 0x00;
const byte cmd_4Length = 0x04;
const byte cmd_8Length = 0x08;
const byte cmd_ATAPIIdentifier = 0x08;

    const byte cmd_FrameId = 0x52;

    #endregion

    #region Overall Functions
    public byte CalcCheckSum(byte[] res)
    {
        int cs = (byte)0xFF - (res[3] + res[4] + res[5] + res[6]);

        byte[] css = BitConverter.GetBytes(cs);

        return css[0];

    }

    public byte[] ConvertTextToHex(string str)
    {
        byte[] Hex;

        System.Text.UTF8Encoding strC = new System.Text.UTF8Encoding();

       Hex =  strC.GetBytes(str);

        return Hex;

    }

    #endregion

    public CommandResponse GetDestinationLowAddress(string FrameId)
    {

        CommandResponse CR = new CommandResponse(8);

        CR.CommandDescription = "DL : Destination Low Address";

        CR.CommandBytes[0] = cmd_StartDelimiter;
        CR.CommandBytes[1] = cmd_Zero;
        CR.CommandBytes[2] = cmd_4Length;
        CR.CommandBytes[3] = cmd_ATAPIIdentifier;
        CR.CommandBytes[4] = ConvertTextToHex(FrameId)[0];
        CR.CommandBytes[5] = ConvertTextToHex("D")[0];
        CR.CommandBytes[6] = ConvertTextToHex("L")[0];

        CR.CommandBytes[7] = CalcCheckSum(CR.CommandBytes);

        return CR;

    }

    public CommandResponse GetFirmwareVersion(string FrameId)
    {
        CommandResponse CR = new CommandResponse(8);

        CR.CommandDescription = "VL : Firmware Version Description";

        CR.CommandBytes[0] = cmd_StartDelimiter;
        CR.CommandBytes[1] = cmd_Zero;
        CR.CommandBytes[2] = cmd_4Length;
        CR.CommandBytes[3] = cmd_ATAPIIdentifier;
        CR.CommandBytes[4] = ConvertTextToHex(FrameId)[0]; ;
        CR.CommandBytes[5] = ConvertTextToHex("V")[0];
        CR.CommandBytes[6] = ConvertTextToHex("L")[0];

        CR.CommandBytes[7] = CalcCheckSum(CR.CommandBytes);

        return CR;

    }
}

    public class CommandResponse
    {
     public string CommandDescription;
     public byte[] CommandBytes;

public CommandResponse(int ByteLength)
{
CommandBytes = new byte[ByteLength];
}

}

}

If you cant understand how to use this, well im sorry. Let me know if you have any questions though.