Ejemplo n.º 1
0
        public static void CreateUSBPacketByteArray(UsbPacketType packetType, Int16 packetId, Int32 dataSize, byte[] data, ref byte[] destinationArray)
        {
            /// TODO: The size of the array should not exceed device.packetsize. throw exception if it is

            if (destinationArray == null)
            {
                // The USB Packet header (12 bytes) and data amounting to the size of the data buffer
                destinationArray = new byte[GarminUSBConstants.PACKET_HEADER_SIZE + dataSize];
            }
            else
            {
                Array.Clear(destinationArray, 0, destinationArray.Length);
                Array.Resize(ref destinationArray, GarminUSBConstants.PACKET_HEADER_SIZE + dataSize);
            }

            List <byte> bufferList = new List <byte>();

            bufferList.Add((byte)packetType);                        // byte 0
            bufferList.Add(new byte());                              // byte 1
            bufferList.AddRange(BitConverter.GetBytes(new Int16())); // byte 2 & 3
            bufferList.AddRange(BitConverter.GetBytes(packetId));    // byte 4 & 5
            bufferList.AddRange(BitConverter.GetBytes(new Int16())); // byte 6 & 7
            bufferList.AddRange(BitConverter.GetBytes(dataSize));    // byte 8,9,10,11
            if ((data != null) && (data.Length != 0))
            {
                bufferList.AddRange(data);                      // byte 12+
            }

            //Copy only the amount of bytes specified in dataSize
            Buffer.BlockCopy(bufferList.ToArray(), 0, destinationArray, 0, destinationArray.Length);
        }
        public byte[]        data;       // byte 12+

        public UsbPacket(UsbPacketType packetType, Int16 packetId, Int32 dataSize)
        {
            this.reserved1 = 0;
            this.reserved2 = 0;
            this.reserved3 = 0;

            this.packetType = packetType;
            this.packetId   = packetId;
            this.dataSize   = dataSize;
            this.data       = new byte[dataSize];
        }
        public UsbPacket(UsbPacketType packetType, Int16 packetId, Int32 dataSize, byte[] data)
        {
            this.reserved1 = 0;
            this.reserved2 = 0;
            this.reserved3 = 0;

            this.packetType = packetType;
            this.packetId   = packetId;
            this.dataSize   = dataSize;
            this.data       = new byte[dataSize];
            Buffer.BlockCopy(data, 0, this.data, 0, dataSize);
        }