Beispiel #1
0
    void Setup()
    {
        if (UsbDev != null)
        {
            return;
        }

        UsbDev = FindDevice();

        if (!UsbDev.SetConfiguration(1))
        {
            throw new M4ATXException("Failed to set device config");
        }

        if (!UsbDev.ClaimInterface(0))
        {
            throw new M4ATXException("Failed to claim interface #0");
        }

        if (!UsbDev.SetAltInterface(0))
        {
            throw new M4ATXException("Failed to set alternate interface to 0");
        }

        Writer = UsbDev.OpenEndpointWriter(WriteEndpointID.Ep01);
        Reader = UsbDev.OpenEndpointReader(ReadEndpointID.Ep01);
    }
        void trinket_OnConnect(object sender, EventArgs e)
        {
            notifyIcon.ShowBalloonTip(1000, "Trinket Fake USB Serial", "USB Device Connected", ToolTipIcon.Info);
            if (trinket.TryConnect())
            {
                try
                {
                    if (trinket.MyUsbDevice.IsOpen == false)
                    {
                        if (trinket.MyUsbDevice.Open() == false)
                        {
                            throw new Exception("Can't Open USB Device");
                        }
                    }

                    trinketReader = trinket.MyUsbDevice.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep01);
                    trinketReader.DataReceived       += new EventHandler <LibUsbDotNet.Main.EndpointDataEventArgs>(trinket_DataReceived);
                    trinketReader.DataReceivedEnabled = true;
                }
                catch (Exception ex)
                {
                    notifyIcon.ShowBalloonTip(1000, "Trinket Fake USB Serial", "Trinket Connecting Error: " + ex.Message, ToolTipIcon.Error);
                }
            }
        }
Beispiel #3
0
 internal void ClosePort()
 {
     lock (lock_access)
     {
         if (Is_opened)
         {
             oldDevice.Enqueue(srb_reader);
             srb_reader.Flush();
             srb_reader.Dispose();
             srb_reader = null;
             oldDevice.Enqueue(srb_writer);
             srb_writer.Flush();
             srb_writer.Dispose();
             srb_writer = null;
             IUsbDevice wholeUsbDevice = selected_device as IUsbDevice;
             if (!ReferenceEquals(wholeUsbDevice, null))
             {
                 // Release interface #0.
                 wholeUsbDevice.ReleaseInterface(0);
                 wholeUsbDevice.ReleaseInterface((1 | 0x80));
                 wholeUsbDevice.ReleaseInterface(2);
                 wholeUsbDevice.Close();
                 oldDevice.Enqueue(selected_device);
                 selected_device.Close();
                 selected_device = null;
             }
         }
     }
 }
Beispiel #4
0
        private void CloseDevice()
        {
            if (_sensorRead != null)
            {
                _sensorRead.DataReceivedEnabled = false;
                _sensorRead.DataReceived       -= SensorRead;
                _sensorRead.Device.Close();
                _sensorRead.Dispose();
            }
            _sensorRead = null;

            if (_controlRead != null)
            {
                _controlRead.DataReceivedEnabled = false;
                _controlRead.DataReceived       -= ControlRead;
                _controlRead.Dispose();
            }
            _controlRead = null;

            if (_controlWrite != null)
            {
                _controlWrite.Dispose();
                _controlWrite.Device.Close();
            }
            _controlWrite = null;
            UsbDevice.Exit();
            PostActions.Clear();
        }
        private void ButtonEp03Int_Click(object sender, EventArgs e)
        {
            try
            {
                if (!_ep3InterupEnable)
                {
                    _epReader = _device.OpenEndpointReader(ReadEndpointID.Ep03, 256, EndpointType.Interrupt);
                    _epReader.DataReceived       += EndpointReader_DataReceived;
                    _epReader.DataReceivedEnabled = true;

                    //var data = Encoding.ASCII.GetBytes("Test String");
                    //var lenTransfer = 0;
                    //_epWriter = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep03, EndpointType.Interrupt);
                    //_epWriter.Write(data, 500, out lenTransfer);

                    _ep3InterupEnable = true;
                    AppendEventLog("End Point 03 Interupt Enable");
                }
                else
                {
                    _ep3InterupEnable             = false;
                    _epReader.DataReceivedEnabled = false;
                    _epReader.DataReceived       -= EndpointReader_DataReceived;
                    _epReader.Abort();
                    AppendEventLog("End Point 03 Interupt Disable");
                }
            }
            catch (Exception ex)
            {
                PopupException(ex.Message);
            }
        }
