/// <summary> /// Fetch central state /// </summary> private void GetCentralState() { // get the central state info LICommandAndAnswer centralState = new LICommandAndAnswer(new GetCentralStateInfo()); QueueNewCommand(centralState); CentralStateInfo csi = centralState.Answer as CentralStateInfo; this.EmergencyOff = csi.EmergencyOff; this.EmergencyStop = csi.EmergencyStop; this.StartMode = csi.StartModeString; this.ProgramMode = csi.ProgramMode; this.ColdReset = csi.ColdReset; this.RamCheckError = csi.RamCheckError; string msg = string.Format(i18n.FlakeLIMsgs.CentralStateInfo, ((EmergencyStop) ? (i18n.FlakeLIBase.yes) : (i18n.FlakeLIBase.no)), ((EmergencyOff) ? (i18n.FlakeLIBase.yes) : (i18n.FlakeLIBase.no)), StartMode, ((ProgramMode) ? (i18n.FlakeLIBase.yes) : (i18n.FlakeLIBase.no)), ((ColdReset) ? (i18n.FlakeLIBase.yes) : (i18n.FlakeLIBase.no)), ((RamCheckError) ? (i18n.FlakeLIBase.yes) : (i18n.FlakeLIBase.no))); logme.Log(msg, logme.LogLevel.limsg); }
/// <summary> /// Send command to central and receive answer /// </summary> /// <param name="command"></param> private void SendCommandAndReceiveAnswer(LICommandAndAnswer command) { bool retry = true; while (retry) { SerialPortConnection.Write(command.Command.ByteArray, 0, command.Command.Length); logme.Log(command.Command.LogMessage, logme.LogLevel.limsg, command.Command.ByteArray); logme.AddLine(); Thread.Sleep(Config.Data.TimeToWaitForLIAnswer_ms); // wait for central DateTime tmp = DateTime.Now; while (LastAnswer == null || EmergencyOff) { // wait for answer if (DateTime.Now > tmp.AddSeconds(Config.Data.TimeoutForLIResponse_s)) { // timeout logme.Log(Resources.FlakeLIErrors.LITimeout, logme.LogLevel.error, command.Command.ByteArray); return; } } if (LastAnswer.GetType() == typeof(ErrorUnknown)) { if (_ErrorInARow < Config.Data.AllowedCentralErrorsInARow) { // unknown error, send again, if error sum < n _ErrorInARow++; } else { // Too much errors in a row retry = false; logme.Log(string.Format(Resources.FlakeLIErrors.ErrorSendingCommad, Config.Data.AllowedCentralErrorsInARow.ToString()), logme.LogLevel.error, command.Command.ByteArray); return; } } else { _ErrorInARow = 0; retry = false; } } command.Answer = LastAnswer; // store answer to command for external use LastAnswer = null; logme.Log(command.Answer.LogMessage, logme.LogLevel.limsg, command.Answer.ByteArray); logme.AddLine('='); }
/// <summary> /// Fetch infos from central an dinterface /// </summary>summary> private void GetInterfaceAndCentralInfo() { bool noError = false; int tries = 0; while (noError == false && tries <= Config.Data.CentralFetchInfoTries) { try { tries++; // get the interface info LICommandAndAnswer liVersion = new LICommandAndAnswer(new GetLIUSBVersion()); LICommandAndAnswer liAddress = new LICommandAndAnswer(new GetLIUSBAddress()); QueueNewCommand(liVersion); QueueNewCommand(liAddress); LIVersionInfo livi = liVersion.Answer as LIVersionInfo; LIAddressInfo liadd = liAddress.Answer as LIAddressInfo; logme.Log(string.Format(i18n.FlakeLIMsgs.LIInfoandAddress, livi.LIVersion.ToString("0.0"), livi.LICodenumber.ToString(), liadd.LIAddress.ToString()), logme.LogLevel.limsg); // get the central version info LICommandAndAnswer centralVersion = new LICommandAndAnswer(new GetCentralVersion()); QueueNewCommand(centralVersion); CentralVersionInfo cvi = centralVersion.Answer as CentralVersionInfo; logme.Log(string.Format(i18n.FlakeLIMsgs.CentralVersionInfo, cvi.CentralVersion.ToString("0.0"), cvi.CentralTypeName), logme.LogLevel.limsg); GetCentralState(); noError = true; } catch (Exception ex) { logme.Log(ex); } } if (noError == false && tries > Config.Data.CentralFetchInfoTries) { logme.Log(i18n.FlakeLIWarnings.TooManyTriesFetchCentralInfo, logme.LogLevel.warning); } }
/// <summary> /// Enqueue a new command to the central command queue /// </summary> /// <param name="command">bytearray-format command</param> public void QueueNewCommand(LICommandAndAnswer command) { SendCommandAndReceiveAnswer(command); }