Search code examples
c#apixbee

C# Reading an API frame received from an XBee


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.

Edit: Here's my progress.

    private void test_read_Click(object sender, EventArgs e)
    {

        byte[] buffer = new byte[14];
        serialPort1.Read(buffer, 0, buffer.Length);

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

        if (buffer[12] == 129)
        {
            textBox1.BackColor = System.Drawing.Color.Green;
        }

        if (buffer[12] == 128)
        {
            textBox1.BackColor = System.Drawing.Color.Red;
        }
    }

This works as long as I press the switch, then click the button in my program. If I click the button in my program without a change being detected (the switch hasn't been pressed) the program waits for such a change, then it reads it. But it reads it incompletely. I get a frame such as 7E 00 00 00... 00. Then the next frame ends with 7E for some reason and offsets it.

Also, if I press the switch several times, then click my button to update, I have to click the button many times to get through to the last frame.

Ultimately, I'd want the program to update the color when a change is detected. Not by a manual button press to check, nor a while loop that runs infinitely using a lot of unnecessary resources.

The byte/frames will always be a fixed size.

Any input will be greatly appreciated.


Solution

  • You'll need to do a little more parsing of the frames. The XBee module will generate other frame types, so you'll need to watch for the 0x7E start of frame, parse the frame length and frame type, and probably even want to verify the checksum before processing the data.

    There should be a serial API for opening the serial port at a specific baud rate and then reading/writing characters to it. I've done it in C as part of an Open Source library I created for interfacing with XBee modules. If you can't find anything in the C# docs for serial ports, maybe you can make use of the Win32 API I'm using (see xbee_serial.c).

    Update:

    In a typical setup, you keep a buffer with partial frames until you have a complete frame for processing. Call serialPort1.Read() periodically (every 100ms -- depends on how responsive you want to be) and add bytes to the buffer. It's probably returning the number of bytes it actually read, so you can keep track of where you are in the buffer.

    Once you have a complete frame, then you hand it off to your function that parses it and acts on the contents.