public byte[] ReadBytes(int Length)
        {
            OperateResult <byte[]> result = siemensTcpNet.Read(Address, (ushort)Length);

            if (!result.IsSuccess)
            {
                ErrorCode = result.ToMessageShowString();
                IsConnect = false;
            }
            _readtime = DateTime.Now;
            ReadBuff  = result.Content;
            return(result.Content);
        }
Beispiel #2
0
        private void btnReadChars_Click(object sender, EventArgs e)
        {
            if (!isConnected)
            {
                MessageBox.Show("还未连接 PLC");
                return;
            }

            try
            {
                ushort.TryParse(edtLength.Text, out ushort length);
                OperateResult <byte[]> rlt =
                    plc.Read(
                        $"DB{edtDBNumber.Text}.{edtOffset.Text}",
                        length);
                if (rlt.IsSuccess)
                {
                    edtText.Text = Encoding.ASCII.GetString(rlt.Content);
                }
                else
                {
                    edtText.Text = $"{rlt.ToMessageShowString()}";
                }
            }
            catch { edtText.Text = ""; }
        }
Beispiel #3
0
 private void button25_Click(object sender, EventArgs e)
 {
     try
     {
         OperateResult <byte[]> read = siemensTcpNet.Read(textBox6.Text, ushort.Parse(textBox9.Text));
         if (read.IsSuccess)
         {
             textBox10.Text = "结果:" + HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content);
         }
         else
         {
             MessageBox.Show("读取失败:" + read.ToMessageShowString( ));
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("读取失败:" + ex.Message);
     }
 }
Beispiel #4
0
        /// <summary>
        /// 读取PLC
        /// </summary>
        /// <param name="db"></param>
        /// <param name="length"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool HsRead(string db, ushort length, ref byte[] msg)
        {
            OperateResult <byte[]> result = sPLC.Read(db, length);

            if (result.IsSuccess)
            {
                msg = result.Content;
                return(true);
            }
            return(false);
        }
Beispiel #5
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
                {
                    // 发生了异常
                }
            }
        }