Beispiel #6
0
        public bool Connect()
        {
            bool rv = false;

            rv = false;
            Disconnect();
            MyUSBDeviceFinder = new UsbDeviceFinder(DevVID, DevPID, DevRevision, DevSerialNumber, DevGUID);
            MyUSBDevice       = UsbDevice.OpenUsbDevice(MyUSBDeviceFinder);
            if (MyUSBDevice != null)
            {
                WholeUSBDevice = MyUSBDevice as IUsbDevice;
                if (!ReferenceEquals(WholeUSBDevice, null))
                {
                    WholeUSBDevice.SetConfiguration(1);
                    WholeUSBDevice.ClaimInterface(0);
                }
                MyUSBWriter = MyUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                MyUSBReader = MyUSBDevice.OpenEndpointReader(ReadEndpointID.Ep01, 4096);
                MyUSBReader.DataReceived       += MyUSBReader_DataReceived;
                MyUSBReader.DataReceivedEnabled = true;

                MyUSBSetupPacket.RequestType = (byte)(UsbCtrlFlags.RequestType_Vendor | UsbCtrlFlags.Recipient_Interface | UsbCtrlFlags.Direction_In);
                MyUSBSetupPacket.Request     = 1;
                MyUSBSetupPacket.Value       = 0;
                MyUSBSetupPacket.Length      = 4;

                bkThread = new System.Threading.Thread(this.Th_DoWork);
                bkThExit = false;
                bkThread.Start();
                rv = true;
            }
            return(rv);
        }
Beispiel #7
0
        public void CloseDevice()
        {
            if (_usbDevice == null || !_usbDevice.IsOpen)
            {
                return;
            }
            if (_epReader != null)
            {
                _epReader.Dispose();
                _epReader = null;
            }
            if (_epWriter != null)
            {
                _epWriter.Abort();
                _epWriter.Dispose();
                _epWriter = null;
            }

            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.
            var wholeUsbDevice = _usbDevice as IUsbDevice;

            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                wholeUsbDevice.ReleaseInterface(0); // Release interface #0.
            }
            _usbDevice.Close();
            _usbDevice = null;
        }
Beispiel #8
0
    public static void Update(Record Record)
    {
        if (MyUsbDevice == null)
        {
            /* we failed to connect to M4ATX device, nothing to do here */
            return;
        }

        UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

        // specify data to send
        ec = writer.Write(new byte[] { 0x81, 0x00 }, 5000, out bytesWritten);

        if (ec != ErrorCode.None)
        {
            throw new Exception("M4ATX: Error sending command: " + ec);
        }

        // open read endpoint 1.
        UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);

        // If the device hasn't sent data in the last 5 seconds,
        // a timeout error (ec = IoTimedOut) will occur.
        reader.Read(readBuffer, 3000, out bytesRead);

        if (ec != ErrorCode.None || bytesRead != readBuffer.Length)
        {
            var msg = string.Format("M4ATX: Error reading result, error {0}, got {1} bytes",
                                    ec, bytesRead);
            throw new Exception(msg);
        }

        Record.Set(Record.DataPoint.M4ATXTemperature, readBuffer[12]);
        Record.Set(Record.DataPoint.M4ATXVoltageIn, readBuffer[2]);
    }
Beispiel #9
0
        public override bool Open()
        {
            try
            {
                UsbDeviceFinder MyUsbFinder   = new UsbDeviceFinder(((UsbConfigModel)Configuration).PID, ((UsbConfigModel)Configuration).VID);
                UsbRegistry     myUsbRegistry = UsbDevice.AllWinUsbDevices.Find(MyUsbFinder);

                if (myUsbRegistry is null)
                {
                    return(false);
                }
                // Open this usb device.

                _dev = UsbDevice.OpenUsbDevice(MyUsbFinder) as IUsbDevice;

                _dev.SetConfiguration(1);

                _dev.ClaimInterface(0);
                _writer = _dev.OpenEndpointWriter(WriteEndpointID.Ep01);
                _reader = _dev.OpenEndpointReader(ReadEndpointID.Ep01);
            }
            catch (Exception exp)
            {
                throw new Exception(exp.ToString());
            }
            return(true);
        }
        /// <summary>
        /// Initializes the device via LibUSB, and sets the refresh interval of the controller data
        /// </summary>
        /// <param name="Interval">The interval that the device is polled for information.</param>
        public void Init(int Interval)
        {
            //ErrorCode ec = ErrorCode.None;

            // Find and open the usb device.
            MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
            if (MyUsbDevice == null)
            {
                throw new Exception("Device Not Found.");
            }

            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;

            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                wholeUsbDevice.SetConfiguration(1);
                wholeUsbDevice.ClaimInterface(0);
            }

            reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
            writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

            ButtonMasks.InitializeMasks();

            SetPollingInterval(Interval);
            pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed);
            pollTimer.Start();

            TestLEDs();
            RefreshLEDState();
        }
Beispiel #11
0
        private void closeTestDevice(object state)
        {
            if (!ReferenceEquals(mUsbDevice, null))
            {
                if (mUsbDevice.IsOpen)
                {
                    mEP1Reader.DataReceived -= OnDataReceived;
                    stopReadWrite();
                    mUsbDevice.ActiveEndpoints.Clear();
                    UsbDevice.UsbErrorEvent -= OnUsbError;

                    // If this is a "whole" usb device (libusb-win32, linux libusb)
                    // it will have an IUsbDevice interface. If not (WinUSB) the
                    // variable will be null indicating this is an interface of a
                    // device.
                    IUsbDevice wholeUsbDevice = mUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }

                    mUsbDevice.Close();
                }
                mUsbDevice = null;
                mEP1Reader = null;
                mEP1Writer = null;
            }
            cmdOpenClose.Text = "Open";
            panDevice.Enabled = false;
        }
        public override void Close()
        {
            try {
                write_lock.WaitOne();

                if (ep_reader != null)
                {
                    // detach read event
                    ep_reader.DataReceivedEnabled = false;
                    ep_reader.DataReceived       -= (read_usb);
                }

                ep_reader = null;
                ep_writer = null;

                if (IsOpen())
                {
                    // close devices
                    usb_device.Close();
                    wholeUsbDevice.ReleaseInterface(1);
                    wholeUsbDevice.Close();
                }

                // release devices
                usb_device     = null;
                wholeUsbDevice = null;
                UsbDevice.Exit();
            } catch (Exception) {
                // Ignore everything
            } finally {
                write_lock.ReleaseMutex();
            }
        }
Beispiel #13
0
        public override void Close()
        {
            if (_reader != null)
            {
                _reader.Abort();
                _reader = null;
            }

            if (_writer != null)
            {
                _writer.Abort();
                _writer = null;
            }

            if (_usbDevice != null)
            {
                if (_usbDevice.IsOpen)
                {
                    _usbDevice.Close();
                }

                _usbDevice = null;
                UsbDevice.Exit();
            }
        }
Beispiel #14
0
        public void closeDevice()
        {
            if (mUsbDevice != null)
            {
                if (mUsbDevice.IsOpen)
                {
                    if (mEpReader != null)
                    {
                        mEpReader.DataReceivedEnabled = false;
                        mEpReader.DataReceived       -= mEp_DataReceived;
                        mEpReader.Dispose();
                        mEpReader = null;
                    }
                    if (mEpWriter != null)
                    {
                        mEpWriter.Abort();
                        mEpWriter.Dispose();
                        mEpWriter = null;
                    }

                    // If this is a "whole" usb device (libusb-win32, linux libusb)
                    // it will have an IUsbDevice interface. If not (WinUSB) the
                    // variable will be null indicating this is an interface of a
                    // device.
                    IUsbDevice wholeUsbDevice = mUsbDevice as IUsbDevice;
                    if (!ReferenceEquals(wholeUsbDevice, null))
                    {
                        // Release interface #0.
                        wholeUsbDevice.ReleaseInterface(0);
                    }
                    mUsbDevice.Close();
                    mUsbDevice = null;
                }
            }
        }
Beispiel #15
0
        public void Open()
        {
            _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_vendorId, _productId));

            if (_device != null)
            {
                IUsbDevice whole = _device as IUsbDevice;

                if (!ReferenceEquals(whole, null))
                {
                    whole.SetConfiguration(1);
                    whole.ClaimInterface(1);
                }

                //Set up the endpoints
                var hci = _device.OpenEndpointReader(ReadEndpointID.Ep01);
                _reader    = _device.OpenEndpointReader(ReadEndpointID.Ep02);
                _writer    = _device.OpenEndpointWriter(WriteEndpointID.Ep02);
                _isoReader = _device.OpenEndpointReader(ReadEndpointID.Ep03);
                _isoWriter = _device.OpenEndpointWriter(WriteEndpointID.Ep03);

                //Set up our read callback(s)
                hci.DataReceived              += hci_DataReceived;
                hci.DataReceivedEnabled        = true;
                _reader.DataReceived          += reader_DataReceived;
                _reader.DataReceivedEnabled    = true;
                _isoReader.DataReceived       += _isoReader_DataReceived;
                _isoReader.DataReceivedEnabled = true;

                //Reset the device
                Reset();
            }
        }
Beispiel #16
0
        public async Task <bool> GetDevice(UsbContext context)
        {
            Console.WriteLine("Looking for device ...");
            // Dump all devices and descriptor information to console output.
            foreach (var usbDevice in context.List())
            {
                if (usbDevice.TryOpen())
                {
                    if (usbDevice.Info.Manufacturer == mfrName)
                    {
                        device = usbDevice;
                        if (usbDevice.ClaimInterface(interfaceID))
                        {
                            monitorBuddyDevice = usbDevice;

                            // Console.WriteLine($"Device open, interface 0 claimed: {device.ToString()}");
                            endpointReader = monitorBuddyDevice.OpenEndpointReader(ReadEndpointID.Ep01, endpointBufferSize, EndpointType.Interrupt);

                            return(true);
                        }
                    }
                }
            }

            await Task.Delay(2000);

            return(false);
        }
Beispiel #17
0
        public static void InitializeUSBDevice()
        {
            // Use the USB device finder to find our Gamecube adapter
            _usbDevice = UsbDevice.OpenUsbDevice(_usbDeviceFinder);
            if (_usbDevice == null)
            {
                // If the gamecube adapter isn't plugged in OR is being used by another application, throw an exception.
                // Is there even a way to take control over the adapter from another device?
                throw new Exception(Strings.ERROR_ADAPTERNOTFOUND);
            }

            IUsbDevice wholeUsbDevice = _usbDevice as IUsbDevice;

            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                wholeUsbDevice.SetConfiguration(1); // Set the adapter to use config one
                wholeUsbDevice.ClaimInterface(0);   // Claim interface zero
            }

            // Set reader to read from endpoint 1 (apparently GCN adapters use endpoint 0x81, but this works?)
            _controllerReader = _usbDevice.OpenEndpointReader(ReadEndpointID.Ep01);

            // Write to endpoint 2
            _controllerWriter = _usbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);

            // begin polling command - https://gbatemp.net/threads/wii-u-gamecube-adapter-reverse-engineering-cont.388169/
            _controllerWriter.Write(Convert.ToByte(0x13), 5000, out int transferLength);
        }
