Esempio n. 1
0
        /// <summary>
        /// Reads the node identifier from the module.
        /// </summary>
        /// <returns>A string containing the node identifier; otherwise null.</returns>
        public string GetNodeIdentifier()
        {
            AtCommandResponse res = Execute(new NodeIdentifierCommand()) as AtCommandResponse;

            if (res == null)
            {
                throw new Exception("Could not execute NodeIdentifier command.");
            }

            NodeIdentifier ni = NodeIdentifier.Parse(res);

            if (ni == null)
            {
                throw new Exception("Could not parse response as NodeIdentifier.");
            }

            return(ni.Identifier);
        }
Esempio n. 2
0
        private void CheckFrame(short length, ByteReader br)
        {
            XBeeApiType  apiId = (XBeeApiType)br.Peek();
            XBeeResponse res   = null;

            switch (apiId)
            {
            case XBeeApiType.ZNetExplicitRxIndicator:
                res = new ExplicitZigBeeResponse(length, br);
                break;

            case XBeeApiType.AtCommandResponse:
                res = new AtCommandResponse(length, br);
                break;

            case XBeeApiType.RemoteAtCommandResponse:
                res = new RemoteAtResponse(length, br);
                break;

            case XBeeApiType.ModemStatus:
                res = new ModemStatusResponse(length, br);
                if (res != null)
                {
                    OnModemStatusChanged((res as ModemStatusResponse).ModemStatus);
                }
                break;

            case XBeeApiType.RxPacket16:
                res = new RxResponse16(length, br);
                break;

            case XBeeApiType.RxPacket64:
                res = new RxResponse64(length, br);
                break;

            case XBeeApiType.TxStatus:
                res = new TxStatusResponse(length, br);
                break;

            case XBeeApiType.NodeIdentificationIndicator:
                res = new ZNetNodeIdentificationResponse(length, br);
                break;

            case XBeeApiType.ZNetRxPacket:
                res = new ZNetRxResponse(length, br);
                break;

            case XBeeApiType.XBeeSensorReadIndicator:
                res = new XBeeSensorRead(length, br);
                break;

            case XBeeApiType.ZNetIODataSampleRxIndicator:
                res = new ZNetRxIoSampleResponse(length, br);
                break;

            case XBeeApiType.ZNetTxStatus:
                res = new ZNetTxStatusResponse(length, br);
                break;


            default:
                break;
            }

            if (res != null)
            {
                if (_waitResponse && res is XBeeResponse)
                {
                    if (res is AtCommandResponse && (res as AtCommandResponse).FrameID != _frameID)
                    {
                        return;
                    }

                    _receivedPacket = res;
                    _waitResponse   = false;
                }
                else
                {
                    OnFrameReceived(res);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Opens the connection to the XBee module
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                if (_serialPort == null)
                {
                    _serialPort = new SerialPort(_port, _baudRate);
                }

                if (!_serialPort.IsOpen)
                {
                    _serialPort.ReadTimeout  = 2000;
                    _serialPort.WriteTimeout = 2000;

                    _serialPort.Open();
                }
            }
            catch (Exception ex)
            {
                OnLogEvent(LogEventType.ServerException, ex.ToString());
                return(false);
            }

            if (_apiType == ApiType.Unknown)
            {
                #region Detecting API or transparent AT mode

                try
                {
                    WriteCommand("+++");
                    Thread.Sleep(1025);     // at least one second to wait for OK response

                    if (ReadResponse() == "OK")
                    {
                        _apiType = ApiType.Disabled;

                        // we need some msecs to wait before calling another EnterCommandMode
                        Thread.Sleep(1000);
                    }
                }
                catch (Exception)
                {
                    // seems that we are using API

                    _thd = new Thread(new ThreadStart(this.ReceiveData));
#if (!MF)
                    _thd.Name         = "Receive Data Thread";
                    _thd.IsBackground = true;
#endif
                    _thd.Start();

                    AtCommandResponse at = Execute(new ApiEnableCommand()) as AtCommandResponse;

                    _apiType = ApiEnable.Parse(at).ApiType;
                }

                #endregion
            }
            else if (_apiType == ApiType.Enabled || _apiType == ApiType.EnabledWithEscaped)
            {
                _thd = new Thread(new ThreadStart(this.ReceiveData));
#if (!MF)
                _thd.Name         = "Receive Data Thread";
                _thd.IsBackground = true;
#endif
                _thd.Start();
            }

            if (_apiType == ApiType.Unknown)
            {
                throw new NotSupportedException("The API type could not be read or is configured wrong.");
            }

            return(true);
        }