private ResponseExecCommand ExecCommand(string command, IEnumerable <ResponseCode> responses) { var culture = Application.CurrentCulture; _port.DiscardOutBuffer(); _port.DiscardInBuffer(); _receiveNow.Reset(); _port.Write(command + "\r"); // <CR> - should be supplied for the command to be executed Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_EXECUTED", culture), command)); Thread.Sleep(TimeoutCommand); var buffer = _port.ReadExisting(); Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_RESPONSE_AT", Application.CurrentCulture), buffer)); var res = GetResponse(buffer, responses); if (res == null) { if (String.IsNullOrEmpty(buffer)) { if (responses.Any(r => r == ResponseCode.NO_ANSWER_MODEM)) { res = new ResponseExecCommand(ResponseCode.NO_ANSWER_MODEM, buffer); } } res = new ResponseExecCommand(ResponseCode.TIMEOUT, buffer); } Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_RESPONSE_CODE", culture), res.Code)); return(res); }
private ResponseExecCommand ExecCommand(string command, int responseTimeout, IEnumerable <ResponseCode> responses) { var culture = Application.CurrentCulture; _port.DiscardOutBuffer(); _port.DiscardInBuffer(); _receiveNow.Reset(); _port.Write(command + "\r"); // <CR> - should be supplied for the command to be executed Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_EXECUTED", culture), command)); var buffer = String.Empty; ResponseExecCommand res; for ( ; ;) { if (_receiveNow.WaitOne(responseTimeout, false)) { // if the current instance receives a signal string read = _port.ReadExisting(); buffer += read; Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_RESPONSE_AT", Application.CurrentCulture), read)); res = GetResponse(buffer, responses); if (res != null) { break; } } else { if (String.IsNullOrEmpty(buffer)) { if (responses.Any(r => r == ResponseCode.NO_ANSWER_MODEM)) { res = new ResponseExecCommand(ResponseCode.NO_ANSWER_MODEM, buffer); break; } } res = new ResponseExecCommand(ResponseCode.TIMEOUT, buffer); break; } } Logger.Write(String.Format(ResourceManagerProvider.GetLocalizedString("MSG_COMMAND_RESPONSE_CODE", culture), res.Code)); return(res); }