>效果: 核心代码: private void startRead_Click(object sender, EventArgs e) { if (this.startRead.Text=="开始读取") { readmethod = (Readmethod)Enum.Parse(typeof(Readmethod), this.cmb_ReadMethod.Text, true); switch (readmethod) { case Readmethod.订阅读取: Kepgroup.DataChange += Kepgroup_DataChange; break; default: this.updateTimer.Enabled = true; break; } this.startRead.Text = "停止读取"; } else { switch (readmethod) { case Readmethod.订阅读取: Kepgroup.DataChange -= Kepgroup_DataChange; break; default: this.updateTimer.Enabled =false; break; } this.startRead.Text = "开始读取"; } //异步读取 private void Kepgroup_AsyncReadComplete(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps, ref Array Errors) { for (int i = 1; i <= NumItems; i++) { object value = ItemValues.GetValue(i); if (value != null) { int clientHandle =Convert.ToInt32(ClientHandles.GetValue(i)); var query = this.OPCVarList.Where(c => c.ClientHandle == clientHandle).FirstOrDefault(); if (query != null) { query.OPCVaule = value.ToString(); query.OPCDataType = value.GetType().ToString(); query.OPCQuanlity = ((Array)Qualities).GetValue(i).ToString(); query.OPCUpdateTime = Convert.ToDateTime(((Array)TimeStamps).GetValue(i)).ToString(); } } } this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = this.OPCVarList; } //订阅方式读取 private void Kepgroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps) { for (int i = 1; i <= NumItems; i++) { object value = ItemValues.GetValue(i); if (value != null) { int clientHandle = Convert.ToInt32(ClientHandles.GetValue(i)); var query = this.OPCVarList.Where(c => c.ClientHandle == clientHandle).FirstOrDefault(); if (query != null) { query.OPCVaule = value.ToString(); query.OPCDataType = value.GetType().ToString(); query.OPCQuanlity = ((Array)Qualities).GetValue(i).ToString(); query.OPCUpdateTime = Convert.ToDateTime(((Array)TimeStamps).GetValue(i)).ToString(); } } } this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = this.OPCVarList; }
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |