Пример #1
0
        /// <summary>
        /// 从三菱PLC中读取想要的数据,返回读取结果
        /// </summary>
        /// <param name="address">读取地址,格式为"M100","D100","W1A0"</param>
        /// <param name="length">读取的数据长度,字最大值960,位最大值7168</param>
        /// <returns>带成功标志的结果数据对象</returns>
        /// <remarks>
        /// 地址支持的列表参考 <seealso cref="MelsecMcAsciiNet"/> 的备注说明
        /// </remarks>
        /// <example>
        /// 假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,读取如下:
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="ReadExample2" title="Read示例" />
        /// 以下是读取不同类型数据的示例
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="ReadExample1" title="Read示例" />
        /// </example>
        public override OperateResult <byte[]> Read(string address, ushort length)
        {
            // 分析地址
            OperateResult <McAddressData> addressResult = McAnalysisAddress(address, length);

            if (!addressResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(addressResult));
            }

            // 地址分析
            byte[] coreResult = MelsecHelper.BuildAsciiReadMcCoreCommand(addressResult.Content, false);

            // 核心交互
            var read = ReadFromCoreServer(PackMcCommand(coreResult, NetworkNumber, NetworkStationNumber));

            if (!read.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(read));
            }

            // 错误代码验证
            ushort errorCode = Convert.ToUInt16(Encoding.ASCII.GetString(read.Content, 18, 4), 16);

            if (errorCode != 0)
            {
                return(new OperateResult <byte[]>(errorCode, StringResources.Language.MelsecPleaseReferToManulDocument));
            }

            // 数据解析,需要传入是否使用位的参数
            return(ExtractActualData(read.Content, false));
        }
        /// <summary>
        /// 根据类型地址长度确认需要读取的报文
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">长度</param>
        /// <param name="isBit">指示是否按照位成批的读出</param>
        /// <param name="networkNumber">网络号信息</param>
        /// <param name="networkStationNumber">网络站号信息</param>
        /// <returns>带有成功标志的指令数据</returns>
        public static OperateResult <byte[]> BuildReadCommand(string address, ushort length, bool isBit, byte networkNumber = 0, byte networkStationNumber = 0)
        {
            OperateResult <byte[]> coreResult = MelsecHelper.BuildAsciiReadMcCoreCommand(address, length, isBit);

            if (!coreResult.IsSuccess)
            {
                return(coreResult);
            }

            return(OperateResult.CreateSuccessResult(PackMcCommand(coreResult.Content, networkNumber, networkStationNumber)));
        }
Пример #3
0
        /// <summary>
        /// 从三菱PLC中批量读取位软元件,返回读取结果
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">读取的长度</param>
        /// <returns>带成功标志的结果数据对象</returns>
        /// <remarks>
        /// 地址支持的列表参考 <seealso cref="MelsecMcAsciiNet"/> 的备注说明
        /// </remarks>
        /// <example>
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\MelsecAscii.cs" region="ReadBool" title="Bool类型示例" />
        /// </example>
        public virtual OperateResult <bool[]> ReadBool(string address, ushort length)
        {
            // 地址分析
            OperateResult <byte[]> coreResult = MelsecHelper.BuildAsciiReadMcCoreCommand(address, length, true, McAnalysisAddress);

            if (!coreResult.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(coreResult));
            }

            // 核心交互
            var read = ReadFromCoreServer(PackMcCommand(coreResult.Content, NetworkNumber, NetworkStationNumber));

            if (!read.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(read));
            }

            // 错误代码验证
            ushort errorCode = Convert.ToUInt16(Encoding.ASCII.GetString(read.Content, 18, 4), 16);

            if (errorCode != 0)
            {
                return(new OperateResult <bool[]>(errorCode, StringResources.Language.MelsecPleaseReferToManulDocument));
            }

            // 数据解析,需要传入是否使用位的参数
            var extract = ExtractActualData(read.Content, true);

            if (!extract.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <bool[]>(extract));
            }

            // 转化bool数组
            return(OperateResult.CreateSuccessResult(extract.Content.Select(m => m == 0x01).Take(length).ToArray( )));
        }
