Android Question USBconnection Bulktransfer

wroyw

Member
Licensed User
Hi,
I write my own CP210x class module.
At time I can get/set any parameters.
Write to my device works also ( I see it on my oscilloscope)
BUT when I need Bulktransfer to read data from my device I get always -1 .
The Inqueue count is okay but I can't read the data.
 

wroyw

Member
Licensed User
I know, but I need the direct write and read functions without events.
The communications works fine , but the reading of the received bytes doesn't work right.
I get as result of Bulktransfer 1 or -1 but not my requested count ...
The Inqueue count is correct (GET_COMM_STATUS)
 
Upvote 0

wroyw

Member
Licensed User
I think it work fine for me ...

Now I can scan the connected USB devices with short commands without events like this :
B4X:
   optDevice.OpenDevice(0,9600)
   optDevice.Purge(True,True)
   Dim answ As TReceiveBuffer = optDevice.SendDeviceCommand(Array As Short (0x2a,0x00,0x2a),18,250) ' (sendarray , receivecount , timeoutms)
   optDevice.CloseDevice

For the general communication I use the USBserial lib.
 
Upvote 0

wroyw

Member
Licensed User
You are right. It works well only sometimes.
What is the reason that the Bulktransfer read doesn't read the bytes from my CP210x device ?
The CP210x say 5 bytes in input queue, but Bulktranfer result is 1 or -1 but not 5 ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

wroyw

Member
Licensed User
I wrote the same in Android Studio and there it works fine . I get every time the right bytes from my queue and Bulktranfer return the right count of reading bytes ...
 
Upvote 0

wroyw

Member
Licensed User
Here is what I do :
B4X:
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
edt.append("USB device count :" + String.valueOf(deviceList.size()) + "\r\n");
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext()) {
  UsbDevice device = deviceIterator.next();
  UsbInterface devInterface = device.getInterface(0); 
  int devInterfaceID = devInterface.getId();
  UsbEndpoint OutEndPoint = devInterface.getEndpoint(0);
  UsbEndpoint InEndPoint = devInterface.getEndpoint(1);
  UsbDeviceConnection connection = manager.openDevice(device);
  connection.claimInterface(devInterface, true); 
  byte[] bytes;
  connection.controlTransfer(CP210x_REQTYPE_HOST2DEVICE, CP210x_IFC_ENABLE, CP210x_IFC_ON, devInterfaceID, null, 0,250);
  connection.controlTransfer(CP210x_REQTYPE_HOST2DEVICE, CP210x_SET_MHS, CP210x_MHS_DTR_ON, devInterfaceID, null, 0, 200); 
  int br = 3686400 / 9600;
  connection.controlTransfer(CP210x_REQTYPE_HOST2DEVICE, CP210x_SET_BAUDDIV, br, devInterfaceID, null, 0, 200);
  bytes = new byte[]{0x2a, 0x00, 0x2a}; 
  byte[] InBuff = new byte[100];
  connection.controlTransfer(CP210x_REQTYPE_DEVICE2HOST, CP210x_GET_COMM_STATUS, 0, devInterfaceID, InBuff, 0x0014, 200); 
  for (int i = 0; i < 20; i++) {
  connection.bulkTransfer(OutEndPoint, bytes, 3, 200);
  while (InBuff[9]!=18) {
    connection.controlTransfer(CP210x_REQTYPE_DEVICE2HOST, CP210x_GET_COMM_STATUS, 0, devInterfaceID, InBuff, 0x0014, 200);
  }
  int rc = connection.bulkTransfer(InEndPoint, InBuff, 18, 500); 
  if (rc == 18) {
   edt.append("18 Zeichen empfangen !\r\n");
  } else {
    edt.append("ERROR Zeichen empfangen !\r\n");
  }
  connection.close();
}
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is the library code:
B4X:
  public int BulkTransfer (UsbEndpoint Endpoint, byte[] Buffer, int Length, int Timeout) {
     return connection.bulkTransfer(Endpoint, Buffer, Length, Timeout);
   }

As you can see it just calls the native API. There is no logic here. The same is true for controlTransfer.

You can wrap this code in your own library if you like and use it.
 
Upvote 0
Top