/// <summary> /// Finite state machine to go through the calibration process having 3 states /// State 0 : Start /// State 1 : Adjust/set zero /// State 2 : Do calibration/Write calibration weight to WTX /// </summary> private void AdjustmentStateMachine() { //Switch depending on the current calibration step described by State switch (_state) { case 0: //start try { _calibrationWeightWithComma = txtCalibrationWeight.Text.Replace(".", ","); // Accept comma and dot _calibrationWeight = double.Parse(_calibrationWeightWithComma); txtCalibrationWeight.Enabled = false; txtInfo.Text = _calibrationWeight.ToString(); } catch (FormatException) { txtInfo.Text = "Wrong format!" + Environment.NewLine + "Accepted format *,***"; break; } catch (OverflowException) { txtInfo.Text = "Overflow!"; break; } txtInfo.Text = "Unload Scale!"; cmdAdjust.Text = "Adjust Zero"; cmdCancel.Text = "<Back"; _state = 1; break; case 1: // Adjust/set zero txtInfo.Text = "Adjusting zero in progess."; Application.DoEvents(); _wtxDevice.AdjustZeroSignal(); txtInfo.Text = "Zero load measured." + Environment.NewLine + "Put weight on scale."; cmdAdjust.Text = "Calibrate"; _state = 2; break; case 2: // Do calibration/Write calibration weight to WTX txtInfo.Text = "Calibration in progress."; Application.DoEvents(); if (_wtxDevice.AdjustNominalSignalWithCalibrationWeight(_calibrationWeight)) { txtInfo.Text = "Calibration finished succesfully"; } else { txtInfo.Text = "Calibration finished incomplete"; } cmdAdjust.Text = "Close"; _state = 3; break; default: //close window _state = 0; Close(); break; } }
} // End method Connect() private static void MenuCases() { // This while loop is repeated till the user enters 'e' (=e meaning exit). After the timer interval the register of the device is read out. // In the while-loop the user can select commands, which are send immediately to the device. while (_valueExitapplication.KeyChar != 'e') { //_isCalibrating = false; _valueOutputwords = Console.ReadKey(); int valueOutput = Convert.ToInt32(_valueOutputwords.KeyChar); switch (_valueOutputwords.KeyChar) { case '0': _wtxDevice.Tare(); break; // Taring case '1': _wtxDevice.SetGross(); break; // Gross/Net case '2': _wtxDevice.Zero(); break; // Zeroing case '3': _wtxDevice.AdjustZeroSignal(); break; // Adjust zero case '4': _wtxDevice.AdjustNominalSignal(); break; // Adjust nominal case '5': _wtxDevice.RecordWeight(); break; // Record Weight // 'c' for writing on multiple registers, which is necessary for the calibration. case 'c': // Calculate Calibration CalculateCalibration(); break; case 'w': // Calculation with weight CalibrationWithWeight(); break; // Change connection from Jetbus to Modbus or from Modbus to Jetbus: case 'j': if (_wtxDevice != null) // Necessary to check if the object of BaseWtDevice have been created and a connection exists. { _wtxDevice.Connection.Disconnect(); _wtxDevice = null; } Thread.Sleep(WAIT_DISCONNECT); // Wait for 2 seconds till the disconnection request is finished. if (connectiontype == ConnectionType.Modbus) { connectiontype = ConnectionType.Jetbus; } else if (connectiontype == ConnectionType.Jetbus) { connectiontype = ConnectionType.Modbus; } InitializeConnection(); break; default: break; } // end switch-case _valueExitapplication = Console.ReadKey(); if (_valueExitapplication.KeyChar == 'e') { break; } } // end while } // end method MenuCases()