/// <summary> /// Wait for the given token or timeout on the given serial port. /// </summary> /// <param name="Port">The port. Must not be null.</param> /// <param name="Token">The token to wait for. Can be null if not waiting for a token.</param> /// <param name="MaxWait">The maximum amount of time to wait for a response, in milliseconds.</param> /// <returns>true if token was received, false if not.</returns> public static bool WaitForToken(TSerialPort Port, string Token, int MaxWait) { string Temp; return(WaitForToken(Port, Token, MaxWait, out Temp)); }
/// <summary> /// Wait for the given token or timeout on the given serial port /// </summary> /// <param name="Port">The port. Must not be null.</param> /// <param name="Token">The token to wait for. Can be null if not waiting for a token.</param> /// <param name="MaxWait">The maximum amount of time to wait for a response, in milliseconds.</param> /// <param name="Result">The data returned. Never null.</param> /// <returns>true if token was received, false if not.</returns> public static bool WaitForToken(TSerialPort Port, string Token, int MaxWait, out string Result) { System.Diagnostics.Stopwatch SW = new System.Diagnostics.Stopwatch(); string Temp = ""; int Timeout = Port.ReadTimeout; Port.ReadTimeout = MaxWait; SW.Start(); while (SW.ElapsedMilliseconds < MaxWait) { string x = Port.ReadExisting(); Temp += x; if ((Token != null) && Temp.Contains(Token)) { Port.ReadTimeout = Timeout; Result = Temp; return(true); } System.Threading.Thread.Sleep(50); } Port.ReadTimeout = Timeout; Result = Temp; return(false); }
/// <summary> /// Query a serial port. /// </summary> /// <param name="Port">The port. Must not be null.</param> /// <param name="Query">The query to send, without the terminator. Must not be null.</param> /// <param name="Terminator">The terminator. Can be null if none.</param> /// <param name="MaxWait">The maximum period to wait in milliseconds.</param> /// <returns>The data returned. Never null.</returns> public static string Query(TSerialPort Port, string Query, string Terminator, int MaxWait) { Port.DiscardInBuffer(); Query = Query + (Terminator == null ? "" : Terminator); Port.Write(Query); string Reply; WaitForToken(Port, Terminator, MaxWait, out Reply); return(Reply); }