Beispiel #6
0
 /// <summary>
 /// 批量读取
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Btn_ReadMany_Click(object sender, EventArgs e)
 {
     try
     {
         OperateResult <byte[]> result = siemensTcpNet.Read(txt_ReadMany_Address.Text, ushort.Parse(txt_ReadMany_Lenth.Text));
         if (result.IsSuccess)
         {
             txt_Result_Many.Text = $"结果:{SoftBasic.ByteToHexString(result.Content)}";
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Beispiel #7
0
        public bool Read <T>(string addr, int length, ref object data)
        {
            var rs = _deviceApi.Read(addr, (ushort)length);

            if (rs.IsSuccess)
            {
                ErrorCode = 0;
                Message   = "Success";
                data      = rs.Content;
                return(true);
            }

            ErrorCode = rs.ErrorCode;
            Message   = rs.Message;
            Logger.Main.Warn($"[{_serviceName}] Read(\"{addr}\", {length})失败: {rs.ErrorCode}, {rs.Message}");
            ConnectStatus = false;
            return(false);
        }
Beispiel #8
0
 private void btnManyRead_Click(object sender, EventArgs e)
 {
     try
     {
         OperateResult <byte[]> read = m_siemensTcpNet.Read(txtManyReadAddr.Text, ushort.Parse(txtManyReadLen.Text));
         if (read.IsSuccess)
         {
             txtManyReadResult.Text = "结果:" + SoftBasic.ByteToHexString(read.Content);
         }
         else
         {
             txtManyReadResult.Text = "读取失败:" + read.ToMessageShowString();
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show("读取失败:" + ex.StackTrace);
     }
 }
Beispiel #9
0
        /// <summary>
        /// 读取数据块内容
        /// </summary>
        /// <param name="dbType">数据块类别标识</param>
        /// <param name="dbNumber">DB数据块编号</param>
        /// <param name="dbOffset">读取的起始偏移量</param>
        /// <param name="bufferLength">缓冲区长度</param>
        /// <param name="buffer">byte[]类型的缓冲区</param>
        /// <param name="errText">执行结果信息</param>
        public int ReadBlock(
            SiemensRegisterType dbType,
            int dbNumber,
            int dbOffset,
            int bufferLength,
            ref byte[] buffer,
            out string errText)
        {
            if (!isConnected)
            {
                isConnected = Connect();
            }

            buffer = new byte[bufferLength];
            OperateResult <byte[]> resNo;

            lock (_lockObjet)
            {
                string addr = $"{dbType.ToString()}{dbNumber}.{dbOffset}";
                resNo =
                    siemensTcpNet.Read(
                        addr,
                        (ushort)bufferLength);
            }

            if (!resNo.IsSuccess)
            {
                errText = resNo.ToMessageShowString();
                _log.Error(
                    $"PLC:[{IPAddress}]读取[{dbType}]" +
                    $"[{dbNumber}]失败,失败信息:" +
                    $"[Message:{errText}");
            }
            else
            {
                buffer  = resNo.Content;
                errText = "读取正常";
            }

            return(resNo.ErrorCode);
        }
Beispiel #10
0
        public void ReadExample2( )
        {
            #region ReadExample2

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

            OperateResult <byte[]> read = siemens.Read("M100", 8);
            if (read.IsSuccess)
            {
                float temp  = siemens.ByteTransform.TransInt16(read.Content, 0) / 10f;
                float press = siemens.ByteTransform.TransInt16(read.Content, 2) / 100f;
                int   count = siemens.ByteTransform.TransInt32(read.Content, 2);

                // do something
            }
            else
            {
                // failed
            }


            #endregion
        }
Beispiel #11
0
 private void userButton9_Click(object sender, EventArgs e)
 {
     for (int i = 0; i < 10; i++)
     {
         OperateResult <byte[]> read = siemensTcpNet.Read("M100", 6);
         if (read.IsSuccess)
         {
             TextBoxAppendStringLine(HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content));
         }
         else
         {
             MessageBox.Show(read.ToMessageShowString());
             if (read.Content != null)
             {
                 textBox1.Text = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(read.Content);
             }
         }
     }
 }
Beispiel #12
0
        //读写PLC2  1500的线程
        private void ThreadBackgroundReadPlc2()
        {
            // 此处假设我们读取的是西门子PLC的数据,其实三菱的数据读取原理是一样的,可以仿照西门子的开发
            while (true)
            {
                //触发事件
                //EventArgs,写一个子类继承该类,子类中添加需要封装的数据信息,此处只需要传递string信息,详见MyEventArgs
                if (SendMsgEvent != null)
                {
                    SendMsgEvent(this, new MyEventArg()
                    {
                        buffer1  = buffer1,
                        strplc1  = "plc1",
                        failed1  = failed1,
                        buffer2  = buffer2,
                        strplc2  = "plc2",
                        failed2  = failed2_Read,
                        userdata = Usertype
                    });
                }

                frmNewMain.bListenPlc2 = true;
                HslCommunication.OperateResult <JObject> read = null;
                // 这里仅仅演示了西门子的数据读取
                HslCommunication.OperateResult <byte[]> buff = siemensTcpNet2_Read.Read("M500", 50);
                bool isSuccess2 = buff.IsSuccess;
                if (isSuccess2)
                {
                    //首先返回一个读取成功的对象
                    read = HslCommunication.OperateResult.CreateSuccessResult(new JObject()
                    {
                    });
                    //PLC2访问成功
                    readPLC2Success = read.IsSuccess;
                    ////临时用先清写指令区
                    //byte[] buffWrite_clear0 = new byte[10];
                    //OperateResult result0 = frmSieMens.siemensTcpNet2.Write("M510", buffWrite_clear0);

                    //集合先清空
                    buffer2.Clear();
                    //读取回来的数据显示到txtbox
                    //ShowReadContent(buff, "PLC2");

                    buffer2.AddRange(buff.Content);//将指定集合的元素添加到集合buffer的末尾

                    ///**************************SICK扫描数据**************************/
                    //Usertype.SICK_CastNumber = buff.Content[5];
                    //Usertype.SICK_CastType = buff.Content[6];
                    //Usertype.SICK_CastCentreX = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 7);
                    //Usertype.SICK_CastCentreY = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 9);
                    //Usertype.SICK_PoolLidHas = buff.Content[11];

                    /**************************PLC反馈数据**************************/
                    Usertype.PLC_Current_X = siemensTcpNet1.ByteTransform.TransUInt32(buff.Content, 0);
                    Usertype.PLC_Current_Y = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 4);
                    Usertype.PLC_Current_Z = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 6);

                    Usertype.X_Arrive_Signal = buff.Content[10];
                    Usertype.Y_Arrive_Signal = buff.Content[11];
                    Usertype.Z_Arrive_Signal = buff.Content[12];
                    Usertype.Clamp_State     = buff.Content[13];

                    //Usertype.Person_Confirm_PoolLid = buff.Content[36];

                    /**************************回读上位机写进PLC的数据**************************/

                    Usertype.ReadBack_Dest_X = siemensTcpNet1.ByteTransform.TransUInt32(buff.Content, 20);
                    Usertype.ReadBack_Dest_Y = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 24);
                    Usertype.ReadBack_Dest_Z = siemensTcpNet1.ByteTransform.TransUInt16(buff.Content, 26);

                    Usertype.ReadBack_XEnable_Signal = buff.Content[30];
                    Usertype.ReadBack_YEnable_Signal = buff.Content[31];
                    Usertype.ReadBack_ZEnable_Signal = buff.Content[32];

                    Usertype.ReadBack_Catch_Release_Enable_Signal = buff.Content[34];
                    Usertype.ReadBack_Write_New_Flag = buff.Content[35];



                    if (frmNewMain.dataListrequest.Count == 0 && !frmNewMain.startExchageLib)//指令请求队列中没有任何指令时,且没有倒库任务时
                    {
                        if (Start_system_flag == false)
                        {
                            //使能信号先清零
                            siemensTcpNet2_Read.Write("M530", 0x00);
                            siemensTcpNet2_Read.Write("M531", 0x00);
                            siemensTcpNet2_Read.Write("M532", 0x00);
                            Start_system_flag = true;
                        }
                    }
                    //初始位值到位,抱闸打开,开始 三个信号同时有
                    if (buffer2[14] == 0x01 && buffer2[15] == 0x01 && buffer2[16] == 0x01)//上位机收到PLC自动演示信号
                    {
                        //开始将自动演示指令放进指令缓存区
                        string s1        = "01020201010102010000";
                        string s2        = "02020201010102010000";
                        string s3        = "04020401019999010000";
                        string sqlupdate = @"update 库存记录表
                                            set
                                            物料规格 = '卷筒',
                                            物料状态 = '空卷',
                                            检测状态 = NULL,
                                            生产批号 = NULL
                                            where 物料状态 is not null";
                        if (frmNewMain.orderhas.Count == 0 && frmNewMain.dataListrequest.Count == 0)
                        {
                            SqlHelper.ExecuteNonQuery(sqlupdate);
                            //首先将库中卷筒变为空卷
                            lock (locker)
                            {
                                frmNewMain.orderhas.Add("01", s1);
                                frmNewMain.orderhas.Add("02", s2);
                                frmNewMain.orderhas.Add("04", s3);
                            }
                        }
                        //线程开启标志
                        Usertype.threadStart_flag = true;
                    }
                    else
                    {
                        //线程关闭
                        Usertype.threadStart_flag = false;
                        //清空任务队列
                        lock (locker)
                        {
                            frmNewMain.orderhas.Clear();
                            frmNewMain.dataListrequest.Clear();
                        }
                    }
                }
                else
                {
                    read = HslCommunication.OperateResult.CreateFailedResult <JObject>(buff);
                }


                if (read.IsSuccess)
                {
                    if (failed2_Read != 0)//断线重读成功
                    {
                        failed2_Read = 0;
                        //ShowFailedMessage(failed2, "执行动作PLC");//显示连接成功
                    }
                    else
                    {
                        failed2_Read = 0; // 读取失败次数清空
                    }
                    frmNewMain.bOpening2 = true;
                }
                else
                {
                    frmNewMain.bOpening2 = false;
                    failed2_Read++;
                    //ShowFailedMessage(failed2, "执行动作PLC");  // 显示出来读取失败的情况
                }

                Thread.Sleep(10);              // 两次读取的时间间隔
            }
        }
