jefffhaynes / XBee

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

Examples #8

Closed PreciousRoy0 closed 8 years ago

PreciousRoy0 commented 8 years ago

Hello,

I want to thank you for letting us use this cool lib, i am however not the best c# programmer and it is not really apparent how to use the ExecuteQueryAsync to send data. currently i keep getting timeout exceptions on the thread.

i was wondering could you make a simple ample on how to send data?

jefffhaynes commented 8 years ago

Can you tell me a little more about what you're trying to do?  Are you trying to send serial data to the device for transparent serial mode or something similar?  If so, you probably want to use TransmitDataAsync.  In general, you should try to use one of the more explicit methods that wrap ExecuteQueryAsync rather than using ExecuteQueryAsync directly. But either way, a timeout exception typically means the device or the remote device didn't respond before the specified timeout occurred. 

_____________________________

From: PreciousRoy0 notifications@github.com Sent: Monday, April 18, 2016 3:21 AM Subject: [jefffhaynes/XBee] Examples (#8) To: jefffhaynes/XBee xbee@noreply.github.com

Hello,

I want to thank you for letting us use this cool lib, i am however not the best c# programmer and it is not really apparent how to use the ExecuteQueryAsync to send data. currently i keep getting timeout exceptions on the thread.

i was wondering could you make a simple ample on how to send data?

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub

PreciousRoy0 commented 8 years ago

No i have my xbees in api mode with AP setting to 2.

i managed to get it sending with await m_Controller.ExecuteAsync(txRequestFrame);

but it took me several hours to figure it all out. What would of been cool is a sample program like this, simple and to the point.

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

using XBee;
using XBee.Frames;

namespace ConsoleXbeeTest
{
    public class XbeeConnector
    {
        private XBeeController m_Controller = new XBeeController();

        public bool IsConnected
        {
            get
            {
                return m_Controller.IsOpen;
            }
        }

        public async void Initialize(string port, int baudeRate)
        {
            if (!m_Controller.IsOpen)
                await m_Controller.OpenAsync(port, baudeRate);

            m_Controller.DataReceived += M_Controller_DataReceived;
        }

        public async void Send(LongAddress address, byte[] data)
        {

            var txRequestFrame = new TxRequestExtFrame(address, data)
            { FrameId = 0x01 };

            await m_Controller.ExecuteAsync(txRequestFrame);
        }

        private void M_Controller_DataReceived(object sender, SourcedDataReceivedEventArgs e)
        {
            Console.WriteLine("Recieved data from: " + e.Address.LongAddress.Value.ToString("X4"));
        }
    }

    class Program
    {
        static XbeeConnector con;

        static void Main(string[] args)
        {
            con = new XbeeConnector();
            con.Initialize("COM9", 9600);

            bool datasent = false;
            while(true)
            {
                if(con.IsConnected && !datasent)
                {
                    con.Send(new XBee.LongAddress(0x0013A20040A9DAEE),
                        System.Text.Encoding.UTF8.GetBytes("Hi here is some data"));
                    datasent = true;
                    Console.WriteLine("Sending data to: " + "0013A20040A9DAEE");
                }
            }
        }
    }
}

The only problem with this sample code is that i can only receive data once.

Edit: after reading my post again, it seems like i am criticizing... I do not mean to do that in any way.

jefffhaynes commented 8 years ago

I don't know if you read the readme but you don't need all that. Try this:

    class Program
    {
        private static XBeeController _controller;

        static void Main(string[] args)
        {
            Run();
            Console.ReadKey();
        }

        private static async void Run()
        {
            _controller = new XBeeController();
            await _controller.OpenAsync("COM9", 9600);

            var node = await _controller.GetRemoteNodeAsync(new NodeAddress(new LongAddress(0x0013A20040A9DAEE)));
            node.DataReceived += (sender, args) => Console.WriteLine("Received: {0}", BitConverter.ToString(args.Data));
            await node.TransmitDataAsync(Encoding.UTF8.GetBytes("hi here is some data"));
        }
    }
PreciousRoy0 commented 8 years ago

Thanks for the help, i am still having some trouble retrieving data

I changed your code, i find lambdas are hard to debug.

    class Program
    {
        private static XBeeController _controller;

        static void Main(string[] args)
        {
            Run();
            Console.ReadKey();
        }

        private static async void Run()
        {
            _controller = new XBeeController();
            await _controller.OpenAsync("COM9", 9600);

            var node = await _controller.GetRemoteNodeAsync(new NodeAddress(new LongAddress(0x0013A20040A9DAEE)));
            node.DataReceived += Node_DataReceived;
            _controller.DataReceived += _controller_DataReceived;
            //node.DataReceived += (sender, args) => 
            //Console.WriteLine("Received: {0}", BitConverter.ToString(args.Data));
            await node.TransmitDataAsync(Encoding.UTF8.GetBytes("hi here is some data"));
        }

        private static void _controller_DataReceived(object sender, SourcedDataReceivedEventArgs e)
        {
            Console.WriteLine("CReceived: {0}", BitConverter.ToString(e.Data));
        }

        private static void Node_DataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine("Received: {0}", BitConverter.ToString(e.Data));
        }
    }

