Esempio n. 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="Communication_Net45.Test\Documentation\Samples\Profinet\melsecTest.cs" region="WriteExample2" title="Write示例" />
        /// 以下是写入不同类型数据的示例
        /// <code lang="cs" source="Communication_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( ));
        }