Beispiel #13
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);
        }
        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( );
        }
Beispiel #15
0
        private void ThreadBackgroundReadPlc( )
        {
            // 此处假设我们读取的是西门子PLC的数据,其实三菱的数据读取原理是一样的,可以仿照西门子的开发

            /**************************************************************************************************
            *
            *    假设一:M100,M101存储了一个温度值,举例,100.5℃数据为1005
            *    假设二:M102存储了设备启停信号,0为停止,1为启动
            *    假设三:M103-M106存储了一个产量值,举例:12345678
            *
            **************************************************************************************************/

            double temperature = 100f;

            while (true)
            {
                if (isReadingPlc)
                {
                    // 这里仅仅演示了西门子的数据读取
                    // 事实上你也可以改成三菱的,无非解析数据的方式不一致而已,其他数据推送代码都是一样的



                    HslCommunication.OperateResult <JObject> read = null; //siemensTcpNet.Read( "M100", 7 );

                    if (isReadRandom)
                    {
                        temperature = Math.Round(temperature + random.Next(100) / 10f - 5f, 1);
                        if (temperature < 0 || temperature > 200)
                        {
                            temperature = 100;
                        }

                        // 当没有测试的设备的时候,此处就演示读取随机数的情况
                        read = HslCommunication.OperateResult.CreateSuccessResult(new JObject( )
                        {
                            { "temp", new JValue(temperature) },
                            { "enable", new JValue(random.Next(100) > 10) },
                            { "product", new JValue(random.Next(10000)) }
                        });
                    }
                    else
                    {
                        HslCommunication.OperateResult <byte[]> tmp = siemensTcpNet.Read("M100", 7);
                        if (tmp.IsSuccess)
                        {
                            double temp1         = siemensTcpNet.ByteTransform.TransInt16(tmp.Content, 0) / 10.0;
                            bool   machineEnable = tmp.Content[2] != 0x00;
                            int    product       = siemensTcpNet.ByteTransform.TransInt32(tmp.Content, 3);

                            read = HslCommunication.OperateResult.CreateSuccessResult(new JObject( )
                            {
                                { "temp", new JValue(temp1) },
                                { "enable", new JValue(machineEnable) },
                                { "product", new JValue(product) }
                            });
                        }
                        else
                        {
                            read = HslCommunication.OperateResult.CreateFailedResult <JObject>(tmp);
                        }
                    }


                    if (read.IsSuccess)
                    {
                        failed = 0;                                                                                   // 读取失败次数清空
                        mqttServer.PublishTopicPayload("A", Encoding.UTF8.GetBytes(read.Content.ToString( )));        // 推送数据,关键字为A
                        ShowReadContent(read.Content);                                                                // 在主界面进行显示,此处仅仅是测试,实际项目中不建议在服务端显示数据信息
                    }
                    else
                    {
                        failed++;
                        ShowFailedMessage(failed);                               // 显示出来读取失败的情况
                    }
                }

                Thread.Sleep(500);                              // 两次读取的时间间隔
            }
        }
