using PlcSiemens.O;
using PlcSiemens.Protocol.Common;

namespace PlcSiemens.Protocol.ReadData
{
    public class ReadItemResponse
    {
        public byte ReturnCode { get; set; }
        public byte TransportSize { get; set; }
        public ushort DataLength { get; set; }
        public byte[] Data { get; set; }

        public void Build(ByteBuffer buffer)
        {
            ReturnCode = buffer.PopByte();
            TransportSize = buffer.PopByte();
            DataLength = buffer.PopUshort();
            if (TransportSize != (byte)TsType.TsResOctet && TransportSize != (byte)TsType.TsResReal &&
                TransportSize != (byte)TsType.TsResBit)
                DataLength = (ushort)(DataLength >> 3);

            Data = buffer.PopBytes(DataLength);
            //如果读取数量为奇数则取出一条填充字节
            if (Data.Length % 2 != 0)
                buffer.PopByte();
        }
    }
}
