public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
Thread.Sleep(200);
port.Write(command + "\r");
string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.Contains("\r\nOK\r\n"))))
throw new ApplicationException("No success message was received.");
return input;
}
catch (Exception ex)
{
//int a = ErrorCmd(port);
throw ex;
}
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
receiveNow.Set();
}
}
catch (Exception ex)
{
throw ex;
}
}
public string ReadResponse(SerialPort port, int timeout)
{
string buffer = string.Empty;
try
{
do
{
//Thread.Sleep(5000);
jump: if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length == 0)
{
goto jump;
}
if (buffer.Length > 0)
{
//goto jump;
throw new ApplicationException("Response received is incomplete.");
}
else
throw new ApplicationException("No data received from phone.");
}
}
while (!buffer.Contains("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}
public string ExecCommand(SerialPort port, string command, int responseTimeout, string errorMessage)
{
try
{
port.DiscardOutBuffer();
port.DiscardInBuffer();
receiveNow.Reset();
Thread.Sleep(200);
port.Write(command + "\r");
string input = ReadResponse(port, responseTimeout);
if ((input.Length == 0) || ((!input.EndsWith("\r\n> ")) && (!input.Contains("\r\nOK\r\n"))))
throw new ApplicationException("No success message was received.");
return input;
}
catch (Exception ex)
{
//int a = ErrorCmd(port);
throw ex;
}
}
public void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
if (e.EventType == SerialData.Chars)
{
receiveNow.Set();
}
}
catch (Exception ex)
{
throw ex;
}
}
public string ReadResponse(SerialPort port, int timeout)
{
string buffer = string.Empty;
try
{
do
{
//Thread.Sleep(5000);
jump: if (receiveNow.WaitOne(timeout, false))
{
string t = port.ReadExisting();
buffer += t;
}
else
{
if (buffer.Length == 0)
{
goto jump;
}
if (buffer.Length > 0)
{
//goto jump;
throw new ApplicationException("Response received is incomplete.");
}
else
throw new ApplicationException("No data received from phone.");
}
}
while (!buffer.Contains("\r\nOK\r\n") && !buffer.EndsWith("\r\n> ") && !buffer.EndsWith("\r\nERROR\r\n"));
}
catch (Exception ex)
{
throw ex;
}
return buffer;
}