private void btnFixedCurrent_Click(object sender, EventArgs e) { decimal current = txtFixedCurrent.DecimalValue; bool ret = HartDevice.SetFixedCurrent((float)current); MessageBox.Show(ret ? "设置成功" : HartDevice.GetLastError(), "消息", MessageBoxButtons.OK, MessageBoxIcon.Question); }
private void WriteTC(byte tag) { float tempAD = 0; float lowerAD = 0; float upperAD = 0; if (!float.TryParse((this.Controls["txtTempAD" + tag.ToString()] as TextBox).Text, out tempAD)) { MessageBox.Show("温度AD不能转化成数值"); (this.Controls["txtTempAD" + tag.ToString()] as TextBox).Focus(); return; } if (!float.TryParse((this.Controls["txtLow" + tag.ToString()] as TextBox).Text, out lowerAD)) { MessageBox.Show("压力零点AD不能转化成数值"); (this.Controls["txtLow" + tag.ToString()] as TextBox).Focus(); return; } if (!float.TryParse((this.Controls["txtUpper" + tag.ToString()] as TextBox).Text, out upperAD)) { MessageBox.Show("压力满度AD不能转化成数值"); (this.Controls["txtUpper" + tag.ToString()] as TextBox).Focus(); return; } TemperatureCompensation tc = new TemperatureCompensation() { TemperatureAD = tempAD, LowerRangeAD = lowerAD, UpperRangeAD = upperAD }; bool ret = HartDevice.WriteTC(tag, tc); MessageBox.Show(ret ? "下载成功" : HartDevice.GetLastError(), "结果", MessageBoxButtons.OK, MessageBoxIcon.Information); }
private void Frm电流校准_FormClosing(object sender, FormClosingEventArgs e) { if (chkExitFixCurrent.Checked) { HartDevice.SetFixedCurrent(0); } }
private void ReadAD_Task() { try { while (_Running) { Thread.Sleep(AppSettings.Current.RealInterval); if (HartDevice != null && HartDevice.IsConnected) { float ad = HartDevice.ReadPVAD(false); this.Invoke((Action)(() => { if (ad > 0) { txtAD.Text = ad.ToString("F0"); } })); } } } catch (ThreadAbortException) { } catch (Exception) { } finally { _TrdReadAD = null; } }
private void ReadAD_Task() { try { while (_Running) { Thread.Sleep(AppSettings.Current.RealInterval); if (HartDevice != null && HartDevice.IsConnected) { byte[] response = HartDevice.ReadCommand(0xAF, _Params); if (response != null && response.Length == 14) { this.Invoke((Action)(() => { txtPresureAD.Text = BitConverter.ToSingle(new byte[] { response[13], response[12], response[11], response[10] }, 0).ToString("F0"); txtTempAD.Text = BitConverter.ToSingle(new byte[] { response[9], response[8], response[7], response[6] }, 0).ToString("F0"); })); } } } } catch (ThreadAbortException) { } catch (Exception) { } finally { _TrdReadAD = null; } }
private void WriteLinearizationItems(List <LinearizationItem> lis) { FrmProcessing frm = new FrmProcessing(); Action action = delegate() { try { for (int i = 0; i < lis.Count; i += 2) { LinearizationItem[] items = new LinearizationItem[2] { lis[i], lis[i + 1] }; bool ret = HartDevice.WriteLinearizationItems(items, (byte)(i / 2)); frm.ShowProgress(string.Empty, (decimal)(i + 2) / lis.Count); Thread.Sleep(100); } frm.ShowProgress(string.Empty, 1); } catch (ThreadAbortException) { } catch (Exception) { } }; Thread t = new Thread(new ThreadStart(action)); t.IsBackground = true; t.Start(); if (frm.ShowDialog() != DialogResult.OK) { t.Abort(); } }
private void ReadTC(byte tag) { TemperatureCompensation tc = HartDevice.ReadTC(tag); (this.Controls["txtTempAD" + tag.ToString()] as TextBox).Text = tc != null?tc.TemperatureAD.ToString() : null; (this.Controls["txtLow" + tag.ToString()] as TextBox).Text = tc != null?tc.LowerRangeAD.ToString() : null; (this.Controls["txtUpper" + tag.ToString()] as TextBox).Text = tc != null?tc.UpperRangeAD.ToString() : null; }
private void ReadLinearizationItems() { FrmProcessing frm = new FrmProcessing(); Action a = delegate() { try { int maxAD = 70000; if (HartDevice != null && HartDevice.IsConnected) { for (int i = 1; i <= 10; i++) { LinearizationItem li = null; for (int j = 0; j < 4; j++) //由于有时会读不到数据,所以每组数据在读取失败时会重新偿试读三次 { li = HartDevice.ReadLinearizationItem((byte)i); if (li != null) { break; } Thread.Sleep(500); } if (li != null && li.SensorAD == maxAD) { break; //后面的都是无效的参数了,不用再继续获取 } this.Invoke((Action)(() => { (this.Controls["txtP" + i.ToString()] as TextBox).Text = li != null ? li.SensorValue.ToString() : null; (this.Controls["txtAD" + i.ToString()] as TextBox).Text = li != null ? li.SensorAD.ToString() : null; })); frm.ShowProgress(string.Empty, (decimal)i / 10); } } frm.ShowProgress(string.Empty, 1); } catch (ThreadAbortException) { } catch (Exception) { } }; Thread t = new Thread(new ThreadStart(a)); t.IsBackground = true; t.Start(); if (frm.ShowDialog() != DialogResult.OK) { t.Abort(); } }
private void btn20_Click(object sender, EventArgs e) { float current = -1; if (!float.TryParse(txt20.Text, out current) || current < 0) { MessageBox.Show("输入的电流不是有效的数字", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } bool ret = HartDevice.TrimDACGain(current); MessageBox.Show(ret ? "设置成功" : HartDevice.GetLastError(), "消息", MessageBoxButtons.OK, MessageBoxIcon.Question); }
private void btnFix20_Click(object sender, EventArgs e) { btn4.Enabled = false; txt4.Enabled = false; bool ret = HartDevice.SetFixedCurrent((float)20.0); txt20.Enabled = ret; btn20.Enabled = ret; if (!ret) { MessageBox.Show(HartDevice.GetLastError(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void ReadData() { if (HartDevice == null || !HartDevice.IsConnected) { return; } btnRead0.Enabled = (HartDevice != null && HartDevice.IsConnected); btnRead1.Enabled = (HartDevice != null && HartDevice.IsConnected); btnRead2.Enabled = (HartDevice != null && HartDevice.IsConnected); btnWrite0.Enabled = (HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice()); btnWrite1.Enabled = (HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice()); btnWrite2.Enabled = (HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice()); }
private void btnCollect_Click(object sender, EventArgs e) { if (HartDevice == null || !HartDevice.IsConnected) { return; } string temp = (sender as Control).Tag.ToString(); int ind = 0; if (int.TryParse(temp, out ind) && ind >= 1 && ind <= 10) { (this.Controls["txtAD" + ind.ToString()] as TextBox).Text = HartDevice.ReadPVAD(false).ToString("F0"); if (ind < 10) { (this.Controls["txtP" + (ind + 1).ToString()] as TextBox).Focus(); } } }
private void btnCollect_Click(object sender, EventArgs e) { if (_LastEnter == null) { return; } object tag = (sender as Control).Tag; if (tag == null) { return; } if (HartDevice != null && HartDevice.IsConnected) { byte[] response = HartDevice.ReadCommand(0xAF, _Params); if (response != null && response.Length == 14) { if (object.ReferenceEquals(_LastEnter, this.Controls["txtTempAD" + tag.ToString()])) { _LastEnter.Text = BitConverter.ToSingle(new byte[] { response[9], response[8], response[7], response[6] }, 0).ToString("F0"); _LastEnter = (this.Controls["txtLow" + tag.ToString()] as TextBox); _LastEnter.Focus(); } else if (object.ReferenceEquals(_LastEnter, this.Controls["txtLow" + tag.ToString()])) { _LastEnter.Text = BitConverter.ToSingle(new byte[] { response[13], response[12], response[11], response[10] }, 0).ToString("F0"); _LastEnter = this.Controls["txtUpper" + tag.ToString()] as TextBox; _LastEnter.Focus(); } else if (object.ReferenceEquals(_LastEnter, this.Controls["txtUpper" + tag.ToString()])) { _LastEnter.Text = BitConverter.ToSingle(new byte[] { response[13], response[12], response[11], response[10] }, 0).ToString("F0"); byte temp = byte.Parse(tag.ToString()); if (temp < 2) //如果没有到未尾 { temp++; _LastEnter = this.Controls["txtTempAD" + temp.ToString()] as TextBox; _LastEnter.Focus(); } } } } }
public void ReadData() { btnFix4.Enabled = HartDevice != null && HartDevice.IsConnected; btnFix20.Enabled = HartDevice != null && HartDevice.IsConnected; btn4.Enabled = HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice(); btn20.Enabled = HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice(); btnFixedCurrent.Enabled = HartDevice != null && HartDevice.IsConnected; }
public void ReadData() { btnRead.Enabled = HartDevice != null && HartDevice.IsConnected; btnWrite.Enabled = HartDevice != null && HartDevice.IsConnected && HartDevice.IsMyDevice(); }