private void WriteIndexes() { if (_currentTabPage == null) { return; } ErrorTextBox.Text = string.Empty; var isError = false; var was = false; if (_changedControls.Count > 0) { was = true; foreach (var c in _changedControls) { var baseIndex = c.Tag as BaseIndex; var v = 0m; if (baseIndex != null) { try { //todo проверка перед сейвом if (c is NumericUpDown) { v = ((NumericUpDown)c).Value; } else if (c is ComboBox) { v = Convert.ToDecimal(((ComboBox)c).SelectedValue); } else if (c is CheckBox) { v = ((CheckBox)c).Checked ? 1 : 0; } ModbusTool.Write(baseIndex, _compressorSetting, v); } catch (Exception ex) { isError = true; ExceptionHelper.ShowException(ErrorTextBox, ex, true, $"{DateTimeHelper.GetNowTime()} Ошибка записи регистра {baseIndex.Address} с функцией {baseIndex.Function} и значением {v}"); } } } ClearChangedControls(); } if (!was) { ErrorTextBox.Text = $"{DateTimeHelper.GetNowTime()} Вы не изменили ни один из регистров \"{_currentTabPage.Text}\""; } else if (!isError) { ErrorTextBox.Text = $"{DateTimeHelper.GetNowTime()} Все измененные регистры \"{_currentTabPage.Text}\" записаны без ошибок"; } }
public void RefreshIndexes() { var defaultValue = "-"; var x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(SpecialFunction.Pressure)); string temp; if (x != null) { var z = ModbusTool.Read(x.Index, CompressorSetting); temp = z.ToString(); CompressorSetting.AddPressure(DateTime.Now, z); } else { temp = defaultValue; } Invoke(() => CompressorData.Pressure = temp); string temp2; x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(SpecialFunction.Temperature)); if (x != null) { var z = ModbusTool.Read(x.Index, CompressorSetting); temp2 = z.ToString(); CompressorSetting.AddTemperature(DateTime.Now, z); } else { temp2 = defaultValue; } Invoke(() => CompressorData.Temperature = temp2); bool?temp3 = null; x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(SpecialFunction.Status)); if (x != null) { temp3 = ModbusTool.Read(x.Index, CompressorSetting) == 1; } else { x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(SpecialFunction.Status0)); if (x != null) { temp3 = ModbusTool.Read(x.Index, CompressorSetting) == 0; } } Invoke(() => CompressorData.InnerStatus = temp3); }
private void button1_Click(object sender, EventArgs e) { byte start; if (!byte.TryParse(StartNumericTextBox.Text, out start)) { start = 1; StartNumericTextBox.Text = start.ToString(); } byte finish; if (!byte.TryParse(FinishTextBox.Text, out finish)) { finish = 255; FinishTextBox.Text = finish.ToString(); } flowLayoutPanel1.Controls.Clear(); var coil = new CoilStatus() { Address = 1 }; for (var i = start; i <= finish && i >= start; i++) { var button = new Button() { Text = i.ToString() }; try { ModbusTool.Read(coil, _setting, i); button.ForeColor = Color.Green; } catch (Exception) { button.ForeColor = Color.Red; } button.Click += (o, args) => { var b = o as Button; if (b != null && b.ForeColor == Color.Green) { Address = Convert.ToByte(b.Text); DialogResult = DialogResult.OK; } }; flowLayoutPanel1.Controls.Add(button); Application.DoEvents(); } }
private void WriteFunction(SpecialFunction function, SpecialFunction function0 = SpecialFunction.None) { var x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(function)); if (x != null) { ModbusTool.Write(x.Index, CompressorSetting, 1); } else if (function0 != SpecialFunction.None) { x = CompressorSetting.Indexes?.SpecialFunctions.FirstOrDefault(u => u.SpecialFunction.HasFlag(function0)); if (x != null) { ModbusTool.Write(x.Index, CompressorSetting, 0); } } }
private void RefreshIndexes() { if (_currentTabPage == null) { _refreshThread = null; return; } var buttons = ButtonsOnCurrentPage; if (buttons != null) { Invoke(() => { foreach (var button in buttons) { button.Enabled = false; } }); } Invoke(() => _currentTabPage.Enabled = false); var isError = false; _stopEvent = true; Invoke(() => ErrorTextBox.Text = $"{DateTimeHelper.GetNowTime()} Чтение регистров \"{_currentTabPage.Text}\""); foreach (var c in _currentTabPage.Controls) { var control = c as Control; var baseIndex = control?.Tag as BaseIndex; if (baseIndex != null) { try { if (_threadAborted) { _threadAborted = false; _refreshThread = null; Invoke(StartRefreshIndexes); return; } var x = ModbusTool.Read(baseIndex, _compressorSetting); if (_threadAborted) { _threadAborted = false; _refreshThread = null; Invoke(StartRefreshIndexes); return; } if (c is CheckBox) { Invoke(() => ((CheckBox)c).Checked = x == 1); } else if (c is TextBox) { Invoke(() => ((TextBox)c).Text = x.ToString()); } else if (c is NumericUpDown) { Invoke(() => { ((NumericUpDown)c).Value = x; ValidateRegister((NumericUpDown)c); }); /* * catch (Exception ex) * { * isError = true; * Invoke(() => ExceptionHelper.ShowException(ErrorTextBox, ex, true, * $"{DateTimeHelper.GetNowTime()} Ошибка чтения регистра {baseIndex.Address} с функцией {baseIndex.Function}, значение {x} не входит в заданные рамки [{((NumericUpDown) c).Minimum} - {((NumericUpDown) c).Maximum}]")); * } */ } else if (c is ComboBox) { var source = ((ComboBox)c).DataSource as List <CustomValue>; Invoke(() => ((ComboBox)c).SelectedValue = x); if (source != null) { Invoke(() => ((ComboBox)c).SelectedItem = source.FirstOrDefault(u => u.Value == x)); } } else { Invoke(() => control.Text = x.ToString()); } } catch (Exception ex) { isError = true; Invoke(() => ExceptionHelper.ShowException(ErrorTextBox, ex, true, $"{DateTimeHelper.GetNowTime()} Ошибка чтения регистра {baseIndex.Address} с функцией {baseIndex.Function}")); } } } _stopEvent = false; if (!isError) { Invoke(() => ErrorTextBox.AppendText($"\r\n{DateTimeHelper.GetNowTime()} Все регистры \"{_currentTabPage.Text}\" прочитаны без ошибок")); } var rButton = RefreshButton; if (rButton != null) { Invoke(() => rButton.Enabled = true); } Invoke(() => _currentTabPage.Enabled = true); /* * if (buttons != null) * Invoke(() => * { * foreach (var button in buttons) * { * button.Enabled = true; * } * }); */ _refreshThread = null; }