private void button1_Click(object sender, EventArgs e) { if (btnConnect.Text == "连接") { btnConnect.Enabled = false; if (client == null) { client = new OPCServiceClient.OPCServiceClient(txtServer.Text, int.Parse(txtPort.Text)); } client.Server = txtServer.Text; client.Port = int.Parse(txtPort.Text); client.BadBlockDetected += Client_BadBlockDetected; if (!client.Connect()) { MessageBox.Show("连接到服务器失败!"); btnConnect.Enabled = true; return; } btnConnect.Text = "断开"; txtServer.Enabled = false; txtPort.Enabled = false; btnAddItem.Enabled = true; btnRemoveitem.Enabled = true; timer1.Start(); } else if (btnConnect.Text == "断开") { client.Disconnect(); btnConnect.Text = "连接"; txtServer.Enabled = true; txtPort.Enabled = true; btnAddItem.Enabled = false; btnRemoveitem.Enabled = false; timer1.Stop(); } btnConnect.Enabled = true; }
static void Main(string[] args) { OPCServiceClient.OPCServiceClient client = new OPCServiceClient.OPCServiceClient("127.0.0.1", 9100); client.BadBlockDetected += Client_BadBlockDetected; if (client.Connect()) { client.SetValue("tag1", "1"); string tagValue1 = client.GetValue("tag1"); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("tag2", "20"); dic.Add("tag3", "0"); client.SetValues(dic); dic = client.GetValues(new List <string>() { "tag2", "tag3" }); client.Disconnect(); } }
public void Disconnect() { _client.Disconnect(); picStatus.Image = imageList1.Images["gray.png"]; }