public void ReadExample( ) { #region ReadExample1 MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); // 此处以D寄存器作为示例 short short_D1000 = melsec_net.ReadInt16("D1000").Content; // 读取D1000的short值 ushort ushort_D1000 = melsec_net.ReadUInt16("D1000").Content; // 读取D1000的ushort值 int int_D1000 = melsec_net.ReadInt32("D1000").Content; // 读取D1000-D1001组成的int数据 uint uint_D1000 = melsec_net.ReadUInt32("D1000").Content; // 读取D1000-D1001组成的uint数据 float float_D1000 = melsec_net.ReadFloat("D1000").Content; // 读取D1000-D1001组成的float数据 long long_D1000 = melsec_net.ReadInt64("D1000").Content; // 读取D1000-D1003组成的long数据 ulong ulong_D1000 = melsec_net.ReadUInt64("D1000").Content; // 读取D1000-D1003组成的long数据 double double_D1000 = melsec_net.ReadDouble("D1000").Content; // 读取D1000-D1003组成的double数据 string str_D1000 = melsec_net.ReadString("D1000", 10).Content; // 读取D1000-D1009组成的条码数据 // 读取数组 short[] short_D1000_array = melsec_net.ReadInt16("D1000", 10).Content; // 读取D1000的short值 ushort[] ushort_D1000_array = melsec_net.ReadUInt16("D1000", 10).Content; // 读取D1000的ushort值 int[] int_D1000_array = melsec_net.ReadInt32("D1000", 10).Content; // 读取D1000-D1001组成的int数据 uint[] uint_D1000_array = melsec_net.ReadUInt32("D1000", 10).Content; // 读取D1000-D1001组成的uint数据 float[] float_D1000_array = melsec_net.ReadFloat("D1000", 10).Content; // 读取D1000-D1001组成的float数据 long[] long_D1000_array = melsec_net.ReadInt64("D1000", 10).Content; // 读取D1000-D1003组成的long数据 ulong[] ulong_D1000_array = melsec_net.ReadUInt64("D1000", 10).Content; // 读取D1000-D1003组成的long数据 double[] double_D1000_array = melsec_net.ReadDouble("D1000", 10).Content; // 读取D1000-D1003组成的double数据 #endregion }
public void WriteBool( ) { #region WriteBool MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); // 以下是简单的写入,没有仔细校验的方式 melsec_net.Write("M100", true); melsec_net.Write("M100", new bool[] { true, false, true, false }); // 如果需要判断是否读取成功 OperateResult write1 = melsec_net.Write("M100", true); if (write1.IsSuccess) { // success } else { // failed } OperateResult write2 = melsec_net.Write("M100", new bool[] { true, false, true, false }); if (write2.IsSuccess) { // success } else { // failed } #endregion }
public void WriteExample2( ) { #region WriteExample2 MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); // 拼凑数据,这样的话,一次通讯就完成数据的全部写入 byte[] buffer = new byte[8]; melsec_net.ByteTransform.TransByte((short)1234).CopyTo(buffer, 0); melsec_net.ByteTransform.TransByte((short)2100).CopyTo(buffer, 2); melsec_net.ByteTransform.TransByte(12353423).CopyTo(buffer, 4); OperateResult write = melsec_net.Write("D100", buffer); if (write.IsSuccess) { // success } else { // failed } // 上面的功能等同于三个数据分别写入,下面的性能更差点,因为进行了三次通讯,而且每次还要判断是否写入成功 //melsec_net.Write( "D100", (short)1234 ); //melsec_net.Write( "D100", (short)2100 ); //melsec_net.Write( "D100", 12353423 ); #endregion }
public void WriteExample( ) { #region WriteExample1 MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); // 此处以D寄存器作为示例 melsec_net.Write("D1000", (short)1234); // 写入D1000 short值 ,W3C0,R3C0 效果是一样的 melsec_net.Write("D1000", (ushort)45678); // 写入D1000 ushort值 melsec_net.Write("D1000", 1234566); // 写入D1000 int值 melsec_net.Write("D1000", (uint)1234566); // 写入D1000 uint值 melsec_net.Write("D1000", 123.456f); // 写入D1000 float值 melsec_net.Write("D1000", 123.456d); // 写入D1000 double值 melsec_net.Write("D1000", 123456661235123534L); // 写入D1000 long值 melsec_net.Write("D1000", 523456661235123534UL); // 写入D1000 ulong值 melsec_net.Write("D1000", "K123456789"); // 写入D1000 string值 // 读取数组 melsec_net.Write("D1000", new short[] { 123, 3566, -123 }); // 写入D1000 short值 ,W3C0,R3C0 效果是一样的 melsec_net.Write("D1000", new ushort[] { 12242, 42321, 12323 }); // 写入D1000 ushort值 melsec_net.Write("D1000", new int[] { 1234312312, 12312312, -1237213 }); // 写入D1000 int值 melsec_net.Write("D1000", new uint[] { 523123212, 213, 13123 }); // 写入D1000 uint值 melsec_net.Write("D1000", new float[] { 123.456f, 35.3f, -675.2f }); // 写入D1000 float值 melsec_net.Write("D1000", new double[] { 12343.542312d, 213123.123d, -231232.53432d }); // 写入D1000 double值 melsec_net.Write("D1000", new long[] { 1231231242312, 34312312323214, -1283862312631823 }); // 写入D1000 long值 melsec_net.Write("D1000", new ulong[] { 1231231242312, 34312312323214, 9731283862312631823 }); // 写入D1000 ulong值 #endregion }
/// <summary> /// 实例化一个三菱的设备对象,从配置信息创建 /// </summary> /// <param name="element">配置信息</param> public DeviceMelsecMc(XElement element) { var nodeMelsec = new NodeMelsecMc(); nodeMelsec.LoadByXmlElement(element); LoadRequest(element); if (nodeMelsec.IsBinary) { protocol = 1; melsecMcNet = new MelsecMcNet(nodeMelsec.IpAddress, nodeMelsec.Port); melsecMcNet.NetworkNumber = nodeMelsec.NetworkNumber; melsecMcNet.NetworkStationNumber = nodeMelsec.NetworkStationNumber; melsecMcNet.ConnectTimeOut = nodeMelsec.ConnectTimeOut; ByteTransform = melsecMcNet.ByteTransform; ReadWriteDevice = melsecMcNet; UniqueId = melsecMcNet.ConnectionId; } else { melsecMcAscii = new MelsecMcAsciiNet(nodeMelsec.IpAddress, nodeMelsec.Port); melsecMcAscii.NetworkNumber = nodeMelsec.NetworkNumber; melsecMcAscii.NetworkStationNumber = nodeMelsec.NetworkStationNumber; melsecMcAscii.ConnectTimeOut = nodeMelsec.ConnectTimeOut; ByteTransform = melsecMcAscii.ByteTransform; ReadWriteDevice = melsecMcAscii; UniqueId = melsecMcAscii.ConnectionId; } TypeName = "三菱设备"; }
public void ClassTest( ) { #region Usage // 实例化对象,指定PLC的ip地址和端口号 MelsecMcAsciiNet melsecMc = new MelsecMcAsciiNet("192.168.1.110", 6000); // 举例读取D100的值 short D100 = melsecMc.ReadInt16("D100").Content; #endregion }
static void MelsecTest( ) { MelsecMcAsciiNet melsec = new MelsecMcAsciiNet("192.168.1.192", 6000); HslCommunication.OperateResult <short> read = melsec.ReadInt16("D100"); if (read.IsSuccess) { Console.WriteLine(read.Content); } else { Console.WriteLine(read.Message); } }
public override void DeviceConn(MelsecMcAsciiNetConfig config) { MelsecMcAsciiNet melsecMcAsciiNet = new MelsecMcAsciiNet(); melsecMcAsciiNet.IpAddress = config.IP; melsecMcAsciiNet.Port = config.Port; OperateResult connect = melsecMcAsciiNet.ConnectServer(); NetworkDevice = melsecMcAsciiNet; if (!connect.IsSuccess) { throw new Exception("Connect Failed"); } }
public void ReadBool( ) { #region ReadBool MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); // 以下是简单的读取,没有仔细校验的方式 bool X1 = melsec_net.ReadBool("X1").Content; bool[] X1_10 = melsec_net.ReadBool("X1", 10).Content; // 如果需要判断是否读取成功 OperateResult <bool> R_X1 = melsec_net.ReadBool("X1"); if (R_X1.IsSuccess) { // success bool value = R_X1.Content; } else { // failed } OperateResult <bool[]> R_X1_10 = melsec_net.ReadBool("X1", 10); if (R_X1_10.IsSuccess) { // success bool x1 = R_X1_10.Content[0]; bool x2 = R_X1_10.Content[1]; bool x3 = R_X1_10.Content[2]; bool x4 = R_X1_10.Content[3]; bool x5 = R_X1_10.Content[4]; bool x6 = R_X1_10.Content[5]; bool x7 = R_X1_10.Content[6]; bool x8 = R_X1_10.Content[7]; bool x9 = R_X1_10.Content[8]; bool xa = R_X1_10.Content[9]; } else { // failed } #endregion }
/// <summary> /// 默认的构造方法 /// </summary> /// <param name="dtu"></param> /// <param name="element"></param> public DeviceMelsecAscii(XElement element) { NodeMelsecMc nodeMelsecMc = new NodeMelsecMc( ); nodeMelsecMc.LoadByXmlElement(element); LoadRequest(element); melsecMcNet = new MelsecMcAsciiNet(nodeMelsecMc.IpAddress, nodeMelsecMc.Port); melsecMcNet.NetworkNumber = nodeMelsecMc.NetworkNumber; melsecMcNet.NetworkStationNumber = nodeMelsecMc.NetworkStationNumber; melsecMcNet.ConnectTimeOut = nodeMelsecMc.ConnectTimeOut; ByteTransform = melsecMcNet.ByteTransform; UniqueId = melsecMcNet.ConnectionId; TypeName = "三菱PLC设备"; }
public void ConnectToPlc() { deviceDriver = new MelsecMcAsciiNet(_device.Ip, _device.Port); deviceDriver.ConnectTimeOut = 2000; // 网络连接的超时时间 deviceDriver.NetworkNumber = 0x00; // 网络号 deviceDriver.NetworkStationNumber = 0x00; // 网络站号 deviceDriver.ConnectClose(); 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 ClassTest2( ) { #region Usage2 // 实例化对象,指定PLC的ip地址和端口号 MelsecMcAsciiNet melsecMc = new MelsecMcAsciiNet("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 }
public void ReadExample2( ) { #region ReadExample2 MelsecMcAsciiNet melsec_net = new MelsecMcAsciiNet("192.168.0.100", 6000); OperateResult <byte[]> read = melsec_net.Read("D100", 4); if (read.IsSuccess) { float temp = melsec_net.ByteTransform.TransInt16(read.Content, 0) / 10f; float press = melsec_net.ByteTransform.TransInt16(read.Content, 2) / 100f; int count = melsec_net.ByteTransform.TransInt32(read.Content, 2); // do something } else { // failed } #endregion }
public FormMelsecAscii( ) { InitializeComponent( ); melsec_net = new MelsecMcAsciiNet( ); }
public void MelsecUnitTest( ) { MelsecMcAsciiNet plc = new MelsecMcAsciiNet("192.168.8.14", 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( ); }