public Read ( |
||
buffer | The buffer to store the recieved data in. | |
offset | int | The position in buffer to start storing the data. |
count | int | The maximum number of bytes to receive. |
timeout | int | Maximum time to wait for the transfer to complete. If the transfer times out, the IO operation will be cancelled. |
transferLength | int | Number of bytes actually transferred. |
return | ErrorCode |
public async Task<bool> OpenAsync() { if (isOpen) return true; if (device.Open()) { // 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 = device 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); } pinConfig = device.OpenEndpointWriter(WriteEndpointID.Ep01); pinState = device.OpenEndpointReader(ReadEndpointID.Ep01); peripheralConfig = device.OpenEndpointWriter(WriteEndpointID.Ep02); peripheralReceive = device.OpenEndpointReader(ReadEndpointID.Ep02); isOpen = true; pinListenerTask = new Task(() => { while (isOpen) { byte[] buffer = new byte[41]; int len = 0; try { ErrorCode error; error = pinState.Read(buffer, 100, out len); if (error == ErrorCode.Success) PinEventDataReceived?.Invoke(buffer); else if(error != ErrorCode.IoTimedOut) Debug.WriteLine("Pin Data Read Failure: " + error); } catch (Exception ex) { Debug.WriteLine("Exception: " + ex.Message); } if (updateDelay > 1) Task.Delay(updateDelay).Wait(); } }); pinListenerTask.Start(); return true; } else { return false; } }
private ReadEndpointID GetEndpoint() { bool found = false; byte readerId = (byte)ReadEndpointID.Ep01; while (!found && (byte)readerId <= (byte)ReadEndpointID.Ep15) { _reader = _device.OpenEndpointReader((ReadEndpointID)readerId); byte[] buffer = new byte[8]; int length; ErrorCode ret = _reader.Read(buffer, 500, out length); found = (ret != ErrorCode.Win32Error); if (!found) { readerId++; } } return (ReadEndpointID)readerId; }