jefffhaynes / XBee

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

DataReceived event not working? #24

Closed Vladdi00 closed 7 years ago

Vladdi00 commented 7 years ago

Hello, First off, thanks for this awesome library. I'm using it in a simple project I'm working on where I send serial data every 3 seconds from an end-point device to an API coordinator (mode 1). The frames show up in XCTU when monitoring the coordinator, but I can't get them to show up using this library. The DataReceived event seems to never trigger.

Am I doing something wrong or is this known?

namespace Gateway
{
    class Program
    {
        private static async void ConnectToCoordinator()
        {
            var controller = new XBeeController("COM3", 9600);

            controller.NodeDiscovered += async (sender, args) =>
            {
                Console.WriteLine("Discovered {0}", args.Name);

                var Node = args.Node;
                Node.DataReceived += (node, ev) => Console.WriteLine("Received: {0}", BitConverter.ToString(ev.Data));
            };

            try
            {
                await controller.OpenAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine("Succes!");

            await controller.DiscoverNetworkAsync();
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Press Enter to connect to Coordinator.");
            Console.ReadLine();

            ConnectToCoordinator();

            Console.ReadLine();
        }
    }
}

My thanks in advance!

jefffhaynes commented 7 years ago

Sorry you're having trouble. It might be a few days but I'll take a look, thanks.


From: Vlad notifications@github.com Sent: Tuesday, May 16, 2017 3:03:00 PM To: jefffhaynes/XBee Cc: Subscribed Subject: [jefffhaynes/XBee] DataReceived event not working? (#24)

Hello, First off, thanks for this awesome library. I'm using it in a simple project I'm working on where I send serial data every 3 seconds from an end-point device to an API coordinator. The frames show up in XCTU when monitoring the coordinator, but I can't get them to show up using this library. The DataReceived event seems to never trigger. Am I doing something wrong?

using System; using System.IO.Ports; using System.Threading.Tasks; using XBee; using Newtonsoft.Json; using XBee.Frames.AtCommands; using System.Collections.Generic;

namespace Gateway { class Program { private static async void ConnectToCoordinator() { var controller = new XBeeController("COM3", 9600);

        controller.NodeDiscovered += async (sender, args) =>
        {
            Console.WriteLine("Discovered {0}", args.Name);

            var Node = args.Node;
            Node.DataReceived += (node, ev) => Console.WriteLine("Received: {0}", BitConverter.ToString(ev.Data));
        };

        try
        {
            await controller.OpenAsync();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.WriteLine("Succes!");

        await controller.DiscoverNetworkAsync();
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Press Enter to connect to Coordinator.");
        Console.ReadLine();

        ConnectToCoordinator();

        Console.ReadLine();
    }
}

}

My thanks in advance!

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/jefffhaynes/XBee/issues/24, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJSKR2tIfM4_9uoquXg50mhn5xqHFcjkks5r6fLkgaJpZM4Nc8Ug.

Vladdi00 commented 7 years ago

I just realized it was simply my stupid not-paying-enough-attention mistake. For what I was trying to do, I was subscribing to the event of the sending node.

I wanted to first go through the sample code you provided in the readme. Specifically:

// register callback for sample recieved from this node // TODO: in practice you would want to make sure you only subscribe once (or better yet use Rx) args.Node.SampleReceived += (node, sample) => Console.WriteLine("Sample recieved: {0}", sample);

I followed this, I just read "sample received from this node" (and "data received from this node" on the DataReceived method description), and assumed this is what I needed. Now I also understand a bit better how to use it.

I'm glad I (hopefully) didn't waste much of your time.

jefffhaynes commented 7 years ago

No problem, I just sat down to look at this. I'm not sure I totally understand though - your original code actually looks correct to me. What am I missing? Is it that you were subscribing to DataReceived vs SampleReceived and you were sending a sample?

Edit: Because you do want to subscribe to the sending node.

Vladdi00 commented 7 years ago

I think I still have a bit of reading to do on how I can configure XBee devices to communicate.

Firstly, I just realized I actually had my "end-point" device programmed as a router, as I've used it before for a different project. Though if I recall correctly, there wasn't significant difference between a router and an end-device for it to matter to me, but now I'm having trouble discovering the device if I update it to actual end-device firmware. So I left it as a router device for now.

The microcontroller on the router device is sending serial data directly to the coordinator via an Explicit RX Indicator type frame, so I think that's why, I just realized, I don't even need to discover the network, this just works:

class Program
    {
        private static async void ConnectToCoordinator(XBeeController controller)
        {
            try
            {
                await controller.OpenAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            // I don't even need to discover the network to receive the messages I'm currently sending
            //await controller.DiscoverNetworkAsync();
        }

        public static string HexToString(String hexString)
        {
            try
            {
                string ascii = string.Empty;
                string[] split = hexString.Split('-');

                foreach (string hs in split)
                {
                    uint decval = System.Convert.ToUInt32(hs, 16);
                    char character = System.Convert.ToChar(decval);
                    ascii += character;
                }

                return ascii;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }

            return string.Empty;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Press Enter to attempt to connect to the mesh network.");
            Console.ReadLine();

            var controller = new XBeeController("COM3", 9600);

            ConnectToCoordinator(controller);

            controller.DataReceived += (node, ev) =>
            {
                string data = BitConverter.ToString(ev.Data);
                string message = HexToString(data);

                Console.WriteLine("Received: {0}", message);
            };

            Console.WriteLine("Connection succesful!");

            Console.ReadLine();
        }
    }

When getting input samples on a end-device/router node, I would subscribe to that node to use the data on the coordinator. When getting serial data on a end-device/router node, I would subscribe and do things in the same way, correct?

But in my case, I'm not getting serial data on the router node, apparently. The microcontroller attached to the router node sends the data, through the XBee directly to the coordinator, but only the coordinator has its DataReceived event triggered. I'm not sure if this is expected behaviour or not as I'm still learning how XBees work.

jefffhaynes commented 7 years ago

I think that may be a bug. I'll look at it today.


From: Vlad notifications@github.com Sent: Sunday, May 21, 2017 10:13:58 AM To: jefffhaynes/XBee Cc: Jeff Haynes; Comment Subject: Re: [jefffhaynes/XBee] DataReceived event not working? (#24)

I think I still have a bit of reading to do on how I can configure XBee devices to communicate.

Firstly, I just realized I actually had my "end-point" device programmed as a router, as I've used it before for a different project. Though if I recall correctly, there wasn't significant difference between a router and an end-device for it to matter to me, but now I'm having trouble discovering the device if I update it to actual end-device firmware. So I left it as a router device for now.

The microcontroller on the router device is sending serial data directly to the coordinator via an Explicit RX Indicator type frame, so I think that's why, I just realized, I don't even need to discover the network, this just works:

class Program { private static async void ConnectToCoordinator(XBeeController controller) { try { await controller.OpenAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } // I don't even need to discover the network to receive the messages I'm currently sending //await controller.DiscoverNetworkAsync(); }

    public static string HexToString(String hexString)
    {
        try
        {
            string ascii = string.Empty;
            string[] split = hexString.Split('-');

            foreach (string hs in split)
            {
                uint decval = System.Convert.ToUInt32(hs, 16);
                char character = System.Convert.ToChar(decval);
                ascii += character;
            }

            return ascii;
        }
        catch (Exception ex) { Console.WriteLine(ex.Message); }

        return string.Empty;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Press Enter to attempt to connect to the mesh network.");
        Console.ReadLine();

        var controller = new XBeeController("COM3", 9600);

        ConnectToCoordinator(controller);

        controller.DataReceived += (node, ev) =>
        {
            string data = BitConverter.ToString(ev.Data);
            string message = HexToString(data);

            Console.WriteLine("Received: {0}", message);
        };

        Console.WriteLine("Connection succesful!");

        Console.ReadLine();
    }
}

When getting input samples on a end-device/router node, I would subscribe to that node to use the data on the coordinator. When getting serial data on a end-device/router node, I would subscribe and do things in the same way, correct?

But in my case, I'm not getting serial data on the router node, apparently. The microcontroller attached to the router node sends the data, through the XBee directly to the coordinator, but only the coordinator has its DataReceived event triggered. I'm not sure if this is expected behaviour or not as I'm still learning how XBees work.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/jefffhaynes/XBee/issues/24#issuecomment-302939312, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJSKRxyOaR1nxHFFsjMP_ZhOw1C7ZuBhks5r8EamgaJpZM4Nc8Ug.

jefffhaynes commented 7 years ago

I just got around to trying your original code and it works for me.