Пример #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
        }
Пример #2
0
        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字符串
        }
Пример #3
0
 private void button_read_short_Click(object sender, EventArgs e)
 {
     // 读取short变量
     if (textBox5.Text == "1")
     {
         DemoUtils.ReadResultRender(siemensTcpNet.ReadInt16(textBox3.Text), textBox3.Text, textBox4);
     }
     else
     {
         DemoUtils.ReadResultRender(siemensTcpNet.ReadInt16(textBox3.Text, ushort.Parse(textBox5.Text)), textBox3.Text, textBox4);
     }
 }
Пример #4
0
        private void Test2( )
        {
            byte   m100_byte   = siemensTcpNet.ReadByte("M100").Content;
            short  m100_short  = siemensTcpNet.ReadInt16("M100").Content;
            ushort m100_ushort = siemensTcpNet.ReadUInt16("M100").Content;
            int    m100_int    = siemensTcpNet.ReadInt32("M100").Content;
            uint   m100_uint   = siemensTcpNet.ReadUInt32("M100").Content;
            float  m100_float  = siemensTcpNet.ReadFloat("M100").Content;
            double m100_double = siemensTcpNet.ReadDouble("M100").Content;
            string m100_string = siemensTcpNet.ReadString("M100", 10).Content;

            HslCommunication.Core.IByteTransform ByteTransform = new HslCommunication.Core.ReverseBytesTransform( );
        }
Пример #5
0
        static void Main(string[] args)
        {
            SiemensS7Net plc = new SiemensS7Net(SiemensPLCS.S1200, "127.0.0.1"); // 此处拿了本地虚拟的PLC测试

            plc.SetPersistentConnection( );                                      // 设置了长连接

            MqttServer mqttServer = new MqttServer( );

            mqttServer.RegisterMqttRpcApi("MainPLC", plc);
            mqttServer.ServerStart(1883);

            while (true)
            {
                Thread.Sleep(1000);                   // 每秒读取一次
                OperateResult <short> read = plc.ReadInt16("M100");
                if (read.IsSuccess)
                {
                    // 读取成功后,进行业务处理,存入数据库,或是其他的分析
                    Console.WriteLine("读取成功,M100:" + read.Content);
                }
                else
                {
                    // 读取失败之后,显示下状态
                    Console.WriteLine("读取PLC失败,原因:" + read.Message);
                }
            }
        }
Пример #6
0
        private void BGReadPlc_DoWork(object sender, DoWorkEventArgs e)
        {
            // 读取short变量
            short result = siemensTcpNet.ReadInt16(db).Content;

            if (result == 1)
            {
                SwichPackage();
                siemensTcpNet.Write(db, short.Parse("0"));
            }
        }
Пример #7
0
        public void ClassTest( )
        {
            #region Usage

            // 实例化对象,指定PLC的ip地址
            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");
            // 举例读取M100的值
            short M100 = siemens.ReadInt16("M100").Content;

            #endregion
        }
Пример #8
0
        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                string readaddr = txtReadAddr.Text.Trim();
                if (string.IsNullOrEmpty(readaddr))
                {
                    MessageBox.Show("请输入正确的读取地址");
                    return;
                }

                OperateResult <short[]> opr = melsecMc.ReadInt16(readaddr, 15);
                if (opr.IsSuccess)
                {
                    string codes = "";
                    foreach (short ts in opr.Content.Skip(3))
                    {
                        codes += ts.ToString() + ",";
                    }

                    //物料编码
                    StringBuilder sb1 = new StringBuilder();
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[6 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[6 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[7 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[7 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[8 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[8 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[9 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[9 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[10 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[10 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[11 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[11 - 3])[0]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[12 - 3])[1]));
                    sb1.Append(Chr(BitConverter.GetBytes(opr.Content[12 - 3])[0]));
                    var           s   = sb1.ToString();
                    StringBuilder sb2 = new StringBuilder();
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[13 - 3])[1]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[13 - 3])[0]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[14 - 3])[1]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[14 - 3])[0]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[15 - 3])[1]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[15 - 3])[0]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[16 - 3])[1]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[16 - 3])[0]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[17 - 3])[1]));
                    sb2.Append(Chr(BitConverter.GetBytes(opr.Content[17 - 3])[0]));
                    s = sb2.ToString();
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
Пример #9
0
        public Int16 readOrder(string Address)
        {
            lock (lockObj1)
            {
                //OperateResult<UInt32> result = _SiementsTcpNet.ReadUInt32(Address);
                OperateResult <Int16> result1 = _SiementsTcpNet.ReadInt16(Address);

                //uint readValue = result.Content;

                return(result1.Content);;
            }
        }