ouput: c# : CReceived: 01-48 Arduino:

hi here is some data
Sending data
Sending data

so the node.DataReceived does not receive anything but sending works, and _controller.DataReceived works but stops after the first packet received.

Packet that i am sending from the arduino after i receive it in XCTU:

Receive Packet (API 2)

7E 00 0E 90 00 7D 33 A2 00 40 A9 DA EE 39 86 01 48 69 98

Start delimiter: 7E
Length: 00 0E (14)
Frame type: 90 (Receive Packet)
64-bit source address: 00 13 A2 00 40 A9 DA EE
16-bit source address: 39 86
Receive options: 01
RF data: 48 69
Checksum: 98

Edit: I just noticed, the data that i do receive is not correct it is {1, 72} when it should be {48, 69}

Edit 2: a little more about the data, if i send "HI MY NAME" from the arduino i receive "\u0001Hi MY NAM" so it leads me to think that it starts the data at the wrong place because it leaves off the E at the end and adds some stuff at the beginning.

if i recieve the same packet with XCTU i get it correctly

Receive Packet (API 2)

7E 00 16 90 00 7D 33 A2 00 40 A9 DA EE 39 86 01 48 69 20 4D 59 20 4E 41 4D 45 91

Start delimiter: 7E
Length: 00 16 (22)
Frame type: 90 (Receive Packet)
64-bit source address: 00 13 A2 00 40 A9 DA EE
16-bit source address: 39 86
Receive options: 01
RF data: 48 69 20 4D 59 20 4E 41 4D 45
Checksum: 91
guyeeba commented 8 years ago

Set your XBee to API mode 1. Arduino's XBee lib needs mode 2, Jeff's lib needs mode 1.

PaulNoto commented 8 years ago

I agree. You need to use API Mode 1. Started with my coordinator and nodes set to mode 2 and wasn't able to communicate with my nodes. Took a guess and switched everything to mode 1 and everybody showed up.

jefffhaynes commented 8 years ago

Yeah, I probably need to figure out a way to check for that.

PreciousRoy0 commented 8 years ago

Hallo and thanks for your reply's. the thin is i am using the arduino xbee lib and it wont work if its in ap mode 1. so the libs are not compatible?

Edit: Forget my last question... apparently it works like a charm now. thank you all for the help!

jefffhaynes commented 8 years ago

I'm not sure I understand. The mode only applies to the controlling device interface which can't be both Arduino and a PC at the same time. The mode shouldn't affect XBee-to-XBee communications. What are you trying to do?

On Sat, Apr 23, 2016 at 11:07 PM -0700, "PreciousRoy0" notifications@github.com wrote:

Hallo and thanks for your reply's. the think is i am using the arduino xbee lib and it wont work if its in ap mode 1. so the libs are not compatible?

— You are receiving this because you commented. Reply to this email directly or view it on GitHub

jefffhaynes commented 8 years ago

This write-up probably explains better than I am.  http://www.desert-home.com/p/the-world-of-xbee.html?m=1#Modes Specifically he talks about mixing API mode 1 and 2 and how that shouldn't be a problem. 

On Sat, Apr 23, 2016 at 11:07 PM -0700, "PreciousRoy0" notifications@github.com wrote:

Hallo and thanks for your reply's. the think is i am using the arduino xbee lib and it wont work if its in ap mode 1. so the libs are not compatible?

— You are receiving this because you commented. Reply to this email directly or view it on GitHub