Beispiel #18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LCTFDevice"/> class.
        /// </summary>
        /// <param name="underlyingWinUsbDevice">The underlying <see cref="UsbDevice"/>.</param>
        public LCTFDevice(UsbDevice underlyingWinUsbDevice)
        {
            this.usbDevice = underlyingWinUsbDevice ?? throw new ArgumentNullException(nameof(underlyingWinUsbDevice));

            this.InstanceId = underlyingWinUsbDevice.DevicePath;

            this.DeviceInfo = this.GetDeviceInfo();

            if (this.DeviceInfo.FirmwareVersion < 107)
            {
                throw new NotSupportedException("This library only supports LCTF firmware v1.07 or newer and cannot be used with older versions.");
            }

            this.WavelengthMin  = (int)(this.GetFloat((byte)CommandIndices.WavelengthMin) + 0.5f);
            this.WavelengthMax  = (int)(this.GetFloat((byte)CommandIndices.WavelengthMax) + 0.5f);
            this.WavelengthStep = (int)(this.GetFloat((byte)CommandIndices.WavelengthStep) + 0.5f);

            UsbDevice.UsbErrorEvent += this.UsbDevice_UsbErrorEvent;

            // Set up reader for interrupts
            this.interruptReader = this.usbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
            this.interruptReader.DataReceivedEnabled = true;
            this.interruptReader.DataReceived       += this.Reader_DataReceived;

            // Turn on all the normal features
            this.SetFilterEnable(true);
            this.SetAutotune(true);
            this.SetOUStatus(true);
        }
Beispiel #19
0
 public void Disconnect()
 {
     if (bkThread != null)
     {
         bkThExit = true;
         bkThInterrupt.Set();
         if (bkThread.ThreadState == System.Threading.ThreadState.Running)
         {
             bkThread.Join();
         }
     }
     bkThread = null;
     if (MyUSBDevice != null)
     {
         if (MyUSBDevice.IsOpen)
         {
             WholeUSBDevice = MyUSBDevice as IUsbDevice;
             if (!ReferenceEquals(WholeUSBDevice, null))
             {
                 WholeUSBDevice.ReleaseInterface(0);
             }
             MyUSBReader.DataReceivedEnabled = false;
             MyUSBReader.DataReceived       -= MyUSBReader_DataReceived;
             MyUSBDevice.Close();
         }
         MyUSBDevice       = null;
         WholeUSBDevice    = null;
         MyUSBDeviceFinder = null;
         MyUSBReader       = null;
         MyUSBWriter       = null;
     }
 }
Beispiel #20
0
        private static void InitalizeAndVerify()
        {
            bool Success = false;

            while (Success == false)
            {
                Writer = DI_2008.OpenEndpointWriter(WriteEndpointID.Ep01);
                Reader = DI_2008.OpenEndpointReader(ReadEndpointID.Ep01);



                //No clue what these do but they keep the device from hanging on program restarts #TrialAndError
                Writer.Abort();
                Writer.Reset();
                Writer.Flush();
                Reader.Abort();
                Reader.ReadFlush();
                Reader.Flush();
                Reader.Reset();
                InternalFunctions.Write("stop"); //Make sure the device wasnt left in a scan state

                try
                {
                    var DeviceResponding = InternalFunctions.Write("info 0");
                    Success = DeviceResponding.Contains("DATAQ");
                }
                catch { }
            }
        }
