public void ReadInt32Array( ) { #region ReadInt32Array MelsecMcNet melsec_net = new MelsecMcNet("192.168.0.100", 6000); // 以下是简单的读取,没有仔细校验的方式 int[] d100_119 = melsec_net.ReadInt32("D100", 10).Content; // 如果需要判断是否读取成功 OperateResult <int[]> R_d100_119 = melsec_net.ReadInt32("D100", 10); if (R_d100_119.IsSuccess) { // success int value_d100 = R_d100_119.Content[0]; int value_d102 = R_d100_119.Content[1]; int value_d104 = R_d100_119.Content[2]; int value_d106 = R_d100_119.Content[3]; int value_d108 = R_d100_119.Content[4]; int value_d110 = R_d100_119.Content[5]; int value_d112 = R_d100_119.Content[6]; int value_d114 = R_d100_119.Content[7]; int value_d116 = R_d100_119.Content[8]; int value_d118 = R_d100_119.Content[9]; } else { // failed } #endregion }
public void ReadExample( ) { #region ReadExample1 MelsecMcNet melsec_net = new MelsecMcNet("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 }
private void button_read_int_Click(object sender, EventArgs e) { // 读取int变量 if (textBox12.Text == "1") { DemoUtils.ReadResultRender(melsec_net.ReadInt32(textBox3.Text), textBox3.Text, textBox4); } else { DemoUtils.ReadResultRender(melsec_net.ReadInt32(textBox3.Text, ushort.Parse(textBox12.Text)), textBox3.Text, textBox4); } }
/// <summary> /// 读多个双字 /// </summary> /// <param name="address"></param> /// <param name="length"></param> /// <returns></returns> public int[] ReadMultiW(string address, ushort length) { OperateResult <Int32[]> read = melsec_net.ReadInt32(address, length); if (read.IsSuccess) { _Connect = true; return(read.Content); } else { _Connect = false; return(null); } }
private void userButton22_Click(object sender, EventArgs e) { bool[] M100 = melsec_net.ReadBool("M100", 1).Content; // 读取M100是否通,十进制地址 bool[] X1A0 = melsec_net.ReadBool("X1A0", 1).Content; // 读取X1A0是否通,十六进制地址 bool[] Y1A0 = melsec_net.ReadBool("Y1A0", 1).Content; // 读取Y1A0是否通,十六进制地址 bool[] B1A0 = melsec_net.ReadBool("B1A0", 1).Content; // 读取B1A0是否通,十六进制地址 short short_D1000 = melsec_net.ReadInt16("D1000").Content; // 读取D1000的short值 ,W3C0,R3C0 效果是一样的 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组成的条码数据 melsec_net.Write("M100", new bool[] { true }); // 写入M100为通 melsec_net.Write("Y1A0", new bool[] { true }); // 写入Y1A0为通 melsec_net.Write("X1A0", new bool[] { true }); // 写入X1A0为通 melsec_net.Write("B1A0", new bool[] { true }); // 写入B1A0为通 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", "K123456789"); // 写入D1000 string值 }
private void test3( ) { short d100_short = melsec_net.ReadInt16("D100").Content; ushort d100_ushort = melsec_net.ReadUInt16("D100").Content; int d100_int = melsec_net.ReadInt32("D100").Content; uint d100_uint = melsec_net.ReadUInt32("D100").Content; long d100_long = melsec_net.ReadInt64("D100").Content; ulong d100_ulong = melsec_net.ReadUInt64("D100").Content; float d100_float = melsec_net.ReadFloat("D100").Content; double d100_double = melsec_net.ReadDouble("D100").Content; // need to specify the text length string d100_string = melsec_net.ReadString("D100", 10).Content; }
/// <summary> /// 读取32位整数 /// </summary> /// <param name="address">标签变量地址结构</param> /// <returns></returns> public ItemData <int> ReadInt32(DeviceAddress address) { var addr = GetAddress(address); var result = melseNet.ReadInt32(addr); if (!result.IsSuccess) { return(new ItemData <int>(0, 0, QUALITIES.QUALITY_BAD)); } else { return(new ItemData <int>(result.Content, 0, QUALITIES.QUALITY_GOOD)); } }
public void ReadInt32( ) { #region ReadInt32 MelsecMcNet melsec_net = new MelsecMcNet("192.168.0.100", 6000); // 以下是简单的读取,没有仔细校验的方式 int d100 = melsec_net.ReadInt32("D100").Content; // 如果需要判断是否读取成功 OperateResult <int> R_d100 = melsec_net.ReadInt32("D100"); if (R_d100.IsSuccess) { // success int value = R_d100.Content; } else { // failed } #endregion }
private void button_read_int_Click(object sender, EventArgs e) { // 读取int变量 readResultRender(melsec_net.ReadInt32(textBox3.Text), textBox3.Text, textBox4); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); //Console.WriteLine( System.Globalization.CultureInfo.CurrentCulture.ToString() ); //NetSimplifyClient AccountSimplifyClient = new NetSimplifyClient( "127.0.0.1", 23456 ); //OperateResult<NetHandle,string> read = AccountSimplifyClient.ReadCustomerFromServer( 1, "" ); //if (read.IsSuccess) //{ // Console.WriteLine( "Handle:" + read.Content1 ); // Console.WriteLine( read.Content2 ); //} //else //{ // Console.WriteLine( "失败:" + read.Message ); //} //RedisSubscribe subscribe = new RedisSubscribe( "127.0.0.1", 6379, new string[] { "WareHouse:HuiBo" } ); //subscribe.CreatePush( ( m, n ) => //{ // Console.WriteLine( DateTime.Now.ToString() + " Key: " + m ); // Console.WriteLine( n ); //} ); RedisClient redisClient = new RedisClient("127.0.0.1", 6379, ""); redisClient.SetPersistentConnection( ); MelsecMcNet melsecMc = new MelsecMcNet("192.168.8.12", 6001); melsecMc.SetPersistentConnection( ); long countPlcSuccess = 0; long countRedisSuccess = 0; long countPlcFailed = 0; long countRedisFailed = 0; while (true) { System.Threading.Thread.Sleep(300); OperateResult <int> read = melsecMc.ReadInt32("D100"); if (!read.IsSuccess) { countPlcFailed++; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"{DateTime.Now.ToString( )} PLC Read Failed: {read.Message} PLC成功:{countPlcSuccess} PLC失败:{countPlcFailed} Redis成功:{countRedisSuccess} Redis失败:{countRedisFailed}"); continue; } ; countPlcSuccess++; OperateResult write = redisClient.WriteKey("Test", read.Content.ToString( )); if (write.IsSuccess) { Console.ForegroundColor = ConsoleColor.Green; countRedisSuccess++; Console.WriteLine($"{DateTime.Now.ToString( )} 存储redis成功! PLC成功:{countPlcSuccess} PLC失败:{countPlcFailed} Redis成功:{countRedisSuccess} Redis失败:{countRedisFailed}"); } else { Console.ForegroundColor = ConsoleColor.Red; countRedisFailed++; Console.WriteLine($"{DateTime.Now.ToString( )} 存储redis失败!{write.Message} PLC成功:{countPlcSuccess} PLC失败:{countPlcFailed} Redis成功:{countRedisSuccess} Redis失败:{countRedisFailed}"); } } //SimpleHybirdLock hybirdLock = new SimpleHybirdLock( ); //int[] buffer = new int[1000]; //DateTime start = DateTime.Now; //for (int i = 0; i < 1000000; i++) //{ // hybirdLock.Enter( ); // for (int j = 0; j < buffer.Length - 1; j++) // { // buffer[j] = buffer[j + 1]; // } // buffer[999] = i; // hybirdLock.Leave( ); //} //Console.WriteLine( (DateTime.Now - start).TotalMilliseconds ); //start = DateTime.Now; //for (int i = 0; i < 1000000; i++) //{ // hybirdLock.Enter( ); // int[] newbuffer = new int[1000]; // Array.Copy( buffer, 0, newbuffer, 0, 999 ); // newbuffer[999] = i; // buffer = newbuffer; // hybirdLock.Leave( ); //} //Console.WriteLine( (DateTime.Now - start).TotalMilliseconds ); //List<int> list = new List<int>( buffer ); //start = DateTime.Now; //for (int i = 0; i < 1000000; i++) //{ // hybirdLock.Enter( ); // list.Add( i ); // list.RemoveAt( 0 ); // hybirdLock.Leave( ); //} //Console.WriteLine( (DateTime.Now - start).TotalMilliseconds ); //SharpList<int> sharpList = new SharpList<int>( 1000, true ); //start = DateTime.Now; //for (int i = 0; i < 1000000; i++) //{ // sharpList.Add( i ); //} //Console.WriteLine( (DateTime.Now - start).TotalMilliseconds ); //int[] data = sharpList.ToArray( ); //Console.ReadLine( ); }
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( ); }
//PLC读取 private object ReadSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, MelsecMcNet plc) { try { if (plctype == null || !plctype.IsConnected || plcdata == null || plc == null) { return(null); } switch (plcdata.DataType) { case Common.DataTypes.Bool: //Bool plcdata.ValueNew = plc.ReadBool(plcdata.Address).Content; break; case Common.DataTypes.Byte: //Byte plcdata.ValueNew = plc.Read(plcdata.Address, 1).Content; break; case Common.DataTypes.Short: plcdata.ValueNew = plc.ReadInt16(plcdata.Address).Content; break; case Common.DataTypes.Ushort: plcdata.ValueNew = plc.ReadUInt16(plcdata.Address).Content; break; case Common.DataTypes.Int: plcdata.ValueNew = plc.ReadInt32(plcdata.Address).Content; break; case Common.DataTypes.UInt: plcdata.ValueNew = plc.ReadUInt32(plcdata.Address).Content; break; case Common.DataTypes.Long: long lValueNew = 0; if (long.TryParse(plc.ReadInt64(plcdata.Address).Content.ToString(), out lValueNew)) { long temp = BpLong.SwapInt64(lValueNew); plcdata.ValueNew = temp; } // plcdata.ValueNew = plc.ReadInt64(plcdata.Address).Content; break; case Common.DataTypes.ULong: plcdata.ValueNew = plc.ReadUInt64(plcdata.Address).Content; break; case Common.DataTypes.Float: plcdata.ValueNew = plc.ReadFloat(plcdata.Address).Content; break; case Common.DataTypes.Double: plcdata.ValueNew = plc.ReadDouble(plcdata.Address).Content; break; case Common.DataTypes.String: HslCommunication.OperateResult <byte[]> data = (HslCommunication.OperateResult <byte[]>)plc.Read(plcdata.Address, 50); if (data != null && data.Content != null && data.Content.Length > 2) { List <byte> lstData = new List <byte>(); int nLen = data.Content[1]; for (int i = 2; i < nLen + 2; i++) { lstData.Add(data.Content[i]); } plcdata.ValueNew = System.Text.Encoding.ASCII.GetString(lstData.ToArray()); } break; default: break; } } catch { //MessageBox.Show(ex.Message); } return(plcdata.ValueNew); }