internal static MultiProtocolRequest CreateRequest(IEnumerable <byte> data, ResponseInfo responseInfo)
 {
     return(new MultiProtocolRequest(default, RequestAddress.NONE, default, responseInfo)
 MultiProtocolRequest(byte deviceId, RequestAddress address, bool isWriteRequest, ResponseInfo responseInfo)
     : this(deviceId, address, isWriteRequest, responseInfo, new IDataEntity[0])
 {
 }
        MultiProtocolRequest(byte deviceId, RequestAddress address, bool isWriteRequest, ResponseInfo responseInfo, IEnumerable <IDataEntity> dataEntities)
        {
            DeviceId       = deviceId;
            Address        = address;
            IsWriteRequest = isWriteRequest;
            var addressInfo = Address.GetInfo();

            _responseInfo   = responseInfo;
            _hasLengthField = _responseInfo.ResponseFullLength.IsUnknown;

            if (address != RequestAddress.NONE)
            {
                _commandWords = new ushort[]
                {
                    (ushort)((DeviceId << 9) | ((~0x80) & Address.GetInfo().Address) | (IsWriteRequest ? 1 << 7 : 0)),
                    0x0000
                };
                _header = wordsToBytes(_commandWords).ToArray();
            }

            Serialized = serialize(dataEntities);

            IEnumerable <byte> serialize(IEnumerable <IDataEntity> entities)
            {
                if (address == RequestAddress.NONE)
                {
                    return(new byte[0]);
                }
                else
                {
                    var dataBytes = entities
                                    .Select(e => e.RawValue)
                                    .Flatten()
                                    .ToArray();
                    var zeroTrailShouldBeAdded = dataBytes.Length % 2 == 1;
                    var actualLength           = zeroTrailShouldBeAdded
                        ? dataBytes.Length + 1
                        : dataBytes.Length;
                    var requestBytes = new Enumerable <byte>()
                    {
                        _header,
                        IsWriteRequest // If there is data - we need to add length of the data
                            ? wordsToBytes(((ushort)actualLength).ToSequence())
                            : new byte[0],
                        dataBytes,
                        zeroTrailShouldBeAdded
                            ? new byte[1]
                            : new byte[0]
                    };

                    requestBytes.Add(wordsToBytes(_checksum.ComputeChecksum(requestBytes).ToSequence()));

                    return(requestBytes);
                }
            }
        }
 MultiProtocolRequest(ResponseInfo responseInfo)
     : this(default, RequestAddress.NONE, default, responseInfo, new IDataEntity[0])
 {
 }