private void timer1_Tick(object sender, EventArgs e) { if (this.server == null) { MessageBox.Show("请先连接OPC服务器"); timer1.Stop(); button2.Text = "开始循环读数"; toolStripStatusLabel2.Text = "Timer:" + timer1.Enabled.ToString(); } else if (dataGridView1.Rows.Count == 0) { MessageBox.Show("请先添加items"); timer1.Stop(); button2.Text = "开始循环读数"; toolStripStatusLabel2.Text = "Timer:" + timer1.Enabled.ToString(); } else { string[] itemID = new String[dataGridView1.Rows.Count]; for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { itemID[i] = dataGridView1.Rows[i].Cells["item"].Value.ToString(); toolStripStatusLabel2.Text = "Timer:" + timer1.Enabled.ToString(); } string[] type; string[] value; OPCOprHelper.GetItems(this.server, itemID, out type, out value); for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { dataGridView1.Rows[i].Cells["type"].Value = type[i]; dataGridView1.Rows[i].Cells["value"].Value = value[i]; dataGridView1.Rows[i].Cells["time"].Value = DateTime.Now.ToString("HH:mm:ss:fff"); } } }
private void 添加ToolStripMenuItem_Click(object sender, EventArgs e) { if (flag == false) { MessageBox.Show("请先点击“读取设定参数”并选中列表中所要查询的参数"); } else { if (listBox1.SelectedItems.Count == 0) { MessageBox.Show("请选取参数列表中所要查询的参数"); } else { string[] itemID = new string[listBox1.SelectedItems.Count]; for (int i = 0; i < listBox1.SelectedItems.Count; i++) { itemID[i] = listBox1.SelectedItems[i].ToString(); } string[] type; string[] value; OPCOprHelper.GetItems(this.server, itemID, out type, out value); for (int i = 0; i < itemID.Length; i++) { AddItem(itemID[i], type[i], value[i]); } } } }
//单Item查询 private void button1_Click_1(object sender, EventArgs e) { string[] itemID = new String[] { textBox2.Text.ToString() }; string type = ""; string value = ""; OPCOprHelper.GetItem(this.server, itemID, out type, out value); textBox3.Text = type; textBox4.Text = value; }
//读取列表中所有items private void button3_Click(object sender, EventArgs e) { if (listBox1.Items.Count == 0) { MessageBox.Show("请先读取设定参数"); } else { string[] itemID = new String[listBox1.Items.Count]; for (int i = 0; i < listBox1.Items.Count; i++) { itemID[i] = listBox1.Items[i].ToString(); } string[] type; string[] value; OPCOprHelper.GetItems(this.server, itemID, out type, out value); for (int i = 0; i < itemID.Length; i++) { AddItem(itemID[i], type[i], value[i]); } } }