Beispiel #21
0
    private void Start()
    {
        var USBFinder = new UsbDeviceFinder(0x057E, 0x0337);

        GCNAdapter = UsbDevice.OpenUsbDevice(USBFinder);


        if (GCNAdapter != null)
        {
            reader = GCNAdapter.OpenEndpointReader(ReadEndpointID.Ep01);
            writer = GCNAdapter.OpenEndpointWriter(WriteEndpointID.Ep02);

            //prompt controller to start sending
            writer.Write(Convert.ToByte((char)19), 10, out transferLength);

            // PORT 1: bytes 02-09
            // PORT 2: bytes 11-17
            // PORT 3: bytes 20-27
            // PORT 4: bytes 29-36l
            ReadBuffer = new byte[37]; // 32 (4 players x 8) bytes for input, 5 bytes for formatting
            //WriteBuffer = new byte[5]; // 1 for command, 4 for rumble state
            //WriteBuffer[0] = 0x11;
            //WriteBuffer[1] = 0;
            reader.ReadThreadPriority = System.Threading.ThreadPriority.Highest;
            reader.Flush();
            gcThread = new Thread(() => inputUpdate(ref ReadBuffer));
            gcThread.Start();
        }
        else
        {
            UnityEngine.Debug.Log("ERROR: Could not detect device WUP-028 Gamecube Controller Adapter.");
        }
    }
        public bool OpenDevice()
        {
            MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
            if (MyUsbDevice == null)
            {
                return(false);
            }

            #region   当设备是一个 "whole" USB 时,我们的设备一般不会是此情况
            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.
            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                // This is a "whole" USB device. Before it can be used,
                // the desired configuration and interface must be selected.

                // Select config #1
                wholeUsbDevice.SetConfiguration(1);

                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
            }
            #endregion

            reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);
            reader.ReadBufferSize = nReadBufSize;

            writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
            return(true);
        }
Beispiel #23
0
        public async Task InitializeAsync()
        {
            if (disposed)
            {
                throw new Exception(DeviceBase.DeviceDisposedErrorMessage);
            }

            await Task.Run(() =>
            {
                //TODO: Error handling etc.
                UsbDevice.Open();

                //TODO: This is far beyond not cool.
                if (UsbDevice is MonoUsbDevice monoUsbDevice)
                {
                    monoUsbDevice.ClaimInterface(0);
                }
                else if (UsbDevice is WinUsbDevice winUsbDevice)
                {
                    //Doesn't seem necessary in this case...
                }
                else
                {
                    ((IUsbDevice)UsbDevice).ClaimInterface(0);
                }

                _UsbEndpointWriter = UsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);
                _UsbEndpointReader = UsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
                ReadPacketSize     = _UsbEndpointReader.EndpointInfo.Descriptor.MaxPacketSize;

                IsInitialized = true;
            });
        }
Beispiel #24
0
        private bool OpenCustomDevice(out WinUsbDevice usb, out UsbEndpointReader reader, out UsbEndpointWriter writer)
        {
            usb    = null;
            reader = null;
            writer = null;

            var device = UsbDevice.AllWinUsbDevices.FirstOrDefault(d => d.Vid == 0x1974 && d.Pid == 0x0001) as WinUsbRegistry;

            if (device != null)
            {
                if (device.Open(out usb))
                {
                    writer = usb.OpenEndpointWriter(WriteEndpointID.Ep01);
                    writer.Reset();
                    writer.Flush();

                    reader = usb.OpenEndpointReader(ReadEndpointID.Ep01);
                    reader.Reset();
                    reader.Flush();
                    reader.ReadBufferSize      = 32;
                    reader.DataReceived       += CustomDataReceived;
                    reader.DataReceivedEnabled = true;
                    return(true);
                }
            }
            return(false);
        }
        private void HeadsetReader_DataReceived(object sender, EndpointDataEventArgs e)
        {
            var buffer = new byte[e.Count];

            Array.Copy(e.Buffer, 0, buffer, 0, buffer.Length);
            UsbEndpointReader reader = sender as UsbEndpointReader;

            if (reader != null)
            {
                //Find the slot this belongs to
                int?index = null;
                foreach (var slot in _slots)
                {
                    if (slot.Value.DataReader == reader)
                    {
                        index = slot.Key;
                        break;
                    }
                }

                if (index.HasValue)
                {
                    if (HeadsetDataReceived != null)
                    {
                        HeadsetDataReceived(this, new DataReceivedEventArgs(index.Value, buffer));
                    }
                }
            }
        }
