jefffhaynes / XBee

A .NET library for XBee wireless controllers
MIT License
41 stars 17 forks source link

Using Series 2 Xbees #3

Closed MeijinGandalf closed 8 years ago

MeijinGandalf commented 8 years ago

I'm having an issue trying to figure out how to construct a frame to send to the Series 2 Xbee. In XBeeController.cs you have the ExecuteAsync function which takes an object of type FrameContent. I assume this is the function that I want to use, but I am not sure where to go from there. Any help would be greatly appreciated! As an example I would like to construct a TX request frame.

jefffhaynes commented 8 years ago

So you actually want to use XBeeNode, which you can create using the XBeeController. Something like:

var node = await controller.GetRemoteAsync(address);
await node.TransmitDataAsync(data);

I don't have anything here to test it with but give it a try and let me know if you have any problems.

MeijinGandalf commented 8 years ago

Awesome that worked great! The only other question that I have is how receiving and parsing the frames works. I will keep looking through the code to see if I can figure it out. Thankyou!!!

jefffhaynes commented 8 years ago

If you want to send and receive frames, I believe you can use the XBeeController class to do that. However, the "preferred" (intended?) method is to call the corresponding method as I've tried to expose the various commands, interrupts, etc. in a more OOP way. Can you give me some idea of what you're looking to do?

MeijinGandalf commented 8 years ago

I want to poll 20-30 devices at a fairly quick pace for information such as speed, location, and voltage. I can't wait anymore than a second for a response before I have to pole the next device. I believe I want to use the ExecuteQueryAsync function in XBeeController and set a short timeout. I see that it returns an object of type TResponseFrame(Though it looks like TResponseFrame only has the frameID, is that correct?). I believe you have written code to parse this though I haven't quite found how to use it yet.

jefffhaynes commented 8 years ago

In XBee there isn't really the concept of a generic query command. The ExecuteQueryAsync method supports specific queries like CoordinatorEnableCommand, SleepOptionsCommand, etc. I think your best bet would be to put the XBees into a polling mode and look at the response data. So for example, you might do (only shown for one node):

await node.SetSampleRate(TimeSpan.FromSeconds(1));
var samples = node.GetSamples(); // reactive extensions version

// or alternatively...

node.SampleReceived+=...

That's probably an oversimplification but try it and let me know. You might also take a look at the short write-up on the front page since it covers some of these topics.

MeijinGandalf commented 8 years ago

So the remote XBees will themselves be attached to an SBC, Arduino, or a PI. So when the host asks sends something the program running on the remote side will look at that info and decide what to send back to the host so setting a sample rate on the remote node is not needed. Knowing that is there another way.

jefffhaynes commented 8 years ago

You basically have three options for getting data back:

-- Periodic sampling -- State change detection -- RX data

If the XBee is directly monitoring something you can use state change detection which is like periodic sampling but only triggered when an input changes. However, since you have a microcontroller or computer of some sort, your best option may be simply sending back RX data. That simply means pumping data into the RX pin on the XBee from a UART on your controller. Any data you push into the RX pin will show up in the node. Then you can do:

var receivedData = node.GetReceivedData(); // reactive extensions option

// or

node.DataReceived += ...
MeijinGandalf commented 8 years ago

Ok that will work out great! Thanks for all your help! I should be able to get everything working from here!

jefffhaynes commented 8 years ago

Sounds good. Let me know if you run into any issues. I don't know that I've ever tested the data receive coming asynchronously from a UART but it's possible I did and I don't remember :)

MeijinGandalf commented 8 years ago

K I will. The first test I ran on it went well. I will be running it through its paces in not too long I guess we will see what happens.

jefffhaynes commented 8 years ago

FYI your question made me think it might be nice to expose a Stream on the XBeeNodes. I don't know if this would work for what you're trying to do but it would allow continual streaming access to the TX/RX line, which could be kind of neat. I've finished coding it but I haven't had a chance to test it yet. If it's something you're interested in, let me know and I can send you an advance version or something.