private void BtnDeviceCtl_Click(object sender, RoutedEventArgs e) { var device = cmbDevices.SelectedItem as DeviceBase; if (device == null) { return; } PLCHelper plc = null; if (device is StopDevice) { var stop = device as StopDevice; plc?.DisConnect(); plc = new PLCHelper(stop.PLCIP, Convert.ToInt32(txtPort.Text), false); if (!plc.Connect()) { return; } } else if (device is MachineDevice) { var machine = device as MachineDevice; plc?.DisConnect(); plc = new PLCHelper(machine.PLCIP, Convert.ToInt32(txtPort.Text), false); if (!plc.Connect()) { return; } } else { return; } string strCmd = ""; if (chkRead.IsChecked.GetValueOrDefault()) { strCmd = $"RD {cmbDevices.SelectedValue}.U\r"; } else { strCmd = $"WR {cmbDevices.SelectedValue}.U {txtRWValue.Text}\r"; } var rlt = plc.Send(strCmd); if (rlt.HasError) { MessageBox.Show(rlt.ErrorMsg); } else { if (chkRead.IsChecked.GetValueOrDefault()) { txtRWValue.Text = rlt.Text; } } plc.DisConnect(); }
private void BtnConnect_Click(object sender, RoutedEventArgs e) { plcTest?.DisConnect(); plcTest = new PLCHelper(txtIP.Text, Convert.ToInt32(txtPort.Text), true); plcTest.OnShowMsg += AppendText; bool rlt = plcTest.Connect(); if (rlt) { MessageBox.Show("PLC连接成功."); } else { MessageBox.Show("PLC连接失败."); } }