public static ApiEnable Parse(IAtCommandResponse cmd) { if (cmd.Command != ApiEnableCommand.command) { throw new ArgumentException("This method is only applicable for the '" + ApiEnableCommand.command + "' command!", "cmd"); } ByteReader br = new ByteReader(cmd.Value, ByteOrder.BigEndian); ApiEnable ae = new ApiEnable(); ae.ReadBytes(br); return(ae); }
/// <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); }