Пример #10
0
        /// <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);
        }
Пример #11
0
 private void ReadMethod()
 {
     //SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, "127.0.0.1");
     plc.SetPersistentConnection();         // 设置长连接
     while (true)
     {
         Thread.Sleep(1000);  // 决定了一秒读1次
         if (writeEnable)
         {
             Random rnd    = new Random();
             short  wriNum = (short)rnd.Next();
             // 启动了读操作
             OperateResult write = plc.Write("M100", wriNum);
             // 跨UI更新界面
             Invoke(new Action(() =>
             {
                 if (write.IsSuccess)
                 {
                     textBox2.Text = wriNum.ToString();
                 }
                 else
                 {
                     textBox2.Text = "写入失败";
                 }
             }));
             if (readEnable)
             {
                 // 启动了读操作
                 OperateResult <short> read = plc.ReadInt16("M100");
                 // 跨UI更新界面
                 Invoke(new Action(() =>
                 {
                     if (read.IsSuccess)
                     {
                         textBox1.Text = read.Content.ToString();
                     }
                     else
                     {
                         textBox1.Text = "读取失败";
                     }
                 }));
             }
             //Thread.Sleep(1000);
         }
     }
 }
Пример #12
0
        public void ClassTest2( )
        {
            #region Usage2

            // 实例化对象,指定PLC的ip地址和端口号
            SiemensS7Net siemens = new SiemensS7Net(SiemensPLCS.S1200, " 192.168.1.110");

            // 连接对象
            OperateResult connect = siemens.ConnectServer( );
            if (!connect.IsSuccess)
            {
                Console.WriteLine("connect failed:" + connect.Message);
                return;
            }

            // 举例读取M100的值
            short M100 = siemens.ReadInt16("M100").Content;

            siemens.ConnectClose( );

            #endregion
        }
Пример #13
0
        private void btnReadint_Click(object sender, EventArgs e)
        {
            if (!isConnected)
            {
                MessageBox.Show("还未连接 PLC");
                return;
            }

            try
            {
                OperateResult <short> rlt = plc.ReadInt16($"DB{edtDBNumber.Text}.{edtOffset.Text}");
                if (rlt.IsSuccess)
                {
                    edtText.Text = rlt.Content.ToString();
                }
                else
                {
                    edtText.Text = $"{rlt.ToMessageShowString()}";
                }
            }
            catch { edtText.Text = ""; }
        }
Пример #14
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);
        }
Пример #15
0
 private void button_read_short_Click(object sender, EventArgs e)
 {
     // 读取short变量
     readResultRender(siemensTcpNet.ReadInt16(textBox3.Text), textBox3.Text, textBox4);
 }
Пример #16
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);
        }
Пример #17
0
        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( );
        }
Пример #18
0
 /// <summary>
 /// 读取short变量
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Btn_Read_Short_Click(object sender, EventArgs e)
 {
     ReadResultRender(siemensTcpNet.ReadInt16(txt_ReadSingle_Address.Text), txt_ReadSingle_Address.Text, txt_Result_Single);
 }
Пример #19
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>
Пример #20
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( );
        }
Пример #21
0
        /// <summary>
        /// 轮询
        /// </summary>
        private void Polling()
        {
            //定时轮询PLC
            while (true)
            {
                try
                {
                    //根据PLCCode加载呼叫盒子信息
                    CallBoxInfo CallInfo = CoreData.AllCallBoxes.FirstOrDefault(P => P.CallBoxID == PLCCode);
                    if (CallInfo == null)
                    {
                        LogHelper.WriteCallBoxLog("未匹配到呼叫器:" + PLCCode + "档案信息!");
                        return;
                    }

                    int readLen = CallInfo.ReadLenth;
                    if (readLen == 0)
                    {
                        LogHelper.WriteCallBoxLog("未配置呼叫器:" + PLCCode + "读取长度!");
                        return;
                    }
                    if (CallInfo.CallType == 1)//呼叫类型的
                    {
                        var t = CoreData.AllCallBoxDetail.Where(z => z.CallBoxID == PLCCode).Select(k => k.DBAddress).Distinct();
                        foreach (var address in t)
                        {
                            OperateResult <short[]> read = melsecMc.ReadInt16(address, (ushort)readLen);
                            if (!read.IsSuccess)
                            {
                                LogHelper.WriteCallBoxLog("读取呼叫器失败!  " + string.Format("[{0},{2},{1}]", PLCCode, address, ConnParam.ServerIP));
                                OperateResult opr = melsecMc.ConnectServer();
                                melsecMc.ConnectClose();
                                melsecMc.Dispose();
                                Thread.Sleep(5000);
                                melsecMc = new SiemensS7Net(SiemensPLCS.S1200)
                                {
                                    IpAddress = this.ConnParam.ServerIP, Port = this.ConnParam.Port
                                };
                                melsecMc.SetPersistentConnection();

                                LogHelper.WriteCallBoxLog("重连呼叫器!  " + string.Format("[{0},{2},{1}] ", PLCCode, address, ConnParam.ServerIP));
                                continue;
                            }
                            string SenDLog = "";
                            foreach (short item in read.Content)
                            {
                                SenDLog += ((int)item).ToString("X") + " ";
                            }
                            LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, address, ConnParam.ServerIP) + "内容为:" + SenDLog);
                            int site            = Convert.ToInt32(read.Content[0]);
                            int PlcRequestState = Convert.ToInt32(read.Content[2]);
                            if (PlcRequestState == 1)
                            {
                                int  RequestContent   = Convert.ToInt32(read.Content[1]);
                                var  cdetail          = CoreData.AllCallBoxDetail.FirstOrDefault(p => p.CallBoxID == PLCCode && p.DBAddress == address && p.ButtonID == RequestContent);
                                bool HandleCallResult = HandleCallBoxCall(CallInfo, cdetail, cdetail.ButtonID);
                                if (!HandleCallResult)
                                {
                                    LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "地址 :" + address + " 呼叫失败!");
                                }
                                #region 处理需要回写的子任务
                                IList <DispatchTaskDetail> detaillist = AGVClientDAccess.LoadTaskDetailByPLCCode(PLCCode, cdetail.LocationID);
                                foreach (var detail in detaillist)
                                {
                                    if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)))
                                    {
                                        //写确认信息
                                        List <byte> write = new List <byte>();
                                        //{ BitConverter.GetBytes(read.Content[0]), BitConverter.GetBytes(read.Content[1]) , BitConverter.GetBytes(read.Content[2])}
                                        write.Add(BitConverter.GetBytes(read.Content[0])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[0])[0]);
                                        write.Add(BitConverter.GetBytes(read.Content[1])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[1])[0]);
                                        write.Add(BitConverter.GetBytes(read.Content[2])[1]);
                                        write.Add(BitConverter.GetBytes(read.Content[2])[0]);
                                        OperateResult opr       = melsecMc.Write(address.Replace(".0", ".6"), write.ToArray());
                                        int           writcount = 1;
                                        while (!opr.IsSuccess)
                                        {
                                            if (writcount > 3)
                                            {
                                                opr = melsecMc.Write(address, write.ToArray());
                                                LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "地址 :" + address + "失败!");
                                                break;
                                            }
                                            writcount++;
                                            Thread.Sleep(1000);
                                        }
                                        if (opr.IsSuccess)
                                        {
                                            AGVClientDAccess.UpdatePlcCallBackState(detail.dispatchNo, detail.DetailID);
                                            LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "地址 :" + address + "成功!");
                                        }
                                    }
                                }
                                #endregion

                                #region 处理需要回写物料信息的子任务
                                IList <DispatchTaskDetail> goodsdetaillist = AGVClientDAccess.LoadGoodsInfoTaskDetailByPLCCode(PLCCode, cdetail.LocationID);
                                foreach (var detail in goodsdetaillist)
                                {
                                    if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)) && (!string.IsNullOrEmpty(detail.GoodsInfo)))
                                    {
                                        //写确认信息
                                        List <byte> write = new List <byte>();
                                        string[]    arr   = detail.GoodsInfo.Split(',');
                                        foreach (var s in arr)
                                        {
                                            if (!string.IsNullOrEmpty(s))
                                            {
                                                short ts = short.Parse(s);
                                                write.Add(BitConverter.GetBytes(ts)[1]);
                                                write.Add(BitConverter.GetBytes(ts)[0]);
                                            }
                                        }
                                        OperateResult opr       = melsecMc.Write(address.Replace(".0", ".12"), write.ToArray());
                                        int           writcount = 1;
                                        while (!opr.IsSuccess)
                                        {
                                            if (writcount > 3)
                                            {
                                                opr = melsecMc.Write(address.Replace(".0", ".12"), write.ToArray());
                                                LogHelper.WriteCallBoxLog("回写呼叫器 物料编码:" + PLCCode + "地址 :" + address + "失败!");
                                                break;
                                            }
                                            writcount++;
                                            Thread.Sleep(1000);
                                        }
                                        if (opr.IsSuccess)
                                        {
                                            AGVClientDAccess.UpdateGoodsCallBackState(detail.dispatchNo, detail.DetailID);
                                            LogHelper.WriteCallBoxLog("回写呼叫器 物料编码:" + PLCCode + "地址 :" + address + "成功!");
                                        }
                                    }
                                }
                                #endregion
                            }
                        }

                        #region OlD
                        ////根据当前呼叫器ID加载呼叫器明细
                        //OperateResult<short[]> read = melsecMc.ReadInt16(ReadAddr, (ushort)readLen);
                        //if (!read.IsSuccess)
                        //{
                        //    LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + "失败!");
                        //    return;
                        //}

                        //string SenDLog = "";
                        //foreach (byte item in read.Content)
                        //{ SenDLog += ((int)item).ToString("X") + " "; }
                        //LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + "内容为:" + SenDLog);

                        //int site = Convert.ToInt32(read.Content[0]);
                        //int PlcRequestState = Convert.ToInt32(read.Content[1]);
                        //string[] codes = GetCode(read.Content);
                        //if (codes.Length != 2)
                        //{
                        //    LogHelper.WriteCallBoxLog("读取呼叫器:" + PLCCode + " 物料编码获取失败");
                        //}
                        //if (PlcRequestState == 1)
                        //{
                        //    int RequestContent = Convert.ToInt32(read.Content[2]);
                        //    CallBoxDetail CallDetail = CoreData.AllCallBoxDetail.FirstOrDefault(p => p.CallBoxID == PLCCode && p.ButtonID == RequestContent);
                        //    if (CallDetail == null)
                        //    {
                        //        LogHelper.WriteCallBoxLog("未配置呼叫器:" + PLCCode + "按钮号:" + RequestContent.ToString() + "明细!");
                        //        return;
                        //    }
                        //    bool HandleCallResult = HandleCallBoxCall(CallInfo, CallDetail, RequestContent, codes);

                        //    if (HandleCallResult)
                        //    {
                        //        ////写确认信息
                        //        //byte[] write = new byte[] { (byte)site, 0x00, (byte)RequestContent, (byte)site, 0x01, (byte)RequestContent };
                        //        //OperateResult opr = melsecMc.Write(ReadAddr, write);
                        //        //int writcount = 1;
                        //        //while (!opr.IsSuccess)
                        //        //{
                        //        //    if (writcount > 3)
                        //        //    {
                        //        //        opr = melsecMc.Write(ReadAddr, write);
                        //        //        LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "失败!");
                        //        //        break;
                        //        //    }
                        //        //    writcount++;
                        //        //    Thread.Sleep(1000);
                        //        //}
                        //        //if (opr.IsSuccess)
                        //        //{
                        //        //    LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "成功!");
                        //        //}

                        //        //LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫成功!");
                        //    }
                        //    else
                        //    {
                        //        LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫失败!");
                        //    }


                        //    #region 处理需要回写的子任务
                        //    IList<DispatchTaskDetail> detaillist = AGVClientDAccess.LoadTaskDetailByPLCCode(PLCCode, CallDetail.LocationID);
                        //    foreach (var detail in detaillist)
                        //    {
                        //        if (detail != null && (!string.IsNullOrEmpty(detail.dispatchNo)))
                        //        {
                        //            //写确认信息
                        //            List<byte> write = new List<byte>();
                        //            //{ BitConverter.GetBytes(read.Content[0]), BitConverter.GetBytes(read.Content[1]) , BitConverter.GetBytes(read.Content[2])}
                        //            write.Add(BitConverter.GetBytes(read.Content[0])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[0])[0]);
                        //            write.Add(BitConverter.GetBytes(read.Content[1])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[1])[0]);
                        //            write.Add(BitConverter.GetBytes(read.Content[2])[1]);
                        //            write.Add(BitConverter.GetBytes(read.Content[2])[0]);


                        //            OperateResult opr = melsecMc.Write(ReadAddr.Replace(".0", ".6"), write.ToArray());
                        //            int writcount = 1;
                        //            while (!opr.IsSuccess)
                        //            {
                        //                if (writcount > 3)
                        //                {
                        //                    opr = melsecMc.Write(ReadAddr, write.ToArray());
                        //                    LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "失败!");
                        //                    break;
                        //                }
                        //                writcount++;
                        //                Thread.Sleep(1000);
                        //            }
                        //            if (opr.IsSuccess)
                        //            {
                        //                AGVClientDAccess.UpdatePlcCallBackState(detail.dispatchNo, detail.DetailID);
                        //                LogHelper.WriteCallBoxLog("回写呼叫器:" + PLCCode + "成功!");
                        //            }
                        //            LogHelper.WriteCallBoxLog("呼叫器:" + PLCCode + "呼叫成功!");
                        //        }
                        //    }
                        //    #endregion

                        //}
                        #endregion
                    }
                    else if (CallInfo.CallType == 2)//监控
                    {
                        var t = CoreData.AllCallBoxDetail.Where(p => p.CallBoxID == CallInfo.CallBoxID);
                        foreach (var d in t)
                        {
                            //if (d.DBAddress != "DB1000.0")
                            //{
                            //    continue;
                            //}
                            if (d.LocationState == 0)
                            {
                                //根据当前呼叫器ID加载呼叫器明细
                                OperateResult <short[]> read = melsecMc.ReadInt16(d.DBAddress, 1);
                                if (!read.IsSuccess)
                                {
                                    LogHelper.WriteCallBoxLog("读取呼叫器失败!  " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP));


                                    melsecMc.ConnectClose();
                                    melsecMc.Dispose();
                                    Thread.Sleep(5000);
                                    melsecMc = new SiemensS7Net(SiemensPLCS.S1200)
                                    {
                                        IpAddress = this.ConnParam.ServerIP, Port = this.ConnParam.Port
                                    };
                                    melsecMc.SetPersistentConnection();

                                    LogHelper.WriteCallBoxLog("重连呼叫器!  " + string.Format("[{0},{2},{1}] ", PLCCode, d.DBAddress, ConnParam.ServerIP));
                                    continue;
                                }
                                string SenDLog = "";
                                foreach (short item in read.Content)
                                {
                                    SenDLog += ((int)item).ToString("X") + " ";
                                }
                                LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "内容为:" + SenDLog);
                                if (Convert.ToInt32(read.Content[0]) == 1)
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 0);
                                }
                                else
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 1);
                                }
                            }
                            else if (d.LocationState == 2)
                            {
                                //根据当前呼叫器ID加载呼叫器明细
                                OperateResult <short[]> read = melsecMc.ReadInt16(d.DBAddress, 15);
                                if (!read.IsSuccess)
                                {
                                    LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "失败!");
                                    continue;
                                }
                                string SenDLog = "";
                                foreach (short item in read.Content)
                                {
                                    SenDLog += ((int)item).ToString("X") + " ";
                                }
                                LogHelper.WriteCallBoxLog("读取呼叫器 " + string.Format("[{0},{2},{1}]", PLCCode, d.DBAddress, ConnParam.ServerIP) + "内容为:" + SenDLog);
                                string codes = "";
                                foreach (short s in read.Content.Skip(3))
                                {
                                    codes += s.ToString() + ",";
                                }
                                if (Convert.ToInt32(read.Content[0]) == 1)
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 2, codes);
                                }
                                else
                                {
                                    HandleCallBoxMonitor(CallInfo, d, 0, codes);
                                }
                            }
                        }
                    }
                    else//无,呼叫器类型按照按钮来区分,秦川不支持
                    {
                    }
                }
                catch (Exception ex)
                { LogHelper.WriteCallBoxLog("处理呼叫器:" + PLCCode + "任务失败->" + ex.Message); }

                Thread.Sleep(1000 * 2);
            }
        }
Пример #22
0
 private void button_read_short_Click(object sender, EventArgs e)
 {
     ReadResultRender(m_siemensTcpNet.ReadInt16(txtAddress.Text), txtAddress.Text, txtResult);
 }
Пример #23
0
 /// <summary>
 /// 读取PLC双字节有符号值
 /// </summary>
 /// <param name="MemoryAddr">寄存器地址,如:V100(注意符号大写)</param>
 /// <returns></returns>
 public short ReadShort(string MemoryAddr)
 {
     return(siemensS7Net.ReadInt16(MemoryAddr).Content);
 }