예제 #1
0
        /// <summary>
        /// Divides the buffer into chunks of 62 bytes and continuously send them. First byte in, first out.
        /// </summary>
        /// <param name="data">Data to be sent</param>
        public void write(byte[] data)
        {
            if (data.Length == 0)
            {
                return;
            }

            lock (WriteLock)
            {
                // Count the number of packets to be sent
                int nbNewPackets = data.Length / 62;
                if (data.Length % 62 != 0)
                {
                    nbNewPackets++;
                }

                // Create the packets
                for (int i = 0; i < nbNewPackets - 1 || (data.Length % 62 == 0 && i < nbNewPackets); i++)
                {
                    byte[] buffer = new byte[64];
                    buffer[0] = 0x3f;   // See Progammer's Guide: MSP430 USB API Stack for CDC/HID/MSC v2.0, page 39, section 7.3
                    buffer[1] = 62;
                    for (int j = 0; j < 62; j++)
                    {
                        buffer[j + 2] = data[j];
                    }
                    packetsOut.Add(buffer);
                }

                if (data.Length % 62 != 0)
                {
                    int len = data.Length % 62;

                    byte[] buffer = new byte[len + 2];
                    buffer[0] = 0x3f;   // See Progammer's Guide: MSP430 USB API Stack for CDC/HID/MSC v2.0, page 39, section 7.3
                    buffer[1] = (byte)len;
                    for (int j = 0; j < len; j++)
                    {
                        buffer[j + 2] = data[j];
                    }

                    packetsOut.Add(buffer);
                }

                // If there is only 1 packet, then no data is being sent
                // Therefore, initiate the sending of the packet
                if (!sendingData)
                {
                    sendingData = true;
                    _selectedDevice.Write(packetsOut.First(), OnWriteReport);
                }
            }
        }