You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

S7.Net中GetWordAt函数实现是否符合协议要求?

S7.Net.Protocol中GetWordAt函数的实现疑问
  • GetWordAt函数的设计目标是从IList<byte>数据中返回ushort类型值,按常规逻辑应该组合两个连续字节,但当前实现仅处理了单个字节:
    代码里的计算逻辑是(buf[index] << 8) + buf[index],相当于把同一个字节既当作高字节又当作低字节,完全没有用到buf[index+1],这和常规的双字节转无符号短整型逻辑不符。

  • 同类中的SetWordAt函数逻辑是正常的:它将ushort值拆分为高字节写入index位置,低字节写入index+1位置,符合标准的字节拆分逻辑。

  • 由于不了解PLC S7 200 Smart协议的具体要求,无法判断当前GetWordAt的异常实现是否是协议规定的特殊处理方式。

  • 该函数仅被S7WriteMultiple类的public static void ParseResponse(byte[] message, int length, DataItem[] dataItems)方法调用一次,最终由PLC类的public void Write(params DataItem[] dataItems)方法及其异步版本触发执行。

相关代码

namespace S7.Net.Protocol
{
    internal static class Serialization
    {
        public static ushort GetWordAt(IList<byte> buf, int index)
        {
            return (ushort)((buf[index] << 8) + buf[index]);
        }
        // ...
        public static void SetWordAt(ByteArray buffer, int index, ushort value)
        {
            buffer[index] = (byte)(value >> 8);
            buffer[index + 1] = (byte)value;
        }
    }
}

内容的提问来源于stack exchange,提问作者tanger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 05:35:29