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 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 btnWrite_Click(object sender, EventArgs e) { if (HartDevice == null || !HartDevice.IsConnected) { return; } List <LinearizationItem> lis = new List <LinearizationItem>(); for (int i = 1; i <= 10; i++) { string strP = (this.Controls["txtP" + i.ToString()] as TextBox).Text; string strAD = (this.Controls["txtAD" + i.ToString()] as TextBox).Text; float fp = 0; float fAD = 0; if (!string.IsNullOrEmpty(strP) && !string.IsNullOrEmpty(strAD)) { if (float.TryParse(strP, out fp) && fp >= 0 && float.TryParse(strAD, out fAD) && fAD >= 0) { if (!(fp == 0 && fAD == 0)) //同时为零的点忽略掉 { LinearizationItem li = new LinearizationItem() { SensorValue = fp, SensorAD = fAD }; lis.Add(li); } } } } if (lis.Count <= 1) { MessageBox.Show("至少要设置2个线性化点"); return; } LinearizationItem.FillHeaderAndTail(lis); WriteLinearizationItems(lis); }