Beispiel #26
0
        /// <summary>
        /// Class constructor for the traq|paq
        /// </summary>
        public TraqpaqDevice()
        {
            // find the device
            traqpaqDeviceFinder = new UsbDeviceFinder(Constants.VID, Constants.PID);

            // open the device
            this.MyUSBDevice = UsbDevice.OpenUsbDevice(traqpaqDeviceFinder);

            // If the device is open and ready
            if (MyUSBDevice == null)
            {
                throw new TraqPaqNotConnectedException("Device not found");
            }

            this.reader = MyUSBDevice.OpenEndpointReader(ReadEndpointID.Ep01);
            this.writer = MyUSBDevice.OpenEndpointWriter(WriteEndpointID.Ep02);

            // create the battery object
            this.battery = new Battery(this);

            // create the otp reader object
            this.myOTPreader = new OTPreader(this);

            // create a saved track reader to get a list of saved tracks
            this.trackReader = new SavedTrackReader(this);
            this.trackList   = trackReader.trackList;

            // get the record table data
            tableReader = new RecordTableReader(this);
            tableReader.readRecordTable();
            this.recordTableList = tableReader.recordTable;
        }
Beispiel #27
0
        /// <summary>
        /// Open connection to the device
        /// </summary>
        public void OpenDevice()
        {
            MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

            if (MyUsbDevice == null)
            {
                throw new Exception("Device Not Found.");
            }
            // If this is a "whole" usb device (libusb-win32, linux libusb)
            // it will have an IUsbDevice interface. If not (WinUSB) the
            // variable will be null indicating this is an interface of a
            // device.

            wholeUsbDevice = MyUsbDevice as IUsbDevice;


            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                // This is a "whole" USB device. Before it can be used,
                // the desired configuration and interface must be selected.

                // Select config #1
                wholeUsbDevice.SetConfiguration(1);
                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
            }
            // open read endpoint 1.
            reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
            // open write endpoint 1.
            writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep02);
        }
Beispiel #28
0
        private void USB_init()
        {
            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;

            if (!ReferenceEquals(wholeUsbDevice, null))
            {
                // This is a "whole" USB device. Before it can be used,
                // the desired configuration and interface must be selected.

                // Select config #1
                wholeUsbDevice.SetConfiguration(1);

                // Claim interface #0.
                wholeUsbDevice.ClaimInterface(0);
            }
            reader1 = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep01);
            writer1 = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

            // Setup trigger

            USB_receive = new Thread(USBReceive);
            // open read endpoint 1.

            USB_receive.Start();

            button1Enable(true);
        }
        public bool Reset()
        {
            bool ret = true;

            Close();

            //Start message thread
            _messageThread = new Thread(new ThreadStart(_SendMessages));
            _messageThread.IsBackground = true;
            _messageThread.Start();

            //Find device
            _device = UsbDevice.OpenUsbDevice(new UsbDeviceFinder(_VENDOR_ID, _PRODUCT_ID)) as IUsbDevice;

            if (_device != null)
            {
                //Set up the incoming and outgoing endpoints

                _reader = _device.OpenEndpointReader(LibUsbDotNet.Main.ReadEndpointID.Ep03);
                _reader.DataReceived       += _reader_DataReceived;
                _reader.DataReceivedEnabled = true;

                lock (_writerLock)
                {
                    _writer = _device.OpenEndpointWriter(LibUsbDotNet.Main.WriteEndpointID.Ep02);
                }
            }
            else
            {
                ret = false;
            }

            return(ret);
        }
Beispiel #30
0
        public bool Open()
        {
            if (IsOpen)
            {
                return(false);
            }

            if (!_Device.Open())
            {
                return(false);
            }

            IUsbDevice whole = _Device as IUsbDevice;

            if (!ReferenceEquals(whole, null))
            {
                if (!whole.SetConfiguration(ConfigurationID) || !whole.ClaimInterface(InterfaceID))
                {
                    _Device.Close();
                    throw new PTPException("could not set USB device configuration and interface to " + ConfigurationID + " and " + InterfaceID + ", respectively");
                }
            }

            Writer = _Device.OpenEndpointWriter(WriterEndpointID);
            Reader = _Device.OpenEndpointReader(ReaderEndpointID);

            return(true);
        }