示例#1
0
        // @brief open selected USB device
        //
        public bool usbOpenHIDInterface(int selectedIndex, showUSBData rxCallback)
        {
            allDevices = UsbDevice.AllDevices;  // get all usb devises
            try
            {
                allDevices[selectedIndex].Open(out MyUsbDevice);

                // Configure selected USB device for read/write operation
                // Configs -> InterfaceInfoList -> EndPointsInfoList -> Descriptor
                // Get number read data from endpoint 0x81
                UInt16 numEndPoints = MyUsbDevice.Configs[0].InterfaceInfoList[0].Descriptor.EndpointCount;
                UInt16 cnt;
                for (cnt = 0; cnt < (numEndPoints); cnt++) // temporary solution - looking for EP_1
                {
                    if (MyUsbDevice.Configs[0].InterfaceInfoList[0].EndpointInfoList[cnt].Descriptor.EndpointID == 0x81)
                    {
                        rxBuferSize = MyUsbDevice.Configs[0].InterfaceInfoList[0].EndpointInfoList[cnt].Descriptor.MaxPacketSize;
                    }
                    if (MyUsbDevice.Configs[0].InterfaceInfoList[0].EndpointInfoList[cnt].Descriptor.EndpointID == 0x01)
                    {
                        txBuferSize = MyUsbDevice.Configs[0].InterfaceInfoList[0].EndpointInfoList[cnt].Descriptor.MaxPacketSize;
                    }
                }
                wholeUsbDevice = MyUsbDevice as IUsbDevice;
                // Select config #1
                wholeUsbDevice.SetConfiguration(1);
                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
                // open write endpoint 1.
                writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                // open read endpoint 1.
                reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
                //Set Rx Callbac function
                libUSBRxCallback = rxCallback;
                setStetOffStream = true;
                readLibUSBThread = new Thread(readUSBHID);
                readLibUSBThread.Start();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#2
0
        //@brief Show USB Rx data
        //
        public void showUSBRx(byte[] data_array, int dataSize)
        {
            int    cnt;
            string rxString = "Read: " + DateTime.Now.ToString("hh:mm:ss:fff") + " - ";

            if (listBoxRxEP1.InvokeRequired)
            {
                showUSBData d = showUSBRx;
                Invoke(d, data_array, dataSize);
            }
            else
            {
                for (cnt = 0; cnt < dataSize; cnt++)
                {
                    rxString += Convert.ToString(data_array[cnt], 16) + "  ";
                }
                listBoxRxEP1.Items.Insert(0, rxString);
            }
        }