Hi!
I want to use the api mode of operation in the xtend module. I am working in c#. how can i assemble hex packet?? can anyone please explain? When i send data to serialport it is a string, not hex.
Hi!
I want to use the api mode of operation in the xtend module. I am working in c#. how can i assemble hex packet?? can anyone please explain? When i send data to serialport it is a string, not hex.
Not sure what the xtend module is, but I’ve been playing with xbee’s with API…here is some simple .net console application code I used for basic testing…just getting started using API…
Three xbee’s all API firmware…2 receiving end devices (X-CTU terminal) on XP computer…1 sending router (via MSVS debug…this code) from vista…
Code does show some packet assembling, checksum calc, and hex conversion…
Delays are required for response from receiving xbee’s…
Pasted code…gave up trying to attach file…code won’t look right…
using System;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static SerialPort xbee = null;
static void Main(string[] args)
{
xbee = new SerialPort(“COM6”, 9600, Parity.None, 8, StopBits.One);
xbee.Open();
// Change 64 and 1 bit addresses
SendString(0x0013A200405CF151, 0x38DC, “Hello\r”);
string str1;
int slp1 = 100;
for (int x = 0; x < 5; x++) // 5 trys for response, increasing delay each try
{
Thread.Sleep(slp1);
if (xbee.BytesToRead > 0)
{
byte[] rsp = new byte[xbee.BytesToRead];
xbee.Read(rsp, 0, xbee.BytesToRead);
str1 = ToHexDigits(rsp);
break;
}
slp1 += 100;
}
// Change 64 and 1 bit addresses
SendString(0x0013A200405CF154, 0x443B, "Goodby\r");
string str2;
int slp2 = 100;
for (int x = 0; x < 5; x++) // 5 trys for response, increasing delay each try
{
Thread.Sleep(slp2);
if (xbee.BytesToRead > 0)
{
byte[] rsp = new byte[xbee.BytesToRead];
xbee.Read(rsp, 0, xbee.BytesToRead);
str2 = ToHexDigits(rsp);
break;
}
slp2 += 100;
}
xbee.Close();
}
// 64bit addres (serial number low/high), 16bit address (MY), string to send
static void SendString(long aDestAHAL, ushort aDestMY, string aString)
{
// Add 18 bytes for Frame delimeter, Frame length, Destination address L/H/MY, and Checksum
uint lBufferLen = (uint)18 + (uint)aString.Length;
// Allocate buffer
byte[] lBuffer = new byte[lBufferLen];
// subtract Frame delimeter, Frame length, and Checksum
lBufferLen -= 4;
lBuffer[0] = 0x7E; // Frame delimeter
lBuffer[1] = (byte)(lBufferLen >> 8); // Lengh MSB
lBuffer[2] = (byte)lBufferLen; // Length LSB
// Checksum START
lBuffer[3] = 0x10; // Frame Type
lBuffer[4] = 0x01; // Frame ID
// Init CheckSum to Frame Type and ID
uint lCheckSum = (uint)(lBuffer[3] + lBuffer[4]);
// Init buffer indexer to end of destination address
int y = 12; // Buffer indexer, we'll be counting backwards
for (int x = 0; x < 8; x++)
{
lBuffer[y] = (byte)aDestAHAL;
lCheckSum += (uint)lBuffer[y];
aDestAHAL = aDestAHAL >> 8;
y--;
}
lBuffer[13] = (byte)(aDestMY >> 8);
lBuffer[14] = (byte)aDestMY;
lCheckSum = lCheckSum + (uint)lBuffer[13] + (uint)lBuffer[14];
// Broadcast Radius and options
lBuffer[15] = 0x00;
lBuffer[16] = 0x00;
lCheckSum = lCheckSum + (uint)lBuffer[15] + (uint)lBuffer[16];
// Init buffer indexer to start of payload(string)
y = 17; // Buffer indexer
for (int x = 0; x < aString.Length; x++)
{
lBuffer[y] = (byte)aString[x]; // Append payload(char)
lCheckSum += (uint)lBuffer[y]; // Accumulate Checksum
y++;
}
// Checksum END
lBuffer[lBuffer.Length - 1] = (byte)(0xFF - lCheckSum); // Checksum
xbee.Write(lBuffer, 0, lBuffer.Length);
}
static string ToHexDigits(byte[] val)
{
char[] ary = new char[2];
string str = "";
for (int x = 0; x < val.Length; x++)
{
ToHexDigits(val[x], ref ary);
str += ary[0];
str += ary[1];
if (x < val.Length - 1)
str += " ";
}
return new string(str.ToCharArray());
}
static void ToHexDigits(byte val, ref char[] ary)
{
ary[1] = "0123456789ABCDEF"[val & 0xF];
ary[0] = "0123456789ABCDEF"[(val / 16) & 0xF];
}
}
}