Exemple #1
0
        public void ReadExample( )
        {
            #region ReadExample1


            SiemensS7Net siemensTcpNet = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 此处以M100寄存器作为示例
            byte   byte_M100   = siemensTcpNet.ReadByte("M100").Content;       // 读取M100的值
            short  short_M100  = siemensTcpNet.ReadInt16("M100").Content;      // 读取M100-M101组成的字
            ushort ushort_M100 = siemensTcpNet.ReadUInt16("M100").Content;     // 读取M100-M101组成的无符号的值
            int    int_M100    = siemensTcpNet.ReadInt32("M100").Content;      // 读取M100-M103组成的有符号的数据
            uint   uint_M100   = siemensTcpNet.ReadUInt32("M100").Content;     // 读取M100-M103组成的无符号的值
            float  float_M100  = siemensTcpNet.ReadFloat("M100").Content;      // 读取M100-M103组成的单精度值
            long   long_M100   = siemensTcpNet.ReadInt64("M100").Content;      // 读取M100-M107组成的大数据值
            ulong  ulong_M100  = siemensTcpNet.ReadUInt64("M100").Content;     // 读取M100-M107组成的无符号大数据
            double double_M100 = siemensTcpNet.ReadDouble("M100").Content;     // 读取M100-M107组成的双精度值
            string str_M100    = siemensTcpNet.ReadString("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据

            // 读取数组
            short[]  short_M100_array  = siemensTcpNet.ReadInt16("M100", 10).Content;  // 读取M100-M101组成的字
            ushort[] ushort_M100_array = siemensTcpNet.ReadUInt16("M100", 10).Content; // 读取M100-M101组成的无符号的值
            int[]    int_M100_array    = siemensTcpNet.ReadInt32("M100", 10).Content;  // 读取M100-M103组成的有符号的数据
            uint[]   uint_M100_array   = siemensTcpNet.ReadUInt32("M100", 10).Content; // 读取M100-M103组成的无符号的值
            float[]  float_M100_array  = siemensTcpNet.ReadFloat("M100", 10).Content;  // 读取M100-M103组成的单精度值
            long[]   long_M100_array   = siemensTcpNet.ReadInt64("M100", 10).Content;  // 读取M100-M107组成的大数据值
            ulong[]  ulong_M100_array  = siemensTcpNet.ReadUInt64("M100", 10).Content; // 读取M100-M107组成的无符号大数据
            double[] double_M100_array = siemensTcpNet.ReadDouble("M100", 10).Content; // 读取M100-M107组成的双精度值

            #endregion
        }
        private void userButton40_Click(object sender, EventArgs e)
        {
            // 读取操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            bool   M100_7      = siemensTcpNet.ReadBool("M100.7").Content;     // 读取M100.7是否通断
            byte   byte_M100   = siemensTcpNet.ReadByte("M100").Content;       // 读取M100的值
            short  short_M100  = siemensTcpNet.ReadInt16("M100").Content;      // 读取M100-M101组成的字
            ushort ushort_M100 = siemensTcpNet.ReadUInt16("M100").Content;     // 读取M100-M101组成的无符号的值
            int    int_M100    = siemensTcpNet.ReadInt32("M100").Content;      // 读取M100-M103组成的有符号的数据
            uint   uint_M100   = siemensTcpNet.ReadUInt32("M100").Content;     // 读取M100-M103组成的无符号的值
            float  float_M100  = siemensTcpNet.ReadFloat("M100").Content;      // 读取M100-M103组成的单精度值
            long   long_M100   = siemensTcpNet.ReadInt64("M100").Content;      // 读取M100-M107组成的大数据值
            ulong  ulong_M100  = siemensTcpNet.ReadUInt64("M100").Content;     // 读取M100-M107组成的无符号大数据
            double double_M100 = siemensTcpNet.ReadDouble("M100").Content;     // 读取M100-M107组成的双精度值
            string str_M100    = siemensTcpNet.ReadString("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据

            // 写入操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            siemensTcpNet.Write("M100.7", true);            // 写位
            siemensTcpNet.Write("M100", (byte)0x33);        // 写单个字节
            siemensTcpNet.Write("M100", (short)12345);      // 写双字节有符号
            siemensTcpNet.Write("M100", (ushort)45678);     // 写双字节无符号
            siemensTcpNet.Write("M100", 123456789);         // 写双字有符号
            siemensTcpNet.Write("M100", (uint)3456789123);  // 写双字无符号
            siemensTcpNet.Write("M100", 123.456f);          // 写单精度
            siemensTcpNet.Write("M100", 1234556434534545L); // 写大整数有符号
            siemensTcpNet.Write("M100", 523434234234343UL); // 写大整数无符号
            siemensTcpNet.Write("M100", 123.456d);          // 写双精度
            siemensTcpNet.Write("M100", "K123456789");      // 写ASCII字符串
        }
Exemple #3
0
 private void button_read_long_Click(object sender, EventArgs e)
 {
     // 读取long变量
     if (textBox5.Text == "1")
     {
         DemoUtils.ReadResultRender(siemensTcpNet.ReadInt64(textBox3.Text), textBox3.Text, textBox4);
     }
     else
     {
         DemoUtils.ReadResultRender(siemensTcpNet.ReadInt64(textBox3.Text, ushort.Parse(textBox5.Text)), textBox3.Text, textBox4);
     }
 }
Exemple #4
0
        private void Test3()
        {
            // 读取操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            bool   M100_7      = siemensTcpNet.ReadBool("M100.7").Content;     // 读取M100.7是否通断,注意M100.0等同于M100
            byte   byte_M100   = siemensTcpNet.ReadByte("M100").Content;       // 读取M100的值
            short  short_M100  = siemensTcpNet.ReadInt16("M100").Content;      // 读取M100-M101组成的字
            ushort ushort_M100 = siemensTcpNet.ReadUInt16("M100").Content;     // 读取M100-M101组成的无符号的值
            int    int_M100    = siemensTcpNet.ReadInt32("M100").Content;      // 读取M100-M103组成的有符号的数据
            uint   uint_M100   = siemensTcpNet.ReadUInt32("M100").Content;     // 读取M100-M103组成的无符号的值
            float  float_M100  = siemensTcpNet.ReadFloat("M100").Content;      // 读取M100-M103组成的单精度值
            long   long_M100   = siemensTcpNet.ReadInt64("M100").Content;      // 读取M100-M107组成的大数据值
            ulong  ulong_M100  = siemensTcpNet.ReadUInt64("M100").Content;     // 读取M100-M107组成的无符号大数据
            double double_M100 = siemensTcpNet.ReadDouble("M100").Content;     // 读取M100-M107组成的双精度值
            string str_M100    = siemensTcpNet.ReadString("M100", 10).Content; // 读取M100-M109组成的ASCII字符串数据

            // 写入操作,这里的M100可以替换成I100,Q100,DB20.100效果时一样的
            siemensTcpNet.Write("M100.7", true);            // 写位,注意M100.0等同于M100
            siemensTcpNet.Write("M100", (byte)0x33);        // 写单个字节
            siemensTcpNet.Write("M100", (short)12345);      // 写双字节有符号
            siemensTcpNet.Write("M100", (ushort)45678);     // 写双字节无符号
            siemensTcpNet.Write("M100", 123456789);         // 写双字有符号
            siemensTcpNet.Write("M100", (uint)3456789123);  // 写双字无符号
            siemensTcpNet.Write("M100", 123.456f);          // 写单精度
            siemensTcpNet.Write("M100", 1234556434534545L); // 写大整数有符号
            siemensTcpNet.Write("M100", 523434234234343UL); // 写大整数无符号
            siemensTcpNet.Write("M100", 123.456d);          // 写双精度
            siemensTcpNet.Write("M100", "K123456789");      // 写ASCII字符串

            OperateResult <byte[]> read = siemensTcpNet.Read("M100", 10);
            {
                if (read.IsSuccess)
                {
                    byte m100 = read.Content[0];
                    byte m101 = read.Content[1];
                    byte m102 = read.Content[2];
                    byte m103 = read.Content[3];
                    byte m104 = read.Content[4];
                    byte m105 = read.Content[5];
                    byte m106 = read.Content[6];
                    byte m107 = read.Content[7];
                    byte m108 = read.Content[8];
                    byte m109 = read.Content[9];
                }
                else
                {
                    // 发生了异常
                }
            }
        }
        /// <summary>
        /// 读取单个变量,返回结果
        /// </summary>
        /// <param name="type">数据类型</param>
        /// <param name="address">地址</param>
        /// <returns></returns>
        public static object ReadItem(DataType type, string address)
        {
            object result = null;

            switch (type)
            {
            case DataType.Bool:
                result = siemensS7Net.ReadBool(address).Content;
                break;

            case DataType.Byte:
                result = siemensS7Net.ReadByte(address).Content;
                break;

            case DataType.Int16:
                result = siemensS7Net.ReadInt16(address).Content;
                break;

            case DataType.UInt16:
                result = siemensS7Net.ReadUInt16(address).Content;
                break;

            case DataType.Int32:
                result = siemensS7Net.ReadInt32(address).Content;
                break;

            case DataType.UInt32:
                result = siemensS7Net.ReadUInt32(address).Content;
                break;

            case DataType.Float:
                result = siemensS7Net.ReadFloat(address).Content;
                break;

            case DataType.Int64:
                result = siemensS7Net.ReadInt64(address).Content;
                break;

            case DataType.UInt64:
                result = siemensS7Net.ReadUInt64(address).Content;
                break;

            case DataType.Double:
                result = siemensS7Net.ReadDouble(address).Content;
                break;
            }
            return(result);
        }
Exemple #6
0
 private void button_read_long_Click(object sender, EventArgs e)
 {
     // 读取long变量
     readResultRender(siemensTcpNet.ReadInt64(textBox3.Text), textBox3.Text, textBox4);
 }
        public void MelsecUnitTest( )
        {
            SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1200, "192.168.8.12");

            if (!plc.ConnectServer( ).IsSuccess)
            {
                Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试");
                return;
            }

            // 开始单元测试,从bool类型开始测试
            string address = "M200.4";

            Assert.IsTrue(plc.Write(address, true).IsSuccess);
            Assert.IsTrue(plc.ReadBool(address).Content == true);

            address = "M300";
            // 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, 6).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, 6).Content, byteTmp));

            plc.ConnectClose( );
        }
Exemple #8
0
        /// <param name="vs"></param>
        /// <summary>
        /// 读取指定块的值
        /// </summary>
        public object Read(int index)
        {
            try
            {
                object result;
                var    item = ListItem[index];
                var    arr  = item.Trim().Split('.');
                if (arr.Length > 1)
                {
                    string types = Regex.Replace(arr[1], "[0-9]", "", RegexOptions.IgnoreCase).Trim();//获取地址块的类型
                    switch (types.ToLower())
                    {
                    case "bool":
                        result = SiemensTcpNet.ReadBool(GetNewItem(item)).Content;
                        break;

                    case "byte":
                        result = SiemensTcpNet.ReadByte(GetNewItem(item)).Content;
                        break;

                    case "w":
                        result = SiemensTcpNet.ReadInt16(GetNewItem(item)).Content;
                        break;

                    case "ushort":    //ushort
                        result = SiemensTcpNet.ReadUInt16(GetNewItem(item)).Content;
                        break;

                    case "dint":
                        result = SiemensTcpNet.ReadInt32(GetNewItem(item)).Content;
                        break;

                    case "uint":
                        result = SiemensTcpNet.ReadUInt32(GetNewItem(item)).Content;
                        break;

                    case "long":
                        result = SiemensTcpNet.ReadInt64(GetNewItem(item)).Content;
                        break;

                    case "ulong":
                        result = SiemensTcpNet.ReadUInt64(GetNewItem(item)).Content;
                        break;

                    case "real":
                        result = SiemensTcpNet.ReadFloat(GetNewItem(item)).Content;
                        break;

                    case "double":
                        result = SiemensTcpNet.ReadDouble(GetNewItem(item)).Content;
                        break;

                    case "string":
                        result = SiemensTcpNet.ReadString(GetNewItem(item), 10).Content;
                        break;

                    default:
                        result = -1;
                        break;
                    }
                    return(result);
                }

                return(-1);
            }
            catch (Exception)
            {
                return(-1);
            }
        } /// <summary>
Exemple #9
0
        public async Task SiemensUnitTest( )
        {
            SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1200, "192.168.8.12");  // "192.168.8.12"

            if (!plc.ConnectServer( ).IsSuccess)
            {
                Console.WriteLine("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试");
                return;
            }

            // 开始单元测试,从bool类型开始测试
            string address = "M200.4";

            Assert.IsTrue(plc.Write(address, true).IsSuccess);
            Assert.IsTrue(plc.ReadBool(address).Content == true);

            address = "M300";
            // 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]);
            }

            // 异步short类型
            for (int j = 0; j < 100; j++)
            {
                Assert.IsTrue((await plc.WriteAsync(address, (short)12345)).IsSuccess);
                Assert.IsTrue((await plc.ReadInt16Async(address)).Content == 12345);
                Assert.IsTrue((await plc.WriteAsync(address, shortTmp)).IsSuccess);
                readShort = (await plc.ReadInt16Async(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).Content == "123123");

            // 中文,编码可以自定义
            Assert.IsTrue(plc.Write(address, "测试信息123", Encoding.Unicode).IsSuccess);
            Assert.IsTrue(plc.ReadString(address, 14, Encoding.Unicode).Content == "测试信息123");

            // 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, 6).Content, byteTmp));

            // 批量写入测试
            short[] shortValues = new short[50];
            for (int i = 0; i < 50; i++)
            {
                shortValues[i] = (short)(i * 5 - 3);
            }
            Assert.IsTrue(plc.Write("M300", shortValues).IsSuccess);

            string[] addresses = new string[50];
            ushort[] lengths   = new ushort[50];

            for (int i = 0; i < 50; i++)
            {
                addresses[i] = "M" + (i * 2 + 300);
                lengths[i]   = 2;
            }
            OperateResult <byte[]> readBytes = plc.Read(addresses, lengths);

            Assert.IsTrue(readBytes.IsSuccess);
            Assert.IsTrue(readBytes.Content.Length == 100);
            for (int i = 0; i < 50; i++)
            {
                short shortTmp1 = plc.ByteTransform.TransInt16(readBytes.Content, i * 2);
                Assert.IsTrue(shortValues[i] == shortTmp1);
            }

            // 自定义类的测试
            DataTest test = new DataTest( )
            {
                Data1 = 425,
                Data2 = 123.53f,
                Data3 = new byte[] { 2, 4, 6, 8, 100, 123 }
            };

            Assert.IsTrue(plc.Write(test).IsSuccess);
            Assert.IsTrue(plc.ReadInt16("M100").Content == 425);
            Assert.IsTrue(plc.ReadFloat("M200").Content == 123.53f);
            Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read("M300", 6).Content, test.Data3));
            DataTest test1 = plc.Read <DataTest>( ).Content;

            Assert.IsTrue(test1.Data1 == test.Data1);
            Assert.IsTrue(test1.Data2 == test.Data2);
            Assert.IsTrue(SoftBasic.IsTwoBytesEquel(test1.Data3, test.Data3));

            // 大数据写入测试
            Assert.IsTrue(plc.Write("M100", (short)12345).IsSuccess);
            Assert.IsTrue(plc.Write("M500", (short)12345).IsSuccess);
            Assert.IsTrue(plc.Write("M800", (short)12345).IsSuccess);
            OperateResult <short[]> readBatchResult = plc.ReadInt16("M100", 351);

            Assert.IsTrue(readBatchResult.IsSuccess);
            Assert.IsTrue(readBatchResult.Content[0] == 12345);
            Assert.IsTrue(readBatchResult.Content[200] == 12345);
            Assert.IsTrue(readBatchResult.Content[350] == 12345);

            plc.ConnectClose( );
        }