Beispiel #16
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( );
        }
Beispiel #17
0
        /// <summary>
        /// 读取所有变量
        /// </summary>
        /// <param name="ut">数据对象</param>
        public static void ReadItems()
        {
            OperateResult <byte[]> read = siemensS7Net.Read("DB2.0", 141);
            {
                if (read.IsSuccess)
                {
                    userItem.Voltage   = siemensS7Net.ByteTransform.TransSingle(read.Content, 0);
                    userItem.Current   = siemensS7Net.ByteTransform.TransSingle(read.Content, 4);
                    userItem.Frequency = siemensS7Net.ByteTransform.TransSingle(read.Content, 8);
                    userItem.Power     = siemensS7Net.ByteTransform.TransSingle(read.Content, 12);
                    userItem.Energy    = siemensS7Net.ByteTransform.TransSingle(read.Content, 16);

                    userItem.XActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 20);
                    userItem.XActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 24);
                    userItem.XSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 28);
                    userItem.XSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 32);
                    userItem.XActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 36);

                    userItem.YActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 40);
                    userItem.YActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 44);
                    userItem.YSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 48);
                    userItem.YSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 52);
                    userItem.YActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 56);

                    userItem.ZActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 60);
                    userItem.ZActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 64);
                    userItem.ZSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 68);
                    userItem.ZSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 72);
                    userItem.ZActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 76);

                    userItem.ActTemp1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 80);
                    userItem.ActTemp2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 84);
                    userItem.ActTemp3 = siemensS7Net.ByteTransform.TransSingle(read.Content, 88);
                    userItem.ActTemp4 = siemensS7Net.ByteTransform.TransSingle(read.Content, 92);

                    userItem.ActFlow1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 96);
                    userItem.ActFlow2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 100);
                    userItem.ActFlow3 = siemensS7Net.ByteTransform.TransSingle(read.Content, 104);

                    userItem.ActPressure = siemensS7Net.ByteTransform.TransSingle(read.Content, 108);

                    userItem.HeatRun    = siemensS7Net.ByteTransform.TransBool(read.Content, 112);
                    userItem.CoolingRun = siemensS7Net.ByteTransform.TransBool(read.Content, 113);
                    Views.CurvePage.heatStart.MyValue    = userItem.HeatRun;
                    Views.CurvePage.coolingStart.MyValue = userItem.CoolingRun;


                    userItem.ActAlarm1 = siemensS7Net.ByteTransform.TransUInt16(read.Content, 114);
                    userItem.ActAlarm2 = siemensS7Net.ByteTransform.TransUInt16(read.Content, 116);

                    userItem.ActLenght1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 118);
                    userItem.ActLenght2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 122);

                    userItem.Y_CDJ_QGQW = siemensS7Net.ByteTransform.TransBool(read.Content, 126);
                    userItem.Y_CDJ_QGHW = siemensS7Net.ByteTransform.TransBool(read.Content, 127);
                    userItem.Z_CDJ_QGQW = siemensS7Net.ByteTransform.TransBool(read.Content, 128);
                    userItem.Z_CDJ_QGHW = siemensS7Net.ByteTransform.TransBool(read.Content, 129);

                    userItem.JLG_1_SW  = siemensS7Net.ByteTransform.TransBool(read.Content, 130);
                    userItem.JLG_1_XW  = siemensS7Net.ByteTransform.TransBool(read.Content, 131);
                    userItem.JLG_2_SW  = siemensS7Net.ByteTransform.TransBool(read.Content, 132);
                    userItem.JLG_2_XW  = siemensS7Net.ByteTransform.TransBool(read.Content, 133);
                    userItem.JLG_3_SW  = siemensS7Net.ByteTransform.TransBool(read.Content, 134);
                    userItem.JLG_3_XW  = siemensS7Net.ByteTransform.TransBool(read.Content, 135);
                    userItem.JLG_4_SW  = siemensS7Net.ByteTransform.TransBool(read.Content, 136);
                    userItem.JLG_4_XW  = siemensS7Net.ByteTransform.TransBool(read.Content, 137);
                    userItem.DY_ZT     = siemensS7Net.ByteTransform.TransBool(read.Content, 138);
                    userItem.PQ_ZT     = siemensS7Net.ByteTransform.TransBool(read.Content, 139);
                    userItem.ProcessOk = siemensS7Net.ByteTransform.TransBool(read.Content, 140);
                    Views.CurvePage.processOk.MyValue = userItem.ProcessOk;
                }
            }
        }
 public override object ReadTag(Tag tag)
 {
     //LOG.Info("HSLTag开始读取" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff"));
     if (tag.AccessType == TagAccessType.Read || tag.AccessType == TagAccessType.ReadWrite)
     {
         try
         {
             if (tag.TagType == "bool")
             {
                 var res = PLC.ReadBool(tag.Address);
                 if (res.IsSuccess)
                 {
                     tag.TagValue = res.Content;
                     tag.Quality  = Quality.Good;
                 }
                 else
                 {
                     tag.Quality = Quality.Bad;
                 }
             }
             else if (tag.TagType == "string")
             {
                 OperateResult <string> res = new OperateResult <string>();
                 if (tag.Address.Contains("#"))
                 {
                     string[] adds    = tag.Address.Split('#');
                     string   address = adds[0];
                     ushort   len     = Convert.ToUInt16(adds[1]);
                     res = PLC.ReadString(adds[0], len);
                 }
                 else
                 {
                     res = PLC.ReadString(tag.Address, 1);
                 }
                 if (res.IsSuccess)
                 {
                     tag.TagValue = res.Content;
                     tag.Quality  = Quality.Good;
                 }
                 else
                 {
                     tag.Quality = Quality.Bad;
                 }
             }
             else if (tag.TagType == "datetime") //西门子Date_And_Time
             {
                 OperateResult <byte[]> res = PLC.Read(tag.Address, 8);
                 if (res.IsSuccess && res.Content.Length == 8)
                 {
                     var time = GetDateTime(res.Content, out bool isSuccess);
                     if (isSuccess)
                     {
                         tag.TagValue = time;
                         tag.Quality  = Quality.Good;
                     }
                 }
                 else
                 {
                     tag.Quality = Quality.Bad;
                 }
             }
             else
             {
                 var len = ConvertUtils.GetLength(tag);
                 OperateResult <byte[]> res = PLC.Read(tag.Address, len);
                 ConvertUtils.DecodeTagValue(tag, res, true);
             }
             return(tag.TagValue);
         }
         catch (Exception ex)
         {
             LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]");
             tag.Quality = Quality.Bad;
             return(tag.TagValue);
         }
     }
     else
     {
         return(null);
     }
 }
