示例#1
0
        public static void Main()
        {
            var port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
            port.Open();

            var xbee = new XBeeDevice(port, (Cpu.Pin)17, (Cpu.Pin)45);

            Debug.Print("MAC address : " + DumpU64(xbee.GetSerialNumber()));

            Debug.Print("Waiting for association...");
            while (xbee.GetAssociationState() != XBeeDevice.AssociationIndication.Success)
            {
                Thread.Sleep(100);
            }

            Debug.Print("Associated - Sending test packet");
            xbee.Send(0UL, "Hello there coordinator");

            xbee.Dispose();
        }
示例#2
0
        public static async Task <IReadOnlyList <XBeeDevice> > Discover()
        {
            var result = new List <XBeeDevice>();

            string serialSelector = SerialDevice.GetDeviceSelector();
            var    devices        = await DeviceInformation.FindAllAsync(serialSelector);

            if (devices != null && devices.Count > 0)
            {
                foreach (var device in devices)
                {
                    var        baudrates = new uint[] { 9600, 19200, 57600, 115200 };
                    bool       success   = false;
                    XBeeDevice candidate = null;
                    var        serport   = await SerialDevice.FromIdAsync(device.Id);

                    if (serport != null)
                    {
                        serport.IsDataTerminalReadyEnabled = true;
                        serport.IsRequestToSendEnabled     = true;
                        serport.DataBits = 8;
                        serport.StopBits = SerialStopBitCount.One;
                        serport.Parity   = SerialParity.None;
                        foreach (var baud in baudrates)
                        {
                            serport.BaudRate = baud;
                            candidate        = new XBeeDevice(serport);
                            try
                            {
                                var hardwareVersion = candidate.GetHardwareVersion();
                                if ((hardwareVersion >> 8) != 0x19 &&
                                    (hardwareVersion >> 8) != 0x1e &&  // undocumented but valid
                                    (hardwareVersion >> 8) != 0x1a)
                                {
                                    break; // unsupported hardware
                                }
                                var firmwareVersion = candidate.GetFirmwareVersion();
                                if (((firmwareVersion >> 8) & 0xf0) != 0x20)
                                {
                                    break; // unsupported firmware
                                }
                                success = true;
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Exception during discovery: " + ex);
                                success = false;
                            }
                            if (success)
                            {
                                break;
                            }
                        }
                    }
                    if (success && candidate != null)
                    {
                        result.Add(candidate);
                    }
                }
            }

            return(result.AsReadOnly());
        }
        public static async Task<IReadOnlyList<XBeeDevice>> Discover()
        {
            var result = new List<XBeeDevice>();

            string serialSelector = SerialDevice.GetDeviceSelector();
            var devices = await DeviceInformation.FindAllAsync(serialSelector);
            if (devices != null && devices.Count > 0)
            {
                foreach (var device in devices)
                {
                    var baudrates = new uint[] { 9600, 19200, 57600, 115200 };
                    bool success = false;
                    XBeeDevice candidate = null;
                    var serport = await SerialDevice.FromIdAsync(device.Id);
                    if (serport != null)
                    {
                        serport.IsDataTerminalReadyEnabled = true;
                        serport.IsRequestToSendEnabled = true;
                        serport.DataBits = 8;
                        serport.StopBits = SerialStopBitCount.One;
                        serport.Parity = SerialParity.None;
                        foreach (var baud in baudrates)
                        {
                            serport.BaudRate = baud;
                            candidate = new XBeeDevice(serport);
                            try
                            {
                                var hardwareVersion = candidate.GetHardwareVersion();
                                if ((hardwareVersion >> 8) != 0x19 &&
                                    (hardwareVersion >> 8) != 0x1e &&  // undocumented but valid
                                    (hardwareVersion >> 8) != 0x1a)
                                    break; // unsupported hardware
                                var firmwareVersion = candidate.GetFirmwareVersion();
                                if (((firmwareVersion >> 8) & 0xf0) != 0x20)
                                    break; // unsupported firmware
                                success = true;
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine("Exception during discovery: " + ex);
                                success = false;
                            }
                            if (success)
                                break;
                        }
                    }
                    if (success && candidate != null)
                    {
                        result.Add(candidate);
                    }
                }
            }

            return result.AsReadOnly();
        }