/// <summary>
        /// 读取字节数组
        /// </summary>
        /// <param name="address">标签变量地址结构</param>
        /// <param name="size">长度,</param>
        /// <returns></returns>
        public byte[] ReadBytes(DeviceAddress address, ushort length)
        {
            if (!_connected)
            {
                return(null);
            }
            //Thread.Sleep(2000);
            var addr   = GetAddress(address);
            var result = melseNet.Read(addr, length);

            if (result.IsSuccess)
            {
                return(result.Content);
            }
            else
            {
                if (OnError != null)
                {
                    OnError(this, new IOErrorEventArgs(this.ID, result.Message));
                }
                if (result.Message.Contains("连接失败"))
                {
                    _connected = false;
                }
                //_connected = false;
                //Connect();
                return(null);
            }
        }
Esempio n. 2
0
 private void button25_Click(object sender, EventArgs e)
 {
     try
     {
         OperateResult <byte[]> read = melsec_net.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);
     }
 }
        private void test5( )
        {
            OperateResult <byte[]> read = melsec_net.Read("D100", 10);

            if (read.IsSuccess)
            {
                int    count   = melsec_net.ByteTransform.TransInt32(read.Content, 0);
                float  temp    = melsec_net.ByteTransform.TransSingle(read.Content, 4);
                short  name1   = melsec_net.ByteTransform.TransInt16(read.Content, 8);
                string barcode = Encoding.ASCII.GetString(read.Content, 10, 10);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 读单字
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public int ReadW(string address)
        {
            OperateResult <byte[]> read = melsec_net.Read(address, 1);

            if (read.IsSuccess)
            {
                _Connect = true;
            }
            else
            {
                _Connect = false;
            }
            if (read.Content.Length >= 2)
            {
                string HexValue = read.Content[1].ToString("X2") + read.Content[0].ToString("X2");
                return(Convert.ToInt16(HexValue, 16));
            }
            else
            {
                return(-1);
            }
        }
        private void userButton1_Click(object sender, EventArgs e)
        {
            // M100-M104读取显示
            OperateResult <byte[]> read = melsec_net.Read("M100", 5);

            if (read.IsSuccess)
            {
                //成功读取,True代表通,False代表不通
                bool M100 = read.Content[0] == 1;
                bool M101 = read.Content[1] == 1;
                bool M102 = read.Content[2] == 1;
                bool M103 = read.Content[3] == 1;
                bool M104 = read.Content[4] == 1;
                // 显示
                TextBoxAppendStringLine($"M100:{M100} M101:{M101} M102:{M102} M103:{M103} M104:{M104}");
            }
            else
            {
                //失败读取,显示失败信息
                MessageBox.Show(read.ToMessageShowString());
            }
        }
Esempio n. 6
0
        /// <summary>
        /// 读双字
        /// </summary>
        /// <param name="address"></param>
        /// <returns></returns>
        public int ReadW(string address)
        {
            OperateResult <byte[]> read = melsec_net.Read(address, 1);

            if (read.IsSuccess)
            {
                _Connect = true;
            }
            else
            {
                _Connect = false;
            }
            if (read.Content.Length >= 2)
            {
                return((read.Content[1] << 8) + read.Content[0]);
            }
            else
            {
                return(-1);
            }
        }
Esempio n. 7
0
        public void ReadExample2( )
        {
            #region ReadExample2

            MelsecMcNet melsec_net = new MelsecMcNet("192.168.0.100", 6000);

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

                // do something
            }
            else
            {
                // failed
            }


            #endregion
        }
Esempio n. 8
0
        public override object ReadTag(Tag tag)
        {
            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")
                    {
                        if (tag.Address.Contains("."))
                        {
                            try
                            {
                                string address = tag.Address.Split('.')[0];
                                ushort len     = Convert.ToUInt16(tag.Address.Split('.')[1]);
                                var    res     = PLC.ReadString(tag.Address.Split('.')[0], len);
                                if (res.IsSuccess)
                                {
                                    tag.TagValue = res.Content.Replace("\0", "");
                                    tag.Quality  = Quality.Good;
                                }
                                else
                                {
                                    tag.Quality = Quality.Bad;
                                }
                            }
                            catch (Exception)
                            {
                                LOG.Error($"Tag Address Error {tag.Address}");
                            }
                        }
                        else
                        {
                            LOG.Error($"Tag Address Error {tag.Address}");
                        }
                    }
                    else
                    {
                        ushort len = ConvertUtils.GetLength(tag);

                        var res = PLC.Read(tag.Address, len);
                        ConvertUtils.DecodeTagValue(tag, res);
                    }
                    return(tag.TagValue);
                }
                catch (Exception ex)
                {
                    LOG.Error($"Datasource[{SourceName}] read error. Tag[{tag.TagName}] Address[{tag.Address}] Message[{ex.Message}]");
                    tag.TagValue = null;
                    tag.Quality  = Quality.Bad;
                    return(tag.TagValue);
                }
            }
            else
            {
                return(null);
            }
        }
        public void MelsecUnitTest( )
        {
            MelsecMcNet plc = new MelsecMcNet("192.168.8.13", 6001);

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

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

            bool[] boolTmp = new bool[] { true, true, false, true, false, true, false };
            Assert.IsTrue(plc.Write(address, true).IsSuccess);
            Assert.IsTrue(plc.ReadBool(address).Content == true);
            Assert.IsTrue(plc.Write(address, boolTmp).IsSuccess);
            bool[] readBool = plc.ReadBool(address, (ushort)boolTmp.Length).Content;
            for (int i = 0; i < boolTmp.Length; i++)
            {
                Assert.IsTrue(readBool[i] == boolTmp[i]);
            }

            address = "D300";
            // short类型
            Assert.IsTrue(plc.Write(address, (short)12345).IsSuccess);
            Assert.IsTrue(plc.ReadInt16(address).Content == 12345);
            short[] shortTmp = new short[] { 123, 423, -124, 5313, 2361 };
            Assert.IsTrue(plc.Write(address, shortTmp).IsSuccess);
            short[] readShort = plc.ReadInt16(address, (ushort)shortTmp.Length).Content;
            for (int i = 0; i < readShort.Length; i++)
            {
                Assert.IsTrue(readShort[i] == shortTmp[i]);
            }

            // ushort类型
            Assert.IsTrue(plc.Write(address, (ushort)51234).IsSuccess);
            Assert.IsTrue(plc.ReadUInt16(address).Content == 51234);
            ushort[] ushortTmp = new ushort[] { 5, 231, 12354, 5313, 12352 };
            Assert.IsTrue(plc.Write(address, ushortTmp).IsSuccess);
            ushort[] readUShort = plc.ReadUInt16(address, (ushort)ushortTmp.Length).Content;
            for (int i = 0; i < ushortTmp.Length; i++)
            {
                Assert.IsTrue(readUShort[i] == ushortTmp[i]);
            }

            // int类型
            Assert.IsTrue(plc.Write(address, 12342323).IsSuccess);
            Assert.IsTrue(plc.ReadInt32(address).Content == 12342323);
            int[] intTmp = new int[] { 123812512, 123534, 976124, -1286742 };
            Assert.IsTrue(plc.Write(address, intTmp).IsSuccess);
            int[] readint = plc.ReadInt32(address, (ushort)intTmp.Length).Content;
            for (int i = 0; i < intTmp.Length; i++)
            {
                Assert.IsTrue(readint[i] == intTmp[i]);
            }

            // uint类型
            Assert.IsTrue(plc.Write(address, (uint)416123237).IsSuccess);
            Assert.IsTrue(plc.ReadUInt32(address).Content == (uint)416123237);
            uint[] uintTmp = new uint[] { 81623123, 91712749, 91273123, 123, 21242, 5324 };
            Assert.IsTrue(plc.Write(address, uintTmp).IsSuccess);
            uint[] readuint = plc.ReadUInt32(address, (ushort)uintTmp.Length).Content;
            for (int i = 0; i < uintTmp.Length; i++)
            {
                Assert.IsTrue(readuint[i] == uintTmp[i]);
            }

            // float类型
            Assert.IsTrue(plc.Write(address, 123.45f).IsSuccess);
            Assert.IsTrue(plc.ReadFloat(address).Content == 123.45f);
            float[] floatTmp = new float[] { 123, 5343, 1.45f, 563.3f, 586.2f };
            Assert.IsTrue(plc.Write(address, floatTmp).IsSuccess);
            float[] readFloat = plc.ReadFloat(address, (ushort)floatTmp.Length).Content;
            for (int i = 0; i < readFloat.Length; i++)
            {
                Assert.IsTrue(floatTmp[i] == readFloat[i]);
            }

            // double类型
            Assert.IsTrue(plc.Write(address, 1234.5434d).IsSuccess);
            Assert.IsTrue(plc.ReadDouble(address).Content == 1234.5434d);
            double[] doubleTmp = new double[] { 1.4213d, 1223d, 452.5342d, 231.3443d };
            Assert.IsTrue(plc.Write(address, doubleTmp).IsSuccess);
            double[] readDouble = plc.ReadDouble(address, (ushort)doubleTmp.Length).Content;
            for (int i = 0; i < doubleTmp.Length; i++)
            {
                Assert.IsTrue(readDouble[i] == doubleTmp[i]);
            }

            // long类型
            Assert.IsTrue(plc.Write(address, 123617231235123L).IsSuccess);
            Assert.IsTrue(plc.ReadInt64(address).Content == 123617231235123L);
            long[] longTmp = new long[] { 12312313123L, 1234L, 412323812368L, 1237182361238123 };
            Assert.IsTrue(plc.Write(address, longTmp).IsSuccess);
            long[] readLong = plc.ReadInt64(address, (ushort)longTmp.Length).Content;
            for (int i = 0; i < longTmp.Length; i++)
            {
                Assert.IsTrue(readLong[i] == longTmp[i]);
            }

            // ulong类型
            Assert.IsTrue(plc.Write(address, 1283823681236123UL).IsSuccess);
            Assert.IsTrue(plc.ReadUInt64(address).Content == 1283823681236123UL);
            ulong[] ulongTmp = new ulong[] { 21316UL, 1231239127323UL, 1238612361283123UL };
            Assert.IsTrue(plc.Write(address, ulongTmp).IsSuccess);
            ulong[] readULong = plc.ReadUInt64(address, (ushort)ulongTmp.Length).Content;
            for (int i = 0; i < readULong.Length; i++)
            {
                Assert.IsTrue(readULong[i] == ulongTmp[i]);
            }

            // string类型
            Assert.IsTrue(plc.Write(address, "123123").IsSuccess);
            Assert.IsTrue(plc.ReadString(address, 3).Content == "123123");

            // byte类型
            byte[] byteTmp = new byte[] { 0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8 };
            Assert.IsTrue(plc.Write(address, byteTmp).IsSuccess);
            Assert.IsTrue(SoftBasic.IsTwoBytesEquel(plc.Read(address, 3).Content, byteTmp));

            plc.ConnectClose( );
        }