Пример #4
0
        /// <summary>
        /// 根据类型地址长度确认需要读取的报文
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="length">长度</param>
        /// <param name="isBit">指示是否按照位成批的读出</param>
        /// <param name="networkNumber">网络号信息</param>
        /// <param name="networkStationNumber">网络站号信息</param>
        /// <returns>带有成功标志的指令数据</returns>
        public static OperateResult <byte[]> BuildReadCommand(string address, ushort length, bool isBit, byte networkNumber = 0, byte networkStationNumber = 0)
        {
            OperateResult <byte[]> coreResult = MelsecHelper.BuildAsciiReadMcCoreCommand(address, length, isBit);

            if (!coreResult.IsSuccess)
            {
                return(coreResult);
            }

            return(OperateResult.CreateSuccessResult(PackMcCommand(coreResult.Content, networkNumber, networkStationNumber)));


            OperateResult <MelsecMcDataType, int> analysis = MelsecHelper.McAnalysisAddress(address);

            if (!analysis.IsSuccess)
            {
                return(OperateResult.CreateFailedResult <byte[]>(analysis));
            }

            // 默认信息----注意:高低字节交错
            byte[] _PLCCommand = new byte[42];
            _PLCCommand[0]  = 0x35;                                                               // 副标题
            _PLCCommand[1]  = 0x30;
            _PLCCommand[2]  = 0x30;
            _PLCCommand[3]  = 0x30;
            _PLCCommand[4]  = MelsecHelper.BuildBytesFromData(networkNumber)[0];                  // 网络号
            _PLCCommand[5]  = MelsecHelper.BuildBytesFromData(networkNumber)[1];
            _PLCCommand[6]  = 0x46;                                                               // PLC编号
            _PLCCommand[7]  = 0x46;
            _PLCCommand[8]  = 0x30;                                                               // 目标模块IO编号
            _PLCCommand[9]  = 0x33;
            _PLCCommand[10] = 0x46;
            _PLCCommand[11] = 0x46;
            _PLCCommand[12] = MelsecHelper.BuildBytesFromData(networkStationNumber)[0];           // 目标模块站号
            _PLCCommand[13] = MelsecHelper.BuildBytesFromData(networkStationNumber)[1];
            _PLCCommand[14] = 0x30;                                                               // 请求数据长度
            _PLCCommand[15] = 0x30;
            _PLCCommand[16] = 0x31;
            _PLCCommand[17] = 0x38;
            _PLCCommand[18] = 0x30;                                                               // CPU监视定时器
            _PLCCommand[19] = 0x30;
            _PLCCommand[20] = 0x31;
            _PLCCommand[21] = 0x30;

            _PLCCommand[22] = 0x30;                                                               // 批量读取数据命令
            _PLCCommand[23] = 0x34;
            _PLCCommand[24] = 0x30;
            _PLCCommand[25] = 0x31;
            _PLCCommand[26] = 0x30;                                                               // 以点为单位还是字为单位成批读取
            _PLCCommand[27] = 0x30;
            _PLCCommand[28] = 0x30;
            _PLCCommand[29] = isBit ? (byte)0x31 : ( byte)0x30;
            _PLCCommand[30] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[0];                     // 软元件类型
            _PLCCommand[31] = Encoding.ASCII.GetBytes(analysis.Content1.AsciiCode)[1];
            _PLCCommand[32] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[0]; // 起始地址的地位
            _PLCCommand[33] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[1];
            _PLCCommand[34] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[2];
            _PLCCommand[35] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[3];
            _PLCCommand[36] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[4];
            _PLCCommand[37] = MelsecHelper.BuildBytesFromAddress(analysis.Content2, analysis.Content1)[5];
            _PLCCommand[38] = MelsecHelper.BuildBytesFromData(length)[0];                                               // 软元件点数
            _PLCCommand[39] = MelsecHelper.BuildBytesFromData(length)[1];
            _PLCCommand[40] = MelsecHelper.BuildBytesFromData(length)[2];
            _PLCCommand[41] = MelsecHelper.BuildBytesFromData(length)[3];

            return(OperateResult.CreateSuccessResult(_PLCCommand));
        }