internal string ExecCommand(string command, int responseTimeout, string errorMessage) { lock (_execCommandLock) { try { Port.DiscardOutBuffer(); Port.DiscardInBuffer(); ReceiveNow.Reset(); //Port.Write(command + "\r"); Port.WriteLine(command); string input = ReadResponse(Port, responseTimeout); if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.EndsWith("\r\nOK\r\n")))) { throw new ApplicationException("No success message was received."); } return(input); } catch { throw; } } }
private string ReadResponse(SerialPort port, int timeout) { string buffer = string.Empty; try { do { if (ReceiveNow.WaitOne(timeout, false)) { string t = port.ReadExisting(); buffer += t; } else { if (buffer.Length > 0) { throw new ApplicationException("Response received is incomplete."); } else { throw new ApplicationException("No data received from phone."); } } }while (!buffer.EndsWith("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n")); } catch { throw; } return(buffer); }
/// <summary> /// An event handler for receiving data from a serial port /// </summary> /// <param name="sender">The .NET object that sent this event</param> /// <param name="e">Event arguments for this event</param> private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { if (e.EventType == SerialData.Chars) { ReceiveNow.Set(); } } catch { throw; } }