public MeasureModeViewModel() { Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us"); ConnectCommand = new ConnectCommand(this); DisConnectCommand = new DisConnectCommand(this); MeasureOffCommand = new MeasureOffCommand(this); MeasureOnCommand = new MeasureOnCommand(this); RunCommand = new RunCommand(this); AvailablePorts = SerialCommunicationSettings.ListOfSerialPorts(); BaudRates = SerialCommunicationSettings.ListOfSerialBaudRates(); CardTypes = XmlFilter.Instance.GetCardTypeNames(); UpdateTimeUI(); UIElementCollectionHelper = new UIElementCollectionHelper(this); UIElementCollectionHelper.UIElementVisibilityUpdater(UIElementStateVariations.ConnectBeforeClick); ReadingSerialState(); ReportDataCollector.InitializeLists(); IsRunningNow = GeneralMessageCollection.IsRunningStateChecker(false); _stopWatchTimeOut = new Stopwatch(); this._udp = new Udp(); }
private void DataRecieved(object sender, SerialDataReceivedEventArgs e) { if (SerialPortManager.Instance.IsOpen) { try { byte received_byte = Convert.ToByte(SerialPortManager.Instance.ReadByte()); _receivedBytes[this.countBytes] = received_byte; string incomingByte = received_byte.ToString(); ByteMessageBuilderRepository.SetStrArrayByIndex(ByteMessages.Instance.MeasureModeIncoming, countBytes, incomingByte); //all bytes arrived if (countBytes == 2) { countBytes = 0; if (WasItRun) { IsRunningNow = GeneralMessageCollection.IsRunningStateChecker(true); TimeOutValidator(TimeOutValidatorStates.Reset); if (ValidatorIncomingMessage.ValidationEOF()) { if (XmlFilter.Instance.IsCommonIncluded(SelectedCardType) == true) { _commonMeasures = XmlFilter.Instance.GetNumberOfExpectedMeasureState(XmlFilter.Instance.GetDefaultName()); } else { _commonMeasures = 0; } if (XmlFilter.Instance.GetResponseCommand(this._receivedBytes[0].ToString()) == "VOIP_UDP_Start") { this._udp = new Udp(); this._udp.IsEnabled = true; this._udp.SendBytes.Add(_receivedBytes[1]); } else if (XmlFilter.Instance.GetResponseCommand(this._receivedBytes[0].ToString()) == "VOIP_UDP_Cont") { this._udp.IsEnabled = true; this._udp.SendBytes.Add(this._receivedBytes[1]); } else if (XmlFilter.Instance.GetResponseCommand(_receivedBytes[0].ToString()) == "VOIP_UDP_Stop") { this._udp.IsEnabled = true; CTRL_udpClient.Send(this._udp.SendBytes.ToArray(), this._udp.SendBytes.Count, this._udp.IPADDRESS, this._udp.PORT); this._udp.IsFinished = true; } if (!this._udp.IsEnabled) { //if the incoming messages's number is equle with the required number from XML file if (_incomingMeasureCounter == (XmlFilter.Instance.GetNumberOfExpectedMeasureState(SelectedCardType)) + _commonMeasures) { TimeOutValidator(TimeOutValidatorStates.Stop); MessageRecievedText = GeneralMessageCollection.GeneralMessageRecivedTranslation(" -> Validate OK", true) + MessageRecievedText; _validateFinished = true; GeneralMessageCollection.LoopCounter = 0; } else { if (SelectedMeasureType != "AutoMeasure") { IEnumerable <String> _measureList = XmlFilter.Instance.GetMeasureListByCardTypeWithoutAuto(SelectedCardType); var index = _measureList.ToList().IndexOf(SelectedMeasureType); GeneralMessageCollection.LoopCounter = index; TimeOutValidator(TimeOutValidatorStates.Stop); MessageRecievedText = GeneralMessageCollection.GeneralMessageRecivedTranslation("") + MessageRecievedText; GeneralMessageCollection.LoopCounter = 0; _validateFinished = false; } else { //when it is not validated yet and AutoMeasure. TimeOutValidator(TimeOutValidatorStates.Reset); _incomingMeasureCounter++; MessageRecievedText = GeneralMessageCollection.GeneralMessageRecivedTranslation("", true) + MessageRecievedText; GeneralMessageCollection.LoopCounter++; _validateFinished = false; } } } //if incoming message returns with measure ok or not-> negative logic if (IsMeasureModeIncomingReseted && ValidatorIncomingMessage.ErrorMessageBack(ByteMessages.Instance.MeasureModeIncoming[1]) && !this._udp.IsEnabled) { TimeOutValidator(TimeOutValidatorStates.Stop); _validateFinished = true; GeneralMessageCollection.LoopCounter = 0; } if (ReportFieldState && !this._udp.IsEnabled) { _savedMeasureCounter++; string reportInsertData = IsMeasureModeIncomingReseted ? XmlFilter.Instance.GetResponseData( ConverterRepository.ConvertDecimalStringToHexString(ByteMessages.Instance.MeasureModeIncoming[1].ToString())) : ""; ReportDataCollector.AddToVertical(reportInsertData); if (ReportFieldState && _validateFinished) { ReportDataCollector.AddToVerticalAtIndex(0, SchauerNumber.ToString()); ReportDataCollector.AddVerticalToHorizontal(); ReportDataCollector.CleanerVertical(); PopUpQuestionbox(); } } } else { TimeOutValidator(TimeOutValidatorStates.Stop); _validateFinished = true; GeneralMessageCollection.LoopCounter = 0; MessageRecievedText = GeneralMessageCollection.GeneralMessageRecived("Validate Error -> Wrong EoF") + MessageRecievedText; } } else { MessageRecievedText = "Info: " + DateTime.Now.ToString("HH:mm:ss").ToString() + " -> " + XmlFilter.Instance.GetResponseTranslate (ByteMessages.Instance.MeasureModeIncoming[0].ToString(), ByteMessages.Instance.MeasureModeIncoming[1].ToString(), ByteMessages.Instance.MeasureModeIncoming[2].ToString()) + "\n" + MessageRecievedText + "\n"; } WasItDisconnect(); ByteMessageBuilderRepository.ClearArray(ByteMessages.Instance.MeasureModeIncoming); } else { countBytes++; } if (_validateFinished) { _incomingMeasureCounter = 1; _validateFinished = false; IsRunningNow = GeneralMessageCollection.IsRunningStateChecker(false); } if (this._udp.IsFinished) { this._udp.Reset(); } } catch (Exception ex) { //throw; MessageBox.Show(GeneralMessageCollection.LogIntoFile(ex)); } } }