/// <summary> /// When the poll timer elapses, this function retrieves data from the controller, and passes /// the raw data to both the raw data event (so applications can analyze the raw data if needed), /// and passes the data to the private CheckStateChanged function. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <remarks> /// Please note - the poll timer is stopped until all events have been processed...so /// keep your event handlers optimized for analyzing the controller data. /// </remarks> private void pollTimer_Elapsed(object sender, ElapsedEventArgs e) { pollTimer.Stop(); byte[] buf = new byte[64]; reader.Read(buf); if (this.RawData != null) { RawData(this, buf); } CheckStateChanged(buf); Array.Copy(buf, 0, rawControlData, 0, rawControlData.Length);//changed here, since WinUSB.read doesn't return bytes read, maybe it always fills buffer with what is available? //moved this section out of CheckStateChanged since we want this to go off after the data from buf has been copied over to rawControlData if ((Enum.GetValues(typeof(ButtonEnum)).Length > 0) && (this.ButtonStateChanged != null)) { ButtonStateChanged(this, stateChangedArray); } //Console.WriteLine(ConvertToHex(rawControlData, rawControlData.Length)); pollTimer.Start(); }
/// <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; //HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\USB\VID_0A7B&PID_D000\6&688a3b8&0&1\Device Parameters\DeviceInterfaceGUIDs USBDeviceInfo[] details = USBDevice.GetDevices("{5C2B3F1A-E9B8-4BD6-9D19-8A283B85726E}"); USBDeviceInfo match = details.First(info => info.VID == 0x0A7B && info.PID == 0xD000); MyUsbDevice = new USBDevice(match); reader = MyUsbDevice.Interfaces.First().InPipe; writer = MyUsbDevice.Interfaces.First().OutPipe; byte[] buf = new byte[64]; reader.Read(buf);//can't remember why I do this. ButtonMasks.InitializeMasks(); SetPollingInterval(Interval); pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed); pollTimer.Start(); TestLEDs(); if (updateGearLights) { GearLightsRefresh((int)unchecked ((sbyte)buf[25])); } RefreshLEDState(); }
/// <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; //zadig creates a new device GUID everytime you install, so I placed file in //C:\Users\salds_000\Documents\Visual Studio 2013\Projects\steel_batallion-64\steelbattalionnet\winusb\usb_driver //check inf file for guid. Right clicking the inf file installs it. //after installation you can find this in regedit //HKEY_LOCAL_MACHINE\System\CurrentControlSet\Enum\USB\VID_0A7B&PID_D000\6&688a3b8&0&1\Device Parameters\DeviceInterfaceGUIDs USBDeviceInfo[] details = USBDevice.GetDevices("{5C2B3F1A-E9B8-4BD6-9D19-8A283B85726E}"); USBDeviceInfo match = details.First(info => info.VID == 0x0A7B && info.PID == 0xD000); MyUsbDevice = new USBDevice(match); reader = MyUsbDevice.Interfaces.First().InPipe; writer = MyUsbDevice.Interfaces.First().OutPipe; byte[] buf = new byte[64]; reader.Read(buf);//can't remember why I do this. ButtonMasks.InitializeMasks(); SetPollingInterval(Interval); pollTimer.Elapsed += new ElapsedEventHandler(pollTimer_Elapsed); pollTimer.Start(); TestLEDs(); if (updateGearLights) { GearLightsRefresh((int)unchecked ((sbyte)buf[25])); } RefreshLEDState(); }
public void FlushDataPipe() { //lock (usbLock) { if (!Destroyed) { try { dataEndpoint.Policy.PipeTransferTimeout = 10; dataEndpoint.Read(new byte[dataEndpoint.MaximumPacketSize]); dataEndpoint.Read(new byte[dataEndpoint.MaximumPacketSize]); } catch (USBException) { } finally { dataEndpoint.Policy.PipeTransferTimeout = USB_TIMEOUT; } } } }
private byte UsbPipeReaderMethod(USBPipe pipe) { byte[] rawByte = new byte[1]; pipe.Read(rawByte); return(rawByte[0]); }
// // Summary: // Reads numBytes into the data buffer at the specified offset. // // Parameters: // data: // The input data buffer. // // offset: // The zero based offset in the data buffer to write the data read. // // numBytes: // The number of bytes to read. // // Returns: // The receive status, IO_SUCCESS if all bytes were received // public SendReceiveStatus receiveBytes(byte[] data, int offset, int numBytes) { SendReceiveStatus status = SendReceiveStatus.IO_SUCCESS; int numRead = 0; int numToRead = numBytes; try { // // Exceptions: // System.ArgumentNullException: // The buffer passed is null. // // System.InvalidOperationException: // The specified port is not open. // // System.ArgumentOutOfRangeException: // The offset or count parameters are outside a valid region of the buffer being // passed. Either offset or count is less than zero. // // System.ArgumentException: // offset plus count is greater than the length of the buffer. // // System.TimeoutException: // No bytes were available to read. // do { numRead += inPipe.Read(data, offset + numRead, numToRead); numToRead = numBytes - numRead; } while (numRead < numBytes); } catch (System.ArgumentNullException) { status = SendReceiveStatus.IO_NULL_BUFFER; } catch (System.InvalidOperationException) { status = SendReceiveStatus.IO_PORT_ERROR; } catch (System.ArgumentOutOfRangeException) { status = SendReceiveStatus.IO_OUT_OF_RANGE; } catch (System.ArgumentException) { status = SendReceiveStatus.IO_OUT_OF_RANGE; } catch (System.TimeoutException) { status = SendReceiveStatus.IO_TIMEOUT; } catch (USBException e) { string message = e.Message; status = SendReceiveStatus.IO_PIPE_ERROR; } catch (Exception) { status = SendReceiveStatus.IO_OTHER_EXCEPTION; } return(status); }
// // read data on the IN endpoint associated to the HID interface // public List <byte> read(int size = -1, int timeout = -1) { byte[] packet = new byte[this.packet_size]; deviceIn.Read(packet); return(packet.ToList()); }