Esempio n. 10
0
        //PLC读取
        private object ReadSieTcpValue(Config.PlcTypeItem plctype, Config.PlcDataItem plcdata, MelsecMcNet plc)
        {
            try
            {
                if (plctype == null || !plctype.IsConnected || plcdata == null || plc == null)
                {
                    return(null);
                }

                switch (plcdata.DataType)
                {
                case Common.DataTypes.Bool:    //Bool
                    plcdata.ValueNew = plc.ReadBool(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Byte:    //Byte
                    plcdata.ValueNew = plc.Read(plcdata.Address, 1).Content;
                    break;

                case Common.DataTypes.Short:
                    plcdata.ValueNew = plc.ReadInt16(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Ushort:
                    plcdata.ValueNew = plc.ReadUInt16(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Int:
                    plcdata.ValueNew = plc.ReadInt32(plcdata.Address).Content;
                    break;

                case Common.DataTypes.UInt:
                    plcdata.ValueNew = plc.ReadUInt32(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Long:
                    long lValueNew = 0;
                    if (long.TryParse(plc.ReadInt64(plcdata.Address).Content.ToString(), out lValueNew))
                    {
                        long temp = BpLong.SwapInt64(lValueNew);
                        plcdata.ValueNew = temp;
                    }
                    // plcdata.ValueNew = plc.ReadInt64(plcdata.Address).Content;
                    break;

                case Common.DataTypes.ULong:
                    plcdata.ValueNew = plc.ReadUInt64(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Float:
                    plcdata.ValueNew = plc.ReadFloat(plcdata.Address).Content;
                    break;

                case Common.DataTypes.Double:
                    plcdata.ValueNew = plc.ReadDouble(plcdata.Address).Content;
                    break;

                case Common.DataTypes.String:
                    HslCommunication.OperateResult <byte[]> data = (HslCommunication.OperateResult <byte[]>)plc.Read(plcdata.Address, 50);
                    if (data != null && data.Content != null && data.Content.Length > 2)
                    {
                        List <byte> lstData = new List <byte>();
                        int         nLen    = data.Content[1];
                        for (int i = 2; i < nLen + 2; i++)
                        {
                            lstData.Add(data.Content[i]);
                        }
                        plcdata.ValueNew = System.Text.Encoding.ASCII.GetString(lstData.ToArray());
                    }
                    break;

                default:
                    break;
                }
            }
            catch
            {
                //MessageBox.Show(ex.Message);
            }

            return(plcdata.ValueNew);
        }