/// <summary> /// Sends a XBeeRequest and waits for the XBeeResponse for specified milliseconds /// </summary> /// <param name="request">The XBeeRequest.</param> /// <param name="timeout">Milliseconds to wait for the response.</param> /// <returns></returns> public XBeeResponse Execute(XBeeRequest request, int timeout) { _waitResponse = true; ExecuteNonQuery(request); if (_apiType == ApiType.Enabled || _apiType == ApiType.EnabledWithEscaped) { int c = 0; while (_waitResponse && ++c <= (timeout / 10)) { Thread.Sleep(10); } if (c > (timeout / 10)) { #if (MF) throw new Exception("Could not receive response."); #else throw new TimeoutException("Could not receive response."); #endif } if (_waitResponse) { return(null); } OnFrameReceived(_receivedPacket); return(_receivedPacket); } else if (_apiType == ApiType.Disabled) { if (ReadResponse() == "OK") { return(null); } // throw new NotImplementedException("This method is not yet implemented."); } throw new NotSupportedException("This ApiType is not supported."); }
/// <summary> /// Sends a XBeeRequest /// </summary> /// <param name="request"></param> /// <returns></returns> public bool ExecuteNonQuery(XBeeRequest request) { if (request is XBeeFrameRequest) { if (_frameID == byte.MaxValue) { _frameID = byte.MinValue; } ((XBeeFrameRequest)request).FrameID = ++_frameID; } byte[] bytes; if (_apiType == ApiType.EnabledWithEscaped) { bytes = request.GetEscapedApiPacket(); } else if (_apiType == ApiType.Enabled) { bytes = request.GetApiPacket(); } else if (_apiType == ApiType.Disabled) { bytes = request.GetAtPacket(); } else { throw new NotSupportedException("This ApiType is not supported."); } #if (LOG && !MF && !WindowsCE) Console.WriteLine(">>\t" + ByteUtil.PrintBytes(bytes, false)); #endif _serialPort.Write(bytes, 0, bytes.Length); return(true); }
/// <summary> /// Sends a XBeeRequest and waits 1000 msec for the XBeeResponse /// </summary> /// <param name="request"></param> /// <returns></returns> /// <exception cref="System.TimoutException">Throws an TimoutException when response could not be read.</exception> public XBeeResponse Execute(XBeeRequest request) { return(Execute(request, 3000)); }
public T Execute <T>(XBeeRequest request, int timeout) where T : XBeeResponse { return((T)Execute(request, timeout)); }