Beispiel #1
0
        /// <summary>
        /// 向PLC写入数据,数据格式为原始的字节类型
        /// </summary>
        /// <param name="address">初始地址</param>
        /// <param name="value">原始的字节数据</param>
        /// <example>
        /// 假设起始地址为D100,D100存储了温度,100.6℃值为1006,D101存储了压力,1.23Mpa值为123,D102,D103存储了产量计数,写入如下:
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="WriteExample2" title="Write示例" />
        /// 以下是写入不同类型数据的示例
        /// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="WriteExample1" title="Write示例" />
        /// </example>
        /// <returns>结果</returns>
        public override OperateResult Write(string address, byte[] value)
        {
            // 分析地址
            OperateResult <byte[]> coreResult = MelsecHelper.BuildWriteWordCoreCommand(address, value, McAnalysisAddress);

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

            // 核心交互
            OperateResult <byte[]> read = ReadFromCoreServer(PackMcCommand(coreResult.Content, NetworkNumber, NetworkStationNumber));

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

            // 错误码校验
            ushort ErrorCode = BitConverter.ToUInt16(read.Content, 9);

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

            // 成功
            return(OperateResult.CreateSuccessResult( ));
        }
Beispiel #2
0
        /// <summary>
        /// 根据类型地址以及需要写入的数据来生成指令头
        /// </summary>
        /// <param name="address">起始地址</param>
        /// <param name="value">数据值,对于写入位地址来说,应该传入{0x01,0x00,0x01} 通断通这样的数组</param>
        /// <param name="networkNumber">网络号</param>
        /// <param name="networkStationNumber">网络站号</param>
        /// <returns>解析后的指令</returns>
        public static OperateResult <byte[]> BuildWriteCommand(string address, byte[] value, byte networkNumber = 0, byte networkStationNumber = 0)
        {
            OperateResult <byte[]> coreResult = MelsecHelper.BuildWriteWordCoreCommand(address, value);

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

            return(OperateResult.CreateSuccessResult(PackMcCommand(coreResult.Content, networkNumber, networkStationNumber)));
        }
Beispiel #3
0
        private OperateResult WriteAddressData(McAddressData addressData, byte[] value)
        {
            // 创建核心报文
            byte[] coreResult = MelsecHelper.BuildWriteWordCoreCommand(addressData, value);

            // 核心交互
            OperateResult <byte[]> read = ReadFromCoreServer(PackMcCommand(coreResult, NetworkNumber, NetworkStationNumber));

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

            // 错误码校验
            ushort ErrorCode = BitConverter.ToUInt16(read.Content, 9);

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

            // 成功
            return(OperateResult.CreateSuccessResult( ));
        }