Exemple #10
0
        public override IEnumerable <IOTData> GetData()
        {
            List <IOTData> iOTs = new List <IOTData>();

            foreach (S7NetResult result in DriveConfig.Results)
            {
                try
                {
                    string sResult;
                    switch (result.DataType.ToUpper())
                    {
                    case "BOOL":
                        sResult = siemensTcpNet.ReadBool(result.DB).Content.ToString();
                        break;

                    case "STRING":
                        sResult = siemensTcpNet.ReadString(result.DB, Convert.ToUInt16(result.Len)).Content;
                        break;

                    case "INT":
                        sResult = siemensTcpNet.ReadInt32(result.DB).Content.ToString();
                        break;

                    case "FLOAT":
                        sResult = siemensTcpNet.ReadFloat(result.DB).Content.ToString(result.Format);
                        break;

                    case "DOUBLE":
                        sResult = siemensTcpNet.ReadDouble(result.DB).Content.ToString(result.Format);
                        break;

                    case "BYTE":
                        sResult = siemensTcpNet.ReadByte(result.DB).Content.ToString();
                        break;

                    case "SHORT":
                        sResult = siemensTcpNet.ReadInt16(result.DB).Content.ToString();
                        break;

                    case "USHORT":
                        sResult = siemensTcpNet.ReadUInt16(result.DB).Content.ToString();
                        break;

                    case "UINT":
                        sResult = siemensTcpNet.ReadUInt32(result.DB).Content.ToString();
                        break;

                    case "LONG":
                        sResult = siemensTcpNet.ReadInt64(result.DB).Content.ToString();
                        break;

                    case "ULONG":
                        sResult = siemensTcpNet.ReadUInt64(result.DB).Content.ToString();
                        break;

                    case "DATETIME":
                        sResult = siemensTcpNet.ReadDateTime(result.DB).Content.ToString();
                        break;

                    default:
                        sResult = siemensTcpNet.ReadString(result.DB).Content;
                        break;
                    }
                    iOTs.Add(new IOTData
                    {
                        DataCode  = result.Address,
                        DataValue = sResult,
                        DataName  = result.Name,
                        DriveCode = DriveConfig.DriveCode,
                        DriveType = DriveConfig.DriveType,
                        GTime     = DateTime.Now,
                        Unit      = result.Unit
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            return(iOTs);
        }
Exemple #11
0
        //PLC读取
        private object ReadSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, SiemensS7Net 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.ReadByte(plcdata.Address).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);
        }
Exemple #12
0
 /// <summary>
 /// 读取long变量
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Btn_Read_Long_Click(object sender, EventArgs e)
 {
     ReadResultRender(siemensTcpNet.ReadInt64(txt_ReadSingle_Address.Text), txt_ReadSingle_Address.Text, txt_Result_Single);
 }