internal virtual void OpenInterface() { Log.Debug(nameof(CdcAcmSerialDriver), "claiming interfaces, count=" + _driver.Device.InterfaceCount); _controlInterface = _driver.Device.GetInterface(0); Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface); if (!_connection.ClaimInterface(_controlInterface, true)) { throw new UsbSerialException("Could not claim control interface."); } _controlEndpoint = _controlInterface.GetEndpoint(0); Log.Debug(nameof(CdcAcmSerialDriver), "Control endpoint direction: " + _controlEndpoint.Direction); Log.Debug(nameof(CdcAcmSerialDriver), "Claiming data interface."); _dataInterface = _driver.Device.GetInterface(1); Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface); if (!_connection.ClaimInterface(_dataInterface, true)) { throw new UsbSerialException("Could not claim data interface."); } _readEndpoint = _dataInterface.GetEndpoint(1); Log.Debug(nameof(CdcAcmSerialDriver), "Read endpoint direction: " + _readEndpoint.Direction); _writeEndpoint = _dataInterface.GetEndpoint(0); Log.Debug(nameof(CdcAcmSerialDriver), "Write endpoint direction: " + _writeEndpoint.Direction); }
public FtdiSerialPort(UsbManager manager, UsbDevice device, int portNumber) : base(manager, device, portNumber) { readEndpoint = UsbDevice.GetInterface(0).GetEndpoint(0); writeEndpoint = UsbDevice.GetInterface(0).GetEndpoint(1); maxPacketSize = readEndpoint.MaxPacketSize; }
public CardReader(UsbDevice device) { _device = device; _interface = device.GetInterface(0); for (var i = 0; i < _interface.EndpointCount; i++) { var endpoint = _interface.GetEndpoint(i); switch (endpoint.Direction) { case UsbAddressing.In when endpoint.Type == UsbAddressing.XferInterrupt: _interruptPipe = endpoint; break; case UsbAddressing.In when endpoint.Type == UsbAddressing.XferBulk: _bulkInPipe = endpoint; _receiveBuffer = new byte[_bulkInPipe.MaxPacketSize]; break; case UsbAddressing.Out when endpoint.Type == UsbAddressing.XferBulk: _bulkOutPipe = endpoint; break; } } SupportsInterrupt = _interruptPipe != null; }
public override void Open(UsbDeviceConnection connection) { if (Connection != null) { throw new IOException("Already open"); } Connection = connection; bool opened = false; try { if (connection.ClaimInterface(Device.GetInterface(PortNumber), true)) { Log.Debug(TAG, "claimInterface " + PortNumber + " SUCCESS"); } else { throw new IOException("Error claiming interface " + PortNumber); } Reset(); opened = true; _readEndpoint = Device.GetInterface(PortNumber).GetEndpoint(0); } finally { if (!opened) { Close(); Connection = null; } } }
/// <summary> /// Retreive bulk Enpoints from the interface /// </summary> /// <param name="usbIface"></param> /// <param name="epIn"></param> /// <param name="epOut"></param> /// <returns></returns> protected int InterfaceGetBulkEndpoints(UsbInterface usbIface, ref UsbEndpoint epIn, ref UsbEndpoint epOut) { int i; int ret; // We iterate over over the interface endpoint to find bulk in and out for (i = 0, ret = 0; (i < usbIface.EndpointCount) && (ret < 2); i++) { UsbEndpoint ep = usbIface.GetEndpoint(i); if (ep.Type == UsbAddressing.XferBulk) { if (ep.Direction == UsbAddressing.In) { epIn = ep; ret++; } else if (ep.Direction == UsbAddressing.Out) { epOut = ep; ret++; } } } // Did we find the two needed endpoints ? return((ret == 2) ? 0 : -1); }
private bool openCP2130() { if (connection.ClaimInterface(mInterface, true)) { Log.Info(CLASS_ID, "Interface succesfully claimed"); } else { Log.Info(CLASS_ID, "Interface could not be claimed"); return(false); } // Assign endpoints int numberEndpoints = mInterface.EndpointCount; for (int i = 0; i <= numberEndpoints - 1; i++) { UsbEndpoint endpoint = mInterface.GetEndpoint(i); if (endpoint.Type == UsbAddressing.XferBulk && endpoint.Direction == UsbAddressing.In) { inEndpoint = endpoint; } else { outEndpoint = endpoint; } } return(true); }
public override void Open(UsbDeviceConnection connection) { _connection = connection; _controlInterface = Device.GetInterface(0); if (!connection.ClaimInterface(_controlInterface, true)) { throw new Exception("Could not claim control interface."); } _controlEndpoint = _controlInterface.GetEndpoint(0); _dataInterface = Device.GetInterface(1); if (!connection.ClaimInterface(_dataInterface, true)) { throw new Exception("Could not claim data interface."); } if (Device.VendorId == 0x1cbe) { _writeEndpoint = _dataInterface.GetEndpoint(1); _readEndpoint = _dataInterface.GetEndpoint(0); } else { _writeEndpoint = _dataInterface.GetEndpoint(0); _readEndpoint = _dataInterface.GetEndpoint(1); } Init(); var buadSet = SetBaudrate(960000); }
public void Dispose() { if (NativeDevice != null) { NativeDevice.Dispose(); NativeDevice = null; } if (Connection != null) { Connection.Dispose(); Connection = null; } if (Endpoint != null) { Endpoint.Dispose(); Endpoint = null; } if (EndpointWrite != null) { EndpointWrite.Dispose(); EndpointWrite = null; } if (Manager != null) { Manager.Dispose(); Manager = null; } }
public override int Write(byte[] src, int timeoutMillis) { int errorCount = 0; UsbEndpoint endpoint = Device.GetInterface(PortNumber).GetEndpoint(1); int offset = 0; using (WriteBufferLock.Lock()) { while (offset < src.Length) { var writeLength = Math.Min(src.Length - offset, WriteBuffer.Length); var amtWritten = Connection.BulkTransfer(endpoint, src, offset, writeLength, timeoutMillis); if (amtWritten <= 0) { errorCount++; if (errorCount >= 3) { return(0); } Thread.Sleep(10); amtWritten = 0; } offset += amtWritten; } } return(offset); }
public SmartScopeUsbInterfaceXamarin(Context context, UsbManager usbManager, UsbDevice device) { Destroyed = false; if (!usbManager.HasPermission(device)) { Logger.Error("Permission denied"); throw new Exception("Device permission not obtained"); } UsbInterface interf = device.GetInterface(0); for (int i = 0; i < interf.EndpointCount; i++) { if (interf.GetEndpoint(i).EndpointNumber == 1) { dataEndpoint = interf.GetEndpoint(i); } else if (interf.GetEndpoint(i).EndpointNumber == 2) { commandWriteEndpoint = interf.GetEndpoint(i); } else if (interf.GetEndpoint(i).EndpointNumber == 3) { commandReadEndpoint = interf.GetEndpoint(i); } } usbConnection = usbManager.OpenDevice(device); usbConnection.ClaimInterface(interf, true); }
private void openInterface() { Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount); mControlInterface = mDevice.GetInterface(0); Log.Debug(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.ClaimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.GetEndpoint(0); Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction); Log.Debug(TAG, "Claiming data interface."); mDataInterface = mDevice.GetInterface(1); Log.Debug(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.ClaimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } mReadEndpoint = mDataInterface.GetEndpoint(1); Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction); mWriteEndpoint = mDataInterface.GetEndpoint(0); Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction); }
private void ParseForUsbDevices() { Int32 deviceIndex = 0; Int32 deviceCount = 0; Int32 interfaceIndex = 0; Int32 endpointIndex = 0; UsbDevice[] usbDevices = null; UsbInterface[][] usbInterfaces = null; UsbEndpoint[][][] usbEndpoints = null; // Get the USB devices (normally one in an Android device): usbDevices = new UsbDevice[Manager.DeviceList.Count]; foreach (KeyValuePair <string, UsbDevice> keyValuePair in Manager.DeviceList) { usbDevices[deviceIndex] = keyValuePair.Value; deviceIndex++; } deviceCount = deviceIndex; // Create usbInterface and usbEnpont lists: usbInterfaces = new UsbInterface[deviceCount][]; usbEndpoints = new UsbEndpoint[deviceCount][][]; // Loop all (one) devices and look for interfaces and endpoints: for (deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++) { if (usbDevices[deviceIndex].InterfaceCount > 0) { if (usbDevices[deviceIndex].ProductName == "INTEGRA-7") { Device = usbDevices[deviceIndex]; usbInterfaces[deviceIndex] = new UsbInterface[usbDevices[deviceIndex].InterfaceCount]; for (interfaceIndex = 0; interfaceIndex < usbDevices[deviceIndex].InterfaceCount; interfaceIndex++) { usbInterfaces[deviceIndex][interfaceIndex] = usbDevices[deviceIndex].GetInterface(interfaceIndex); } usbEndpoints[deviceIndex] = new UsbEndpoint[usbDevices[deviceIndex].InterfaceCount][]; for (interfaceIndex = 0; interfaceIndex < usbDevices[deviceIndex].InterfaceCount; interfaceIndex++) { usbEndpoints[deviceIndex][interfaceIndex] = new UsbEndpoint[usbInterfaces[deviceIndex][interfaceIndex].EndpointCount]; Interface = usbInterfaces[deviceIndex][interfaceIndex]; for (endpointIndex = 0; endpointIndex < usbInterfaces[deviceIndex][interfaceIndex].EndpointCount; endpointIndex++) { usbEndpoints[deviceIndex][interfaceIndex][endpointIndex] = usbInterfaces[deviceIndex][interfaceIndex].GetEndpoint(endpointIndex); if (usbEndpoints[deviceIndex][interfaceIndex][endpointIndex].Direction == Android.Hardware.Usb.UsbAddressing.Out) { OutputEndpoint = usbEndpoints[deviceIndex][interfaceIndex][endpointIndex]; } else { InputEndpoint = usbEndpoints[deviceIndex][interfaceIndex][endpointIndex]; } } } } } } }
public override void Open(UsbDeviceConnection connection) { if (mConnection != null) { throw new IOException("Already opened."); } mConnection = connection; bool opened = false; bool controlInterfaceFound = false; try { for (var i = 0; i < mDevice.InterfaceCount; i++) { mControlInterface = mDevice.GetInterface(i); if (mControlInterface.InterfaceClass == UsbClass.Comm) { if (!mConnection.ClaimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface"); } (Driver as STM32SerialDriver).mCtrlInterf = i; controlInterfaceFound = true; break; } } if (!controlInterfaceFound) { throw new IOException("Could not claim control interface"); } for (var i = 0; i < mDevice.InterfaceCount; i++) { mDataInterface = mDevice.GetInterface(i); if (mDataInterface.InterfaceClass == UsbClass.CdcData) { if (!mConnection.ClaimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface"); } mReadEndpoint = mDataInterface.GetEndpoint(1); mWriteEndpoint = mDataInterface.GetEndpoint(0); opened = true; break; } } if (!opened) { throw new IOException("Could not claim data interface."); } } finally { if (!opened) { mConnection = null; } } }
protected override int ReadInternal(byte[] dest, int timeoutMillis) { UsbEndpoint endpoint = UsbDevice.GetInterface(0).GetEndpoint(0); if (ENABLE_ASYNC_READS) { int readAmt; lock (mInternalReadBufferLock) { // mReadBuffer is only used for maximum read size. readAmt = Math.Min(dest.Length, mInternalReadBuffer.Length); } UsbRequest request = new UsbRequest(); request.Initialize(Connection, endpoint); ByteBuffer buf = ByteBuffer.Wrap(dest); if (!request.Queue(buf, readAmt)) { throw new IOException("Error queueing request."); } UsbRequest response = Connection.RequestWait(); if (response == null) { throw new IOException("Null response"); } int payloadBytesRead = buf.Position() - MODEM_STATUS_HEADER_LENGTH; if (payloadBytesRead > 0) { //Log.Debug(TAG, HexDump.DumpHexString(dest, 0, Math.Min(32, dest.Length))); return(payloadBytesRead); } else { return(0); } } else { int totalBytesRead; lock (mInternalReadBufferLock) { int readAmt = Math.Min(dest.Length, mInternalReadBuffer.Length); totalBytesRead = Connection.BulkTransfer(endpoint, mInternalReadBuffer, readAmt, timeoutMillis); if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) { throw new IOException("Expected at least " + MODEM_STATUS_HEADER_LENGTH + " bytes"); } return(FilterStatusBytes(mInternalReadBuffer, dest, totalBytesRead, endpoint.MaxPacketSize)); } } }
public override void Open(UsbDeviceConnection connection) { if (_connection != null) { throw new UsbSerialException("Already opened."); } _connection = connection; bool opened = false; try { for (int i = 0; i < _driver.Device.InterfaceCount; i++) { UsbInterface usbIface = _driver.Device.GetInterface(i); Log.Debug(nameof(Cp21xxSerialDriver), _connection.ClaimInterface(usbIface, true) ? $"claimInterface {i} SUCCESS" : $"claimInterface {i} FAIL"); } UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1); for (int i = 0; i < dataIface.EndpointCount; i++) { UsbEndpoint ep = dataIface.GetEndpoint(i); if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)) { if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) { _readEndpoint = ep; } else { _writeEndpoint = ep; } } } SetConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE); SetConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS); SetConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE); opened = true; } finally { if (!opened) { try { Close(); } catch (UsbSerialException) { //Swallowing this exception for now } } } }
public override void Open(UsbDeviceConnection connection) { if (_connection != null) { throw new UsbSerialException("Already opened."); } _connection = connection; bool opened = false; try { for (int i = 0; i < _driver.Device.InterfaceCount; i++) { UsbInterface usbIface = _driver.Device.GetInterface(i); Log.Debug(nameof(Ch34xSerialDriver), _connection.ClaimInterface(usbIface, true) ? $"claimInterface {i} SUCCESS" : $"claimInterface {i} FAIL"); } UsbInterface dataIface = _driver.Device.GetInterface(_driver.Device.InterfaceCount - 1); for (int i = 0; i < dataIface.EndpointCount; i++) { UsbEndpoint ep = dataIface.GetEndpoint(i); if (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK)) { if (ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) { _readEndpoint = ep; } else { _writeEndpoint = ep; } } } Initialize(); BaudRate = DEFAULT_BAUD_RATE; opened = true; } finally { if (!opened) { try { Close(); } catch (UsbSerialException) { //Swallowing this exception for now } } } }
public UsbDataBinder(UsbManager manager, UsbDevice device) { // TODO Auto-generated constructor stub mUsbManager = manager; mDevice = device; mIntf = mDevice.GetInterface(0); mEndpoint = mIntf.GetEndpoint(0); mConnection = mUsbManager.OpenDevice(mDevice); }
public override void Open() { bool openedSuccessfully = false; try { CreateConnection(); for (int i = 0; i < UsbDevice.InterfaceCount; i++) { UsbInterface usbIface = UsbDevice.GetInterface(i); if (Connection.ClaimInterface(usbIface, true)) { Log.Debug(TAG, "ClaimInterface " + i + " SUCCESS"); } else { Log.Debug(TAG, "ClaimInterface " + i + " FAIL"); } } UsbInterface dataIface = UsbDevice.GetInterface(UsbDevice.InterfaceCount - 1); for (int i = 0; i < dataIface.EndpointCount; i++) { UsbEndpoint ep = dataIface.GetEndpoint(i); if (ep.Type == UsbAddressing.XferBulk) { // UsbConstants.USB_ENDPOINT_XFER_BULK if (ep.Direction == UsbAddressing.In) { // UsbConstants.USB_DIR_IN ReadEndpoint = ep; } else { WriteEndpoint = ep; } } } setConfigSingle(SILABSER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE); setConfigSingle(SILABSER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS); setConfigSingle(SILABSER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE); ResetParameters(); openedSuccessfully = true; } finally { if (openedSuccessfully) { IsOpened = true; StartUpdating(); } else { CloseConnection(); } } }
internal virtual void OpenSingleInterface() { _controlInterface = _driver.Device.GetInterface(0); Log.Debug(nameof(CdcAcmSerialDriver), "Control iface=" + _controlInterface); _dataInterface = _driver.Device.GetInterface(0); Log.Debug(nameof(CdcAcmSerialDriver), "data iface=" + _dataInterface); if (!_connection.ClaimInterface(_controlInterface, true)) { throw new UsbSerialException("Could not claim shared control/data interface."); } int endCount = _controlInterface.EndpointCount; if (endCount < 3) { Log.Debug(nameof(CdcAcmSerialDriver), "not enough endpoints - need 3. count=" + _controlInterface.EndpointCount); throw new UsbSerialException("Insufficient number of endpoints(" + _controlInterface.EndpointCount + ")"); } _controlEndpoint = null; _readEndpoint = null; _writeEndpoint = null; for (int i = 0; i < endCount; ++i) { UsbEndpoint ep = _controlInterface.GetEndpoint(i); if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_INT))) { Log.Debug(nameof(CdcAcmSerialDriver), "Found controlling endpoint"); _controlEndpoint = ep; } else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_IN)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))) { Log.Debug(nameof(CdcAcmSerialDriver), "Found reading endpoint"); _readEndpoint = ep; } else if ((ep.Direction.IsEqualTo(ExtendedUsbConstants.USB_DIR_OUT)) && (ep.Type.IsEqualTo(ExtendedUsbConstants.USB_ENDPOINT_XFER_BULK))) { Log.Debug(nameof(CdcAcmSerialDriver), "Found writing endpoint"); _writeEndpoint = ep; } if ((_controlEndpoint != null) && (_readEndpoint != null) && (_writeEndpoint != null)) { Log.Debug(nameof(CdcAcmSerialDriver), "Found all required endpoints"); break; } } if ((_controlEndpoint == null) || (_readEndpoint == null) || (_writeEndpoint == null)) { Log.Debug(nameof(CdcAcmSerialDriver), "Could not establish all endpoints"); throw new UsbSerialException("Could not establish all endpoints"); } }
public void AddDescriptor(ReadOnlySpan <byte> descriptor) { var offset = 0; UsbConfiguration? configuration = null; UsbAlternateInterface?alternateInterface = null; while (offset != descriptor.Length) { BytesToStruct(descriptor, offset, out UsbCommonDescriptor common); switch (common.bDescriptorType) { case UsbDescriptorType.USB_CONFIGURATION_DESCRIPTOR_TYPE: if (configuration != null) { throw new ArgumentException("duplicate USB_CONFIGURATION_DESCRIPTOR_TYPE"); } BytesToStruct(descriptor, offset, out UsbConfigurationDescriptor config); configuration = new UsbConfiguration(config); Configurations.Add(config.bConfigurationValue, configuration); break; case UsbDescriptorType.USB_INTERFACE_DESCRIPTOR_TYPE: if (configuration == null) { throw new ArgumentException("expected USB_CONFIGURATION_DESCRIPTOR_TYPE"); } BytesToStruct(descriptor, offset, out UsbInterfaceDescriptor iface); if (iface.bAlternateSetting == 0) { configuration.Interfaces[iface.bInterfaceNumber] = new UsbInterface(); } alternateInterface = new UsbAlternateInterface(iface); configuration.Interfaces[iface.bInterfaceNumber].Alternates[iface.bAlternateSetting] = alternateInterface; break; case UsbDescriptorType.USB_ENDPOINT_DESCRIPTOR_TYPE: if (alternateInterface == null) { throw new ArgumentException("expected USB_INTERFACE_DESCRIPTOR_TYPE"); } BytesToStruct(descriptor, offset, out UsbEndpointDescriptor ep); var endpoint = new UsbEndpoint(ep); switch (endpoint.TransferType) { case UsbEndpointType.USB_ENDPOINT_TYPE_CONTROL: alternateInterface.Endpoints.Add((byte)(ep.bEndpointAddress & 0x0f), endpoint); break; default: alternateInterface.Endpoints.Add((byte)(ep.bEndpointAddress & 0x8f), endpoint); break; } break; } offset += common.bLength; } }
public ReadingRun(UsbDeviceConnection connection, UsbEndpoint readEndpoint, UsbEndpoint writeEndpoint, Handler handler, CancellationToken token, int channel = -1) { _connection = connection; _readEndpoint = readEndpoint; _writeEndpoint = writeEndpoint; _handler = handler; _token = token; _scanMode = channel < 0; _channel = !_scanMode ? channel : 0; }
public void Close() { connection = null; fd = -1; usbdev = null; usbIface = null; ep_in = null; ep_out = null; max_pkt_size = 0; }
private bool OpenCP2102() { if (connection.ClaimInterface(mInterface, true)) { Log.Info(CLASS_ID, "Interface succesfully claimed"); } else { Log.Info(CLASS_ID, "Interface could not be claimed"); return(false); } // Assign endpoints int numberEndpoints = mInterface.EndpointCount; for (int i = 0; i <= numberEndpoints - 1; i++) { UsbEndpoint endpoint = mInterface.GetEndpoint(i); if (endpoint.Type == UsbAddressing.XferBulk && endpoint.Direction == UsbAddressing.In) { inEndpoint = endpoint; } else { outEndpoint = endpoint; } } // Default Setup if (SetControlCommand(CP210x_IFC_ENABLE, CP210x_UART_ENABLE, null) < 0) { return(false); } SetControlCommand(CP210x_SET_BAUDDIV, BAUD_RATE_GEN_FREQ / DEFAULT_BAUDRATE, null); //SetBaudRate(DEFAULT_BAUDRATE); //if (SetControlCommand(CP210x_SET_LINE_CTL, CP210x_LINE_CTL_DEFAULT, null) < 0) //{ // return false; //} SetFlowControl(UsbSerialInterface.FLOW_CONTROL_OFF); //SetControlCommand(CP210x_SET_MHS, MCR_ALL | CP210x_MHS_DTR_OFF | CP210x_MHS_RTS_OFF, null); if (SetControlCommand(CP210x_SET_MHS, CP210x_MHS_DEFAULT, null) < 0) { return(false); } PurgeHwBuffers(true, true); return(true); }
private void openInterface() { Log.Debug(TAG, "claiming interfaces, count=" + mDevice.InterfaceCount); mControlInterface = mDevice.GetInterface(0); Log.Debug(TAG, "Control iface=" + mControlInterface); // class should be USB_CLASS_COMM if (!mConnection.ClaimInterface(mControlInterface, true)) { throw new IOException("Could not claim control interface."); } mControlEndpoint = mControlInterface.GetEndpoint(0); Log.Debug(TAG, "Control endpoint direction: " + mControlEndpoint.Direction); Log.Debug(TAG, "Claiming data interface."); mDataInterface = mDevice.GetInterface(1); Log.Debug(TAG, "data iface=" + mDataInterface); // class should be USB_CLASS_CDC_DATA if (!mConnection.ClaimInterface(mDataInterface, true)) { throw new IOException("Could not claim data interface."); } for (int i = 0; i < mDataInterface.EndpointCount; ++i) { UsbEndpoint ep = mDataInterface.GetEndpoint(i); if ((ep.Direction == UsbAddressing.In) && (ep.Type == UsbAddressing.XferBulk)) { Log.Debug(TAG, "Found reading endpoint"); mReadEndpoint = ep; } else if ((ep.Direction == UsbAddressing.Out) && (ep.Type == UsbAddressing.XferBulk)) { Log.Debug(TAG, "Found writing endpoint"); mWriteEndpoint = ep; } } //mReadEndpoint = mDataInterface.GetEndpoint(1); //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction); //mWriteEndpoint = mDataInterface.GetEndpoint(0); //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction); //mReadEndpoint = mDataInterface.GetEndpoint(0); //Log.Debug(TAG, "Read endpoint direction: " + mReadEndpoint.Direction); //mWriteEndpoint = mDataInterface.GetEndpoint(1); //Log.Debug(TAG, "Write endpoint direction: " + mWriteEndpoint.Direction); }
private void getEndpoint(UsbDeviceConnection connection, UsbInterface intf) { if (intf.GetEndpoint(1) != null) { epOut = intf.GetEndpoint(1); } if (intf.GetEndpoint(0) != null) { epIn = intf.GetEndpoint(0); } }
public override int Read(byte[] dest, int timeoutMilliseconds) { UsbEndpoint endpoint = _driver.Device.GetInterface(0).GetEndpoint(0); if (ENABLE_ASYNC_READS) { int readAmt; lock (_readBufferLock) { readAmt = Math.Min(dest.Length, _readBuffer.Length); } var request = new UsbRequest(); request.Initialize(_connection, endpoint); var buf = ByteBuffer.Wrap(dest); if (!request.Queue(buf, readAmt)) //TODO: Must fix this { throw new UsbSerialException("Error queueing request."); } UsbRequest response = _connection.RequestWait(); if (response == null) { throw new UsbSerialException("Null response"); } int payloadBytesRead = buf.Position() - MODEM_STATUS_HEADER_LENGTH; if (payloadBytesRead > 0) { Log.Debug(nameof(FtdiSerialDriver), HexDump.DumpHexString(dest, 0, Math.Min(32, dest.Length))); return(payloadBytesRead); } else { return(0); } } else { lock (_readBufferLock) { int readAmt = Math.Min(dest.Length, _readBuffer.Length); int totalBytesRead = _connection.BulkTransfer(endpoint, _readBuffer, readAmt, timeoutMilliseconds); if (totalBytesRead < MODEM_STATUS_HEADER_LENGTH) { throw new UsbSerialException($"Expected at least {MODEM_STATUS_HEADER_LENGTH} bytes"); } return(FilterStatusBytes(_readBuffer, dest, totalBytesRead, endpoint.MaxPacketSize)); } } }
public AndroidUsbEndpoint(UsbEndpoint usbEndpoint) { var isRead = usbEndpoint.Direction == UsbAddressing.In; var isWrite = usbEndpoint.Direction == UsbAddressing.Out; var isInterrupt = usbEndpoint.Type == UsbAddressing.XferInterrupt; IsRead = isRead; IsWrite = isWrite; IsInterrupt = isInterrupt; UsbEndpoint = usbEndpoint; PipeId = (byte)usbEndpoint.Address; }
protected void SetThreadsParams(UsbRequest request, UsbEndpoint endpoint) { writeThread.SetUsbEndpoint(endpoint); if (mr1Version) { workerThread.SetUsbRequest(request); } else { readThread.SetUsbEndpoint(request.Endpoint); } }
/** * 分配端点,IN | OUT,即输入输出;此处我直接用1为OUT端点,0为IN,当然你也可以通过判断 */ private void assignEndpoint() { if (myInterface.GetEndpoint(1) != null) { epOut = myInterface.GetEndpoint(1); } if (myInterface.GetEndpoint(0) != null) { epIn = myInterface.GetEndpoint(0); } //Log.Debug(TAG, getString(R.string.text)); }
public void Stop() { _cts.Cancel(); _cts = null; _handler.SendMessage(_handler.ObtainMessage((int)USBDeviceStatus.DeviceConnectionClosed)); _usbReadEndpoint = null; _usbWriteEndpoint = null; _usbConnection.Close(); _usbInterface = null; _usbConnection = null; _thread = null; //Device.DataReceived -= Device_DataReceived; //Device.Dispose(); //Device = null; }