private async void button1_Click(object sender, EventArgs e) { melsec_net.IpAddress = textBox1.Text; if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } melsec_net.Port = port; melsec_net.ConnectClose( ); button1.Enabled = false; melsec_net.ConnectTimeOut = 3000; // 连接3秒超时 OperateResult connect = await melsec_net.ConnectServerAsync( ); if (connect.IsSuccess) { MessageBox.Show(HslCommunication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlReadWriteOp1.SetReadWriteNet(melsec_net, "D100", true); } else { MessageBox.Show(connect.Message + Environment.NewLine + "ErrorCode: " + connect.ErrorCode); button1.Enabled = true; } }
/// <summary> /// 连接 /// </summary> /// <returns></returns> public bool Connect() { if (string.IsNullOrEmpty(_ip)) { return(false); } if (melseNet != null) { try { melseNet.ConnectClose(); melseNet.Dispose(); } catch (Exception err) { if (OnError != null) { OnError(this, new IOErrorEventArgs(this.ID, err.Message)); } } } melseNet = new MelsecMcNet(); melseNet.IpAddress = _ip; melseNet.Port = _port; melseNet.ReceiveTimeOut = _timeout; var result = melseNet.ConnectServer(); return(_connected = true); }
public override void Disconnect() { try { PLC.ConnectClose(); } catch { } }
private void BtnConnect_Click(object sender, EventArgs e) { if (!System.Net.IPAddress.TryParse(TxtIPPLC.Text, out System.Net.IPAddress address)) { MessageBox.Show("Wrong IP Address"); return; } melsec_net.IpAddress = TxtIPPLC.Text; if (!int.TryParse(TxtPortPLC.Text, out int port)) { MessageBox.Show("Port PLC Wrong"); return; } melsec_net.Port = port; melsec_net.ReceiveTimeOut = 1000; melsec_net.ConnectTimeOut = 1000; melsec_net.ConnectClose(); try { OperateResult connect = melsec_net.ConnectServer(); if (connect.IsSuccess) { MessageBox.Show("ConnectedSuccess"); TxtIPPLC.Enabled = false; TxtPortPLC.Enabled = false; BtnConnect.Enabled = false; BtnDisconnect.Enabled = true; StartReading(); } else { MessageBox.Show("Connected Failed"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void ConnectCloseExample( ) { MelsecMcNet client = new MelsecMcNet( ); #region ConnectCloseExample client.ConnectClose( ); #endregion }
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; } melsec_net.IpAddress = textBox1.Text; if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show(DemoUtils.PortInputWrong); return; } melsec_net.Port = port; melsec_net.ConnectClose( ); try { OperateResult connect = melsec_net.ConnectServer( ); if (connect.IsSuccess) { MessageBox.Show(Communication.StringResources.Language.ConnectedSuccess); button2.Enabled = true; button1.Enabled = false; panel2.Enabled = true; userControlCurve1.ReadWriteNet = melsec_net; } else { MessageBox.Show(Communication.StringResources.Language.ConnectedFailed); } } 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; } melsec_net.IpAddress = textBox1.Text; if (!int.TryParse(textBox2.Text, out int port)) { MessageBox.Show("端口输入格式不正确!"); return; } melsec_net.Port = port; melsec_net.ConnectClose( ); try { OperateResult connect = melsec_net.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); } }
//MelsecPLC初始化 void DoInitMelsecPlc(LD.Config.PlcTypeItem item) { MelsecMcNet MelsecPlc = new MelsecMcNet(item.IP, item.Port); MelsecPlc.ConnectTimeOut = item.ConnectOutTime; item.Tag = MelsecPlc; if (MelsecPlc != null) { MelsecPlc.ConnectClose(); } OperateResult connect = MelsecPlc.ConnectServer(); if (connect.IsSuccess) { item.IsConnected = true; } else { item.IsConnected = false; } }
public void ClassTest2( ) { #region Usage2 // 实例化对象,指定PLC的ip地址和端口号 MelsecMcNet melsecMc = new MelsecMcNet("192.168.1.110", 6000); // 连接对象 OperateResult connect = melsecMc.ConnectServer( ); if (!connect.IsSuccess) { Console.WriteLine("connect failed:" + connect.Message); return; } // 举例读取D100的值 short D100 = melsecMc.ReadInt16("D100").Content; melsecMc.ConnectClose( ); #endregion }
/// <summary> /// 关闭通信接口 /// </summary> /// <returns></returns> public override bool ClosePLC() { try { //首先关闭实时读取 ParLogicPLC.P_I.BlRead = false; Thread.Sleep(100); OperateResult connect = Melsec_net.ConnectClose(); if (connect.IsSuccess) { return(true); } Log.L_I.WriteError(NameClass + "Error", connect.ErrorCode.ToString() + connect.Message); return(true); } catch (Exception ex) { Log.L_I.WriteError(NameClass, ex); return(false); } }
public void MelsecUnitTest( ) { MelsecMcNet plc = new MelsecMcNet("192.168.8.13", 6001); if (!plc.ConnectServer( ).IsSuccess) { Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试"); return; } // 开始单元测试,从bool类型开始测试 string address = "M200"; bool[] boolTmp = new bool[] { true, true, false, true, false, true, false }; Assert.IsTrue(plc.Write(address, true).IsSuccess); Assert.IsTrue(plc.ReadBool(address).Content == true); Assert.IsTrue(plc.Write(address, boolTmp).IsSuccess); bool[] readBool = plc.ReadBool(address, (ushort)boolTmp.Length).Content; for (int i = 0; i < boolTmp.Length; i++) { Assert.IsTrue(readBool[i] == boolTmp[i]); } address = "D300"; // short类型 Assert.IsTrue(plc.Write(address, (short)12345).IsSuccess); Assert.IsTrue(plc.ReadInt16(address).Content == 12345); short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 }; Assert.IsTrue(plc.Write(address, shortTmp).IsSuccess); short[] readShort = plc.ReadInt16(address, (ushort)shortTmp.Length).Content; for (int i = 0; i < readShort.Length; i++) { Assert.IsTrue(readShort[i] == shortTmp[i]); } // ushort类型 Assert.IsTrue(plc.Write(address, (ushort)51234).IsSuccess); Assert.IsTrue(plc.ReadUInt16(address).Content == 51234); ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 }; Assert.IsTrue(plc.Write(address, ushortTmp).IsSuccess); ushort[] readUShort = plc.ReadUInt16(address, (ushort)ushortTmp.Length).Content; for (int i = 0; i < ushortTmp.Length; i++) { Assert.IsTrue(readUShort[i] == ushortTmp[i]); } // int类型 Assert.IsTrue(plc.Write(address, 12342323).IsSuccess); Assert.IsTrue(plc.ReadInt32(address).Content == 12342323); int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 }; Assert.IsTrue(plc.Write(address, intTmp).IsSuccess); int[] readint = plc.ReadInt32(address, (ushort)intTmp.Length).Content; for (int i = 0; i < intTmp.Length; i++) { Assert.IsTrue(readint[i] == intTmp[i]); } // uint类型 Assert.IsTrue(plc.Write(address, (uint)416123237).IsSuccess); Assert.IsTrue(plc.ReadUInt32(address).Content == (uint)416123237); uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 }; Assert.IsTrue(plc.Write(address, uintTmp).IsSuccess); uint[] readuint = plc.ReadUInt32(address, (ushort)uintTmp.Length).Content; for (int i = 0; i < uintTmp.Length; i++) { Assert.IsTrue(readuint[i] == uintTmp[i]); } // float类型 Assert.IsTrue(plc.Write(address, 123.45f).IsSuccess); Assert.IsTrue(plc.ReadFloat(address).Content == 123.45f); float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f }; Assert.IsTrue(plc.Write(address, floatTmp).IsSuccess); float[] readFloat = plc.ReadFloat(address, (ushort)floatTmp.Length).Content; for (int i = 0; i < readFloat.Length; i++) { Assert.IsTrue(floatTmp[i] == readFloat[i]); } // double类型 Assert.IsTrue(plc.Write(address, 1234.5434d).IsSuccess); Assert.IsTrue(plc.ReadDouble(address).Content == 1234.5434d); double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d }; Assert.IsTrue(plc.Write(address, doubleTmp).IsSuccess); double[] readDouble = plc.ReadDouble(address, (ushort)doubleTmp.Length).Content; for (int i = 0; i < doubleTmp.Length; i++) { Assert.IsTrue(readDouble[i] == doubleTmp[i]); } // long类型 Assert.IsTrue(plc.Write(address, 123617231235123L).IsSuccess); Assert.IsTrue(plc.ReadInt64(address).Content == 123617231235123L); long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 }; Assert.IsTrue(plc.Write(address, longTmp).IsSuccess); long[] readLong = plc.ReadInt64(address, (ushort)longTmp.Length).Content; for (int i = 0; i < longTmp.Length; i++) { Assert.IsTrue(readLong[i] == longTmp[i]); } // ulong类型 Assert.IsTrue(plc.Write(address, 1283823681236123UL).IsSuccess); Assert.IsTrue(plc.ReadUInt64(address).Content == 1283823681236123UL); ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL }; Assert.IsTrue(plc.Write(address, ulongTmp).IsSuccess); ulong[] readULong = plc.ReadUInt64(address, (ushort)ulongTmp.Length).Content; for (int i = 0; i < readULong.Length; i++) { Assert.IsTrue(readULong[i] == ulongTmp[i]); } // string类型 Assert.IsTrue(plc.Write(address, "123123").IsSuccess); Assert.IsTrue(plc.ReadString(address, 3).Content == "123123"); // byte类型 byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 }; Assert.IsTrue(plc.Write(address, byteTmp).IsSuccess); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read(address, 3).Content, byteTmp)); plc.ConnectClose( ); }