public override bool Connect() { modusRtu?.ConnectClose(); OperateResult result = modusRtu.ConnectServer(); connectFlag = result.IsSuccess; return(result.IsSuccess); }
public void Connection() { try { busTcpClient?.ConnectClose(); busTcpClient = new ModbusTcpNet(IP, Port, Station); busTcpClient.AddressStartWithZero = true; busTcpClient.IsStringReverse = false; try { OperateResult connect = busTcpClient.ConnectServer(); if (connect.IsSuccess) { IsConnected = true; } else { IsConnected = false; } } catch (Exception ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } } catch (SocketException ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
private void disconnect_btn_Click(object sender, EventArgs e)//modbustcp断开连接 { // 断开连接 DELTARobotClient.ConnectClose(); disconnect_btn.Enabled = false; connect_btn.Enabled = true; }
private void userButton2_Click(object sender, EventArgs e) { // 断开PLC client.ConnectClose( ); userButton1.Enabled = true; userButton2.Enabled = false; }
public void DisConnection() { if (busTcpClient != null) { busTcpClient.ConnectClose(); } }
private void button2_Click(object sender, EventArgs e) { // 断开连接 busTcpClient.ConnectClose( ); button2.Enabled = false; button1.Enabled = true; panel2.Enabled = false; }
private void button2_Click(object sender, EventArgs e) { // 断开连接 busTcpClient.ConnectClose( ); button2.Enabled = false; panel2.Enabled = false; // 通知下线 networkAlien.AlienSessionLoginOut(busTcpClient.AlienSession); }
public void Dispose() { modbusNet.ConnectClose(); _connected = false; foreach (IGroup grp in _grps) { grp.Dispose(); } _grps.Clear(); }
private static void InitModbusTcp() { TCP.ConnectClose(); TCP.IpAddress = _address; TCP.Port = _port; TCP.Station = _station; TCP.ConnectTimeOut = 500; TCP.ReceiveTimeOut = 500; // TCP.ConnectServer(); }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(textBox15.Text, out byte station)) { MessageBox.Show("Station input is wrong!"); return; } busTcpClient?.ConnectClose( ); busTcpClient = new ModbusTcpNet(textBox1.Text, port, station); busTcpClient.AddressStartWithZero = checkBox1.Checked; busTcpClient.SetLoginAccount(textBox14.Text, textBox12.Text); ComboBox1_SelectedIndexChanged(null, new EventArgs( )); // 设置数据服务 busTcpClient.IsStringReverse = checkBox3.Checked; try { OperateResult connect = busTcpClient.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlReadWriteOp1.SetReadWriteNet(busTcpClient, "100", false); } else { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message + Environment.NewLine + "Error: " + connect.ErrorCode); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
/// <summary> /// 停止 /// </summary> public override void DoStop() { try { this.isClose = true; foreach (LD.Config.PlcTypeItem item in this.config.PlcTypeItems) { if (!item.IsActive || !item.IsConnected) { continue; } switch (item.DevType) { case Common.DeviceType.S1200: case Common.DeviceType.S1500: case Common.DeviceType.S200: case Common.DeviceType.S200Smart: case Common.DeviceType.S300: case Common.DeviceType.S400: SiemensS7Net plc = (SiemensS7Net)item.Tag; plc.ConnectClose(); break; case Common.DeviceType.ModbusTcp: ModbusTcpNet mdTcp = (ModbusTcpNet)item.Tag; mdTcp.ConnectClose(); break; case Common.DeviceType.ModbusRtu: ModbusRtu mdRtu = (ModbusRtu)item.Tag; mdRtu.Close(); break; default: break; } item.IsConnected = false; } foreach (LD.Config.PlcDataItem plc in this.config.PlcDataItems) { plc.PropertyChanged -= new PropertyChangedEventHandler(plc_PropertyChanged); plc.PropertyChanged -= new PropertyChangedEventHandler(Ui.frmSiemens.Instance.plc_PropertyChanged); } this.PlcValueChanged -= new LD.Config.PlcValueChangeEventHandler(PlcClient_PlcValueChanged); } catch (Exception ex) { throw new StopException(this.ToString(), ex.ToString()); } }
public void ConnectToPlc() { _deviceDriver?.ConnectClose(); _deviceDriver?.Dispose(); _deviceDriver = new ModbusTcpNet(_device.Ip, _device.Port, _device.Station); _deviceDriver.AddressStartWithZero = true; //首地址从0开始 _deviceDriver.DataFormat = HslCommunication.Core.DataFormat.ABCD; _deviceDriver.IsStringReverse = false; //字符串跌倒 _deviceDriver.SetPersistentConnection(); //OperateResult res= _deviceDriver.ConnectServer(); //if(res.IsSuccess) //{ // _log.WriteLog($"{_device.Name}{_device.Ip}连接成功"); //} //else //{ // _log.WriteLog($"{_device.Name}{_device.Ip}连接失败"); //} //_device.IsConnected = res.IsSuccess; }
public void Disconnection() { try { busTcpClient.ConnectClose(); } catch (SocketException ex) { EventscadaException?.Invoke(this.GetType().Name, ex.Message); } }
public bool Connection() { if (!System.Net.IPAddress.TryParse(IP, out System.Net.IPAddress address)) { EventscadaException?.Invoke(GetType().Name, DemoUtils.IpAddressInputWrong); return(false); } if (!int.TryParse($"{Port}", out int port)) { EventscadaException?.Invoke(GetType().Name, DemoUtils.PortInputWrong); return(false); } try { busTcpClient?.ConnectClose(); busTcpClient = new ModbusTcpNet(IP, Port, Station) { AddressStartWithZero = true, IsStringReverse = false }; try { OperateResult connect = busTcpClient.ConnectServer(); if (connect.IsSuccess) { EventscadaException?.Invoke(GetType().Name, StringResources.Language.ConnectedSuccess); IsConnected = true; } else { EventscadaException?.Invoke(GetType().Name, StringResources.Language.ConnectedFailed); } return(IsConnected); } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); return(IsConnected); } } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); return(IsConnected); } }
public bool Disconnection() { try { busTcpClient.ConnectClose(); return(IsConnected); } catch (Exception ex) { EventscadaException?.Invoke(GetType().Name, ex.Message); return(IsConnected); } }
private void connect_btn_Click(object sender, EventArgs e)//modbustcp连接 { // 连接 if (!System.Net.IPAddress.TryParse(IpAddress.Text, out System.Net.IPAddress address)) { MessageBox.Show(DemoUtils.IpAddressInputWrong); return; } if (!int.TryParse(PortNum.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } if (!byte.TryParse(stationNum.Text, out byte station)) { MessageBox.Show("Station input is wrong!"); return; } DELTARobotClient?.ConnectClose(); DELTARobotClient = new ModbusTcpNet(IpAddress.Text, port, station); DELTARobotClient.AddressStartWithZero = checkBox1.Checked; comboBox1_SelectedIndexChanged(null, new EventArgs()); // 设置数据服务 DELTARobotClient.IsStringReverse = checkBox3.Checked; try { OperateResult connect = DELTARobotClient.ConnectServer(); if (connect.IsSuccess) { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess); disconnect_btn.Enabled = true; connect_btn.Enabled = false; } else { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedFailed + connect.Message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show("Ip地址输入不正确!"); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show("端口输入不正确!"); return; } if (!byte.TryParse(textBox15.Text, out byte station)) { MessageBox.Show("站号输入不正确!"); return; } busTcpClient?.ConnectClose( ); busTcpClient = new ModbusTcpNet(textBox1.Text, port, station); busTcpClient.AddressStartWithZero = checkBox1.Checked; ComboBox1_SelectedIndexChanged(null, new EventArgs( )); // 设置数据服务 busTcpClient.IsStringReverse = checkBox3.Checked; try { OperateResult connect = busTcpClient.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show("连接成功!"); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; } else { MessageBox.Show("连接失败!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public bool Connection() { busTcpClient?.ConnectClose(); busTcpClient = new ModbusTcpNet(adds, port, 1); busTcpClient.AddressStartWithZero = false; OperateResult connect = busTcpClient.ConnectServer(); if (connect.IsSuccess) { return(true); } else { return(false); } }
/// <summary> /// 断开连接 /// </summary> /// <returns></returns> public bool CloseConnect() { if (busTcpClient != null) { // 断开连接 OperateResult connect = busTcpClient.ConnectClose(); if (connect.IsSuccess) { return(true); } else { return(false); } } return(false); }
private void button1_Click(object sender, EventArgs e) { // 连接 if (!System.Net.IPAddress.TryParse(textBox1.Text, out System.Net.IPAddress address)) { MessageBox.Show("Ip地址输入不正确!"); return; } if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show("端口输入不正确!"); return; } if (!byte.TryParse(textBox15.Text, out byte station)) { MessageBox.Show("站号输入不正确!"); return; } busTcpClient?.ConnectClose( ); busTcpClient = new ModbusTcpNet(textBox1.Text, port, station); try { OperateResult connect = busTcpClient.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show("连接成功!"); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; } else { MessageBox.Show("连接失败!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void Btn_disconnect_Click(object sender, EventArgs e) { busTcpClient.ConnectClose(); btn_disconnect.Enabled = false; btn_connect.Enabled = true; // 读线圈部分禁止 tb_coil_address.Enabled = false; tb_coil_length.Enabled = false; rb_coil_one.Enabled = false; rb_coil_two.Enabled = false; rb_coil_four.Enabled = false; btn_coil_send.Enabled = false; // 读寄存器部分禁止 tb_register_address.Enabled = false; tb_register_length.Enabled = false; rb_register_one.Enabled = false; rb_register_two.Enabled = false; rb__register_four.Enabled = false; btn_register_send.Enabled = false; // 读离散禁止 tb_discrete_address.Enabled = false; btn_discrete_send.Enabled = false; rb_discrete_one.Enabled = false; rb_discrete_two.Enabled = false; rb_discrete_four.Enabled = false; tb_discrete_length.Enabled = false; // 写线圈部分禁止 tb_write_coil_address.Enabled = false; tb_write_coil_length.Enabled = false; rb_write_coil_one.Enabled = false; rb_write_coil_two.Enabled = false; rb_write_coil_four.Enabled = false; btn_write_coil_send.Enabled = false; tb_write_coil_data_bit0.Enabled = false; tb_write_coil_data_bit1.Enabled = false; tb_write_coil_data_bit2.Enabled = false; tb_write_coil_data_bit3.Enabled = false; tb_write_coil_data_bit4.Enabled = false; tb_write_coil_data_bit5.Enabled = false; tb_write_coil_data_bit6.Enabled = false; tb_write_coil_data_bit7.Enabled = false; tb_write_coil_data_bit8.Enabled = false; tb_write_coil_data_bit9.Enabled = false; tb_write_coil_data_bit10.Enabled = false; tb_write_coil_data_bit11.Enabled = false; tb_write_coil_data_bit12.Enabled = false; tb_write_coil_data_bit13.Enabled = false; tb_write_coil_data_bit14.Enabled = false; tb_write_coil_data_bit15.Enabled = false; // 写寄存器部分禁止 tb_write_register_address.Enabled = false; btn_write_register_send.Enabled = false; tb_write_register_data.Enabled = false; this.cb_write_register_one.Enabled = false; // 自动发送部分禁止 this.cb_cron_coil.Enabled = false; this.cb_cron_register.Enabled = false; this.cb_cron_discrete.Enabled = false; this.tb_cron_time.Enabled = false; this.btn_cron_start.Enabled = false; this.btn_cron_stop.Enabled = false; }
public static void DisConnnectPlc2() { ModbusTcpNet2.ConnectClose(); }
public void ModbusTcpUnitTest( ) { ModbusTcpNet modbus = new ModbusTcpNet("127.0.0.1", 502, 1); if (!modbus.ConnectServer( ).IsSuccess) { Console.WriteLine("无法连接modbus,将跳过单元测试。等待网络正常时,再进行测试"); return; } // 开始单元测试,从coil类型开始测试 string address = "1200"; bool[] boolTmp = new bool[] { true, true, false, true, false, true, false }; Assert.IsTrue(modbus.WriteCoil(address, true).IsSuccess); Assert.IsTrue(modbus.ReadCoil(address).Content == true); Assert.IsTrue(modbus.WriteCoil(address, boolTmp).IsSuccess); bool[] readBool = modbus.ReadCoil(address, (ushort)boolTmp.Length).Content; for (int i = 0; i < boolTmp.Length; i++) { Assert.IsTrue(readBool[i] == boolTmp[i]); } address = "300"; // short类型 Assert.IsTrue(modbus.Write(address, (short)12345).IsSuccess); Assert.IsTrue(modbus.ReadInt16(address).Content == 12345); short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 }; Assert.IsTrue(modbus.Write(address, shortTmp).IsSuccess); short[] readShort = modbus.ReadInt16(address, (ushort)shortTmp.Length).Content; for (int i = 0; i < readShort.Length; i++) { Assert.IsTrue(readShort[i] == shortTmp[i]); } // ushort类型 Assert.IsTrue(modbus.Write(address, (ushort)51234).IsSuccess); Assert.IsTrue(modbus.ReadUInt16(address).Content == 51234); ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 }; Assert.IsTrue(modbus.Write(address, ushortTmp).IsSuccess); ushort[] readUShort = modbus.ReadUInt16(address, (ushort)ushortTmp.Length).Content; for (int i = 0; i < ushortTmp.Length; i++) { Assert.IsTrue(readUShort[i] == ushortTmp[i]); } // int类型 Assert.IsTrue(modbus.Write(address, 12342323).IsSuccess); Assert.IsTrue(modbus.ReadInt32(address).Content == 12342323); int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 }; Assert.IsTrue(modbus.Write(address, intTmp).IsSuccess); int[] readint = modbus.ReadInt32(address, (ushort)intTmp.Length).Content; for (int i = 0; i < intTmp.Length; i++) { Assert.IsTrue(readint[i] == intTmp[i]); } // uint类型 Assert.IsTrue(modbus.Write(address, (uint)416123237).IsSuccess); Assert.IsTrue(modbus.ReadUInt32(address).Content == (uint)416123237); uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 }; Assert.IsTrue(modbus.Write(address, uintTmp).IsSuccess); uint[] readuint = modbus.ReadUInt32(address, (ushort)uintTmp.Length).Content; for (int i = 0; i < uintTmp.Length; i++) { Assert.IsTrue(readuint[i] == uintTmp[i]); } // float类型 Assert.IsTrue(modbus.Write(address, 123.45f).IsSuccess); Assert.IsTrue(modbus.ReadFloat(address).Content == 123.45f); float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f }; Assert.IsTrue(modbus.Write(address, floatTmp).IsSuccess); float[] readFloat = modbus.ReadFloat(address, (ushort)floatTmp.Length).Content; for (int i = 0; i < readFloat.Length; i++) { Assert.IsTrue(floatTmp[i] == readFloat[i]); } // double类型 Assert.IsTrue(modbus.Write(address, 1234.5434d).IsSuccess); Assert.IsTrue(modbus.ReadDouble(address).Content == 1234.5434d); double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d }; Assert.IsTrue(modbus.Write(address, doubleTmp).IsSuccess); double[] readDouble = modbus.ReadDouble(address, (ushort)doubleTmp.Length).Content; for (int i = 0; i < doubleTmp.Length; i++) { Assert.IsTrue(readDouble[i] == doubleTmp[i]); } // long类型 Assert.IsTrue(modbus.Write(address, 123617231235123L).IsSuccess); Assert.IsTrue(modbus.ReadInt64(address).Content == 123617231235123L); long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 }; Assert.IsTrue(modbus.Write(address, longTmp).IsSuccess); long[] readLong = modbus.ReadInt64(address, (ushort)longTmp.Length).Content; for (int i = 0; i < longTmp.Length; i++) { Assert.IsTrue(readLong[i] == longTmp[i]); } // ulong类型 Assert.IsTrue(modbus.Write(address, 1283823681236123UL).IsSuccess); Assert.IsTrue(modbus.ReadUInt64(address).Content == 1283823681236123UL); ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL }; Assert.IsTrue(modbus.Write(address, ulongTmp).IsSuccess); ulong[] readULong = modbus.ReadUInt64(address, (ushort)ulongTmp.Length).Content; for (int i = 0; i < readULong.Length; i++) { Assert.IsTrue(readULong[i] == ulongTmp[i]); } // string类型 Assert.IsTrue(modbus.Write(address, "123123").IsSuccess); Assert.IsTrue(modbus.ReadString(address, 3).Content == "123123"); // byte类型 byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 }; Assert.IsTrue(modbus.Write(address, byteTmp).IsSuccess); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(modbus.Read(address, 3).Content, byteTmp)); modbus.ConnectClose( ); }
public override void Disconnect() { _modbusDevice.ConnectClose(); }
/// <summary> /// 在关闭的时候需要进行的操作 /// </summary> protected override void AfterClose() { modbusTcp.ConnectClose(); }
public void Close( ) { modbusTcp.ConnectClose( ); }