Beispiel #19
0
        //读取PLC1 地面PLC的线程
        private void ThreadBackgroundReadPlc1()
        {
            // 此处假设我们读取的是西门子PLC的数据,其实三菱的数据读取原理是一样的,可以仿照西门子的开发
            while (true)
            {
                frmNewMain.bListenPlc1 = true;
                HslCommunication.OperateResult <JObject> read = null;
                // 这里仅仅演示了西门子的数据读取
                HslCommunication.OperateResult <byte[]> buff = siemensTcpNet1.Read("M500", 50);
                ////读取字符串数据
                //HslCommunication.OperateResult<string> strbuff = siemensTcpNet1.ReadString("M522", 10);

                bool isSuccess = buff.IsSuccess;
                if (isSuccess)
                {
                    //首先返回一个读取成功的对象
                    read = HslCommunication.OperateResult.CreateSuccessResult(new JObject()
                    {
                    });
                    //访问成功
                    readPLC1Success = read.IsSuccess;
                    //集合先清空
                    buffer1.Clear();
                    //读取回来的数据显示到txtbox
                    //ShowReadContent(buff, "PLC1");

                    buffer1.AddRange(buff.Content);                                                      //将指定集合的元素添加到集合buffer的末尾
                    if (buffer1[3] == 0x01 && buffer1[0] != 0x00 && frmNewMain.startExchageLib == false) //有动作请求指令且没有倒库指令时
                    {
                        byte[] data = new byte[10];
                        buffer1.CopyTo(0, data, 0, 10);
                        //转换成字符串出掉末尾的\0\0\0
                        String d = frmNewMain.byteToHexStr(data).TrimEnd('\0');
                        //缓存中无相应工位的指令,且主队列中也没有相同的指令(避免同一位置下的重复指令)先将对应区域的指令放进字典中
                        if (!frmNewMain.orderhas.ContainsKey(buffer1[0].ToString("00")) && !frmNewMain.dataListrequest.Contains(d))
                        {
                            lock (locker)
                            {
                                frmNewMain.orderhas.Add(buffer1[0].ToString("00"), d);
                            }
                            frmNewMain.bread = true;
                            //将PLC请求指令区清零
                            byte[]        buffWrite_clear = new byte[10];
                            OperateResult result1         = siemensTcpNet1.Write("M500", buffWrite_clear);
                        }
                        else //直接将PLC请求指令区清零
                        {
                            byte[]        buffWrite_clear = new byte[10];
                            OperateResult result1         = siemensTcpNet1.Write("M500", buffWrite_clear);
                        }

                        //退库自动出卷时需要判断MB514,MB515
                        if (buffer1[14] != 0x00)
                        {
                            autoGoodscode = buffer1[14].ToString("00");
                        }
                        if (buffer1[15] == 0x01)
                        {
                            autoGoodssatate = "半卷";
                        }
                        else if (buffer1[15] == 0x02)
                        {
                            autoGoodssatate = "满卷";
                        }
                    }
                    if (frmNewMain.orderhas.Count > 0)
                    {
                        //如果触摸屏有A1工位撤销指令状态过来,且此时字典中有指令
                        if (buffer1[10] != 0)
                        {
                            if (frmNewMain.orderhas.ContainsKey(buffer1[10].ToString()))
                            {
                                lock (locker)
                                {
                                    frmNewMain.orderhas.Remove(buffer1[10].ToString());//移除对应项
                                }
                            }
                            siemensTcpNet1.Write("M510", 0);
                        }
                        //如果触摸屏有A2工位撤销指令状态过来,且此时字典中有指令
                        if (buffer1[11] != 0)
                        {
                            if (frmNewMain.orderhas.ContainsKey(buffer1[11].ToString()))
                            {
                                lock (locker)
                                {
                                    frmNewMain.orderhas.Remove(buffer1[11].ToString());//移除对应项
                                }
                            }

                            siemensTcpNet1.Write("M511", 0);
                        }
                        //如果触摸屏有抽检工位撤销指令状态过来,且此时字典中有指令
                        if (buffer1[12] != 0)
                        {
                            if (frmNewMain.orderhas.ContainsKey(buffer1[12].ToString()))
                            {
                                lock (locker)
                                {
                                    frmNewMain.orderhas.Remove(buffer1[12].ToString());//移除对应项
                                }
                            }

                            siemensTcpNet1.Write("M512", 0);
                        }
                        //如果触摸屏有B工位撤销指令状态过来,且此时字典中有对应工位的指令指令
                        if (buffer1[13] != 0)
                        {
                            if (frmNewMain.orderhas.ContainsKey(buffer1[13].ToString()))
                            {
                                lock (locker)
                                {
                                    frmNewMain.orderhas.Remove(buffer1[13].ToString());//移除对应项
                                }
                            }
                            siemensTcpNet1.Write("M513", 0);
                        }
                    }
                    else//字典中无指令时
                    {
                        //清撤销指令状态区
                        byte[] order_clear = new byte[4];
                        siemensTcpNet1.Write("M510", order_clear);
                        siemensTcpNet1.Write("M520", 0);
                        siemensTcpNet1.Write("M521", 0);
                    }

                    //获取字符串
                    string strKT = Encoding.ASCII.GetString(buff.Content, 22, 10);
                }
                else
                {
                    read = HslCommunication.OperateResult.CreateFailedResult <JObject>(buff);
                }


                if (read.IsSuccess)
                {
                    if (failed1 != 0)//断线重读成功
                    {
                        failed1 = 0;
                        //ShowFailedMessage(failed1, "地面PLC");//显示连接成功
                    }
                    else
                    {
                        failed1 = 0; // 读取失败次数清空
                    }
                    frmNewMain.bOpening1 = true;
                }
                else
                {
                    frmNewMain.bOpening1 = false;

                    failed1++;
                    //ShowFailedMessage(failed1, "地面PLC");  // 显示出来读取失败的情况
                }


                Thread.Sleep(10);                      // 两次读取的时间间隔
            }
        }
        /// <summary>
        /// 读取所有变量
        /// </summary>
        /// <param name="ut">数据对象</param>
        public static void ReadItems()
        {
            OperateResult <byte[]> read = siemensS7Net.Read("DB2.0", 140);
            {
                if (read.IsSuccess)
                {
                    userType.Voltage   = siemensS7Net.ByteTransform.TransSingle(read.Content, 0);
                    userType.Current   = siemensS7Net.ByteTransform.TransSingle(read.Content, 4);
                    userType.Frequency = siemensS7Net.ByteTransform.TransSingle(read.Content, 8);
                    userType.Power     = siemensS7Net.ByteTransform.TransSingle(read.Content, 12);
                    userType.Energy    = siemensS7Net.ByteTransform.TransSingle(read.Content, 16);

                    userType.XActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 20);
                    userType.XActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 24);
                    userType.XSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 28);
                    userType.XSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 32);
                    userType.XActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 36);

                    userType.YActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 40);
                    userType.YActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 44);
                    userType.YSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 48);
                    userType.YSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 52);
                    userType.YActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 56);

                    userType.ZActPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 60);
                    userType.ZActSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 64);
                    userType.ZSetPos     = siemensS7Net.ByteTransform.TransSingle(read.Content, 68);
                    userType.ZSetSpeed   = siemensS7Net.ByteTransform.TransSingle(read.Content, 72);
                    userType.ZActCurrent = siemensS7Net.ByteTransform.TransSingle(read.Content, 76);

                    userType.ActTemp1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 80);
                    userType.ActTemp2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 84);
                    userType.ActTemp3 = siemensS7Net.ByteTransform.TransSingle(read.Content, 88);
                    userType.ActTemp4 = siemensS7Net.ByteTransform.TransSingle(read.Content, 92);

                    userType.ActFlow1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 96);
                    userType.ActFlow2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 100);
                    userType.ActFlow3 = siemensS7Net.ByteTransform.TransSingle(read.Content, 104);

                    userType.ActPressure = siemensS7Net.ByteTransform.TransSingle(read.Content, 108);

                    userType.HeatRun    = Convert.ToBoolean(read.Content[112]);
                    userType.CoolingRun = Convert.ToBoolean(read.Content[113]);

                    userType.ActAlarm1 = siemensS7Net.ByteTransform.TransInt16(read.Content, 114);
                    userType.ActAlarm2 = siemensS7Net.ByteTransform.TransInt16(read.Content, 116);

                    userType.ActLenght1 = siemensS7Net.ByteTransform.TransSingle(read.Content, 118);
                    userType.ActLenght2 = siemensS7Net.ByteTransform.TransSingle(read.Content, 122);

                    userType.Y_CDJ_QGQW = Convert.ToBoolean(read.Content[126]);
                    userType.Y_CDJ_QGHW = Convert.ToBoolean(read.Content[127]);
                    userType.Z_CDJ_QGQW = Convert.ToBoolean(read.Content[128]);
                    userType.Z_CDJ_QGHW = Convert.ToBoolean(read.Content[129]);

                    userType.JLG_1_SW = Convert.ToBoolean(read.Content[130]);
                    userType.JLG_1_XW = Convert.ToBoolean(read.Content[131]);
                    userType.JLG_2_SW = Convert.ToBoolean(read.Content[132]);
                    userType.JLG_2_XW = Convert.ToBoolean(read.Content[133]);
                    userType.JLG_3_SW = Convert.ToBoolean(read.Content[134]);
                    userType.JLG_3_XW = Convert.ToBoolean(read.Content[135]);
                    userType.JLG_4_SW = Convert.ToBoolean(read.Content[136]);
                    userType.JLG_4_XW = Convert.ToBoolean(read.Content[137]);
                    userType.DY_ZT    = Convert.ToBoolean(read.Content[138]);
                    userType.PQ_ZT    = Convert.ToBoolean(read.Content[139]);
                }
            }
        }