Home/Support/Support Forum/C# Reading an API frame received from an XBee

C# Reading an API frame received from an XBee

0 votes
I'm receiving an api frame from an XBee endpoint to an XBee coordinator that's connected to my computer through a com port.

This API frame I'm receiving is a change detect that's sent when a change is detected (simply enough). There's no set interval for this change detect.

When this api frame is received I want to be able to read it in my C# application, for the following reason:

When the frame is like this: 7E 00 0A 83 00 00 1C 00 01 00 81 00 80 5E (12th position is 80) I want to indicate that the light is off.

When the frame is like this: 7E 00 0A 83 00 00 2B 00 01 00 81 00 81 4E (12th position is 81) I want to indicate that the light is on.

My code for this is very simple - all I am missing is getting a hold of the frame in C#. Here's my code:

byte[] switch_indicator = somehow_read_the_frame;
if (switch_indicator[12] = 0x81)
{
textBox1_TextChanged.BackColor = System.Drawing.Color.Green;
}

if (switch_indicator[12] = 0x80)
{
textBox1_TextChanged.BackColor = System.Drawing.Color.Red;
}

How would I go about getting this frame?

I've thought about somehow getting it from the com port but I am not sure how to do this.

Any ideas or suggestions would be highly appreciated.
asked Apr 5, 2016 in IEEE 802.15.4 by Lars New to the Community (5 points)

Please log in or register to answer this question.

1 Answer

0 votes
Have you tried simply doing a read till the buffer is empty and feeding the data into an array or

public int Read(
char[] buffer,
int offset,
int count
)
answered Apr 6, 2016 by mvut Veteran of the Digi Community (12,786 points)
I have tried assigning a button to this
            byte[] buffer = new byte[14];

            serialPort1.Read(buffer, 0, buffer.Length);


            string buffer_string = BitConverter.ToString(buffer);
            read_textbox.Text = buffer_string;

And it works fine except when no change is detected, and I press the button to read the frame. Since there is no new frame, the program waits for a new frame. When the new frame is received (a change is detected) it's transmitted incompletely. Such as 7E 00 00 00 00... 00, and then the next frames gets ruined as well, because the incomplete frame "offsets" it.

I believe this is because it only updates on a button press. If I somehow got it to run continuously in a while loop I think that would solve the problem - or if there's a way to add a delay so that it takes the time to read the whole frame.

Edit: There's also a problem with the frames stacking up, so if I turn the switch on/off several times, then update in my program, I have to update once for every switch I did. This would also be solved with a real time update of the serialPort1.Read(buffer, 0, buffer.Length) function.

What bothers me is that it waits for a new frame when it's read the last one.
I think you answered yourself.  A while loop. Try doing that and feeding that data into an array.
But the problem is the serialPort1.Read(buffer, 0, buffer.Length); function. When that specific line of code runs, it waits for a new frame. The code stops there to wait. That's the frustrating part.
...