Beispiel #1
0
        // Build up device name from manufacturer, product name, and
        // serial number strings. The index of each of these strings
        // is in a fixed location in the device descriptor. Then add
        // the device to the list box.
        void GetDeviceName(UsbDevice dev)
        {
            byte[] arbBuf;
            string strTag;

            try
            {
                dev.Open();

                // Get device descriptor
                // bmRequestType = 0 - standard device request
                // bRequest = 6 - Get_Descriptor
                // wValue: high byte = 1 - device; low byte = 0 - descriptor index
                // wIndex = 0 - not used
                // wLength = 18 - length of descriptor
                arbBuf = dev.ControlRead(0, 6, 0x100, 0, 18);

                // Entries 14, 15, and 16 have the index of the manufacturer,
                // product name, and serial number string descriptors, respectively.
                strTag  = GetStringDescriptor(dev, arbBuf[14]);
                strTag += '/';
                strTag += GetStringDescriptor(dev, arbBuf[15]);
                strTag += '/';
                strTag += GetStringDescriptor(dev, arbBuf[16]);

                // Assign the combined ID string to the Tag.
                dev.Tag = strTag;
            }
            catch (Exception exc)
            {
                dev.Tag = "Can't open: " + exc.Message;
            }

            dev.Close();                // done for now

            // Use the Tag as the value to be returned by ToString().
            // This makes it easy to add the UsbDevice to a list box.
            dev.GetMyString = delegate(UsbDevice device) { return((string)device.Tag); };

            // The list box will display ToString(), which is now the Tag.
            lstDevices.Items.Add(dev);
        }
Beispiel #2
0
        // Helper function to get a string descriptor from the USB device.
        string GetStringDescriptor(UsbDevice dev, int iString)
        {
            byte[] arbBuf;

            if (iString != 0)
            {
                // Get the string descriptor:
                // bmRequestType = 0 - standard device request
                // bRequest = 6 - Get_Descriptor
                // wValue: high byte = 3 - string; low byte = descriptor index
                // wIndex = 0x409 - language ID, US English
                // wLength = 255 - max length
                arbBuf = dev.ControlRead(0, 6, (ushort)(iString | 0x300), 0x409, 255);

                // arbBuf[0] = total length
                // arbBuf[1] = 3 (string)
                // arbBuf[2] = start of string (Unicode)
                if (arbBuf.Length >= 4)
                {
                    return(Encoding.Unicode.GetString(arbBuf, 2, arbBuf[0] - 2));
                }
            }
            return(string.Empty);
        }
Beispiel #3
0
        private void btnXfer_Click(object sender, EventArgs e)
        {
            byte   bmRequestType;
            byte   bRequest;
            byte   idPipe;
            ushort wValue;
            ushort wIndex;
            ushort wLength;

            byte[]       arbData;
            PipeStream   pipe;
            EndpointItem item;
            int          cbRead;

            try
            {
                item = (EndpointItem)drpEndpoint.SelectedItem;
                if (item.PipeId == -1)
                {
                    // Control transfer
                    bmRequestType = ReadHexByte(txtType);
                    bRequest      = ReadHexByte(txtRequest);
                    wValue        = ReadHexWord(txtValue);
                    wIndex        = ReadHexWord(txtIndex);
                    if (radIn.Checked)
                    {
                        wLength = ReadInt(txtLength);
                        arbData = m_Device.ControlRead(bmRequestType, bRequest, wValue, wIndex, wLength);
                        // Check for the case of getting a string descriptor
                        bmRequestType |= 0x80;
                        wValue       >>= 8;
                        if (bmRequestType == 0x80 && bRequest == 6 && wValue == 3 && arbData.Length > 2)
                        {
                            string str = Encoding.Unicode.GetString(arbData, 2, arbData.Length - 2);
                            LogMsg(str, MsgType.MSG_In);
                        }
                        else
                        {
                            LogData(arbData, arbData.Length, MsgType.MSG_In);
                        }
                    }
                    else
                    {
                        arbData = ReadOutData();
                        wLength = (ushort)arbData.Length;
                        m_Device.ControlWrite(bmRequestType, bRequest, wValue, wIndex, arbData);
                        LogData(arbData, arbData.Length, MsgType.MSG_Out);
                    }
                }
                else
                {
                    // bulk data transfer
                    idPipe = (byte)item.PipeId;
                    pipe   = m_Device.PipeStreams[idPipe];

                    if (radIn.Checked)
                    {
                        wLength = ReadInt(txtLength);
                        arbData = new byte[wLength];
                        if (wLength == 1)
                        {
                            // Test ReadByte()
                            cbRead = pipe.ReadByte();
                            if (cbRead == -1)
                            {
                                cbRead = 0;
                            }
                            else
                            {
                                arbData[0] = (byte)cbRead;
                                cbRead     = 1;
                            }
                        }
                        else
                        {
                            cbRead = pipe.Read(arbData, 0, arbData.Length);
                        }

                        LogData(arbData, cbRead, MsgType.MSG_In);
                    }
                    else
                    {
                        arbData = ReadOutData();
                        if (arbData.Length == 1)
                        {
                            pipe.WriteByte(arbData[0]);
                        }
                        else
                        {
                            pipe.Write(arbData, 0, arbData.Length);
                        }

                        LogData(arbData, arbData.Length, MsgType.MSG_Out);
                    }
                }
            }
            catch (Exception exc)
            {
                LogMsg(exc.Message, MsgType.MSG_Info);
                return;
            }
        }