0

I'm sending information to an Arduino Nano from an Android device. bulkTransfer returns the correct value (length of data transmitted) but the Arduino receives strange values. I think it is because the baud rate of the Arduino (9600) is different from the baud rate the Android is transmitting, but I can't find the way to know which is this value.

So I have tried to call controlTransfer to set the baud rate of the Android but controlTransfer always return -1. I don't understand why bulkTransfer works but controlTransfer fails.

I'm using this code:

public int sendData(byte[] nbyte) throws IOException, NullPointerException {
  if(nbyte == null) throw new NullPointerException("nbyte cannot be null");
  int result = this.usbDeviceConnection.controlTransfer(0x40, 0, 0, 0, null, 0, 0);// reset
  result = this.usbDeviceConnection.controlTransfer(0x40, 0, 1, 0, null, 0, 0);// clear Rx
  result = this.usbDeviceConnection.controlTransfer(0x40, 0, 2, 0, null, 0, 0);// clear Tx
  result =  this.usbDeviceConnection.controlTransfer(0x40, 0x03, 0x4138, 0, null, 0, 0);
  int len = this.usbDeviceConnection.bulkTransfer(this.usbEndpoint, nbyte, nbyte.length, 5000);
  if(len == -1) throw new IOException("Bulktransfer failed");
  return len;
}
dda
  • 6,030
  • 2
  • 25
  • 34
  • If I understand the Android USB interface right you are "talking" directly with the USB-virtual-serial chip in the Arduino without driver. Are you sure you are sending the correct data for initializing the USB chip? Do you have the chip reference manual or something else describing the USB commands? – Robert Mar 17 '18 at 16:08
  • I'm not sending any data to initialize the USB chip. The chip is CH340 https://cdn.sparkfun.com/datasheets/Dev/Arduino/Other/CH340DS1.PDF but I have no idea of how to do this. I thought that the initialization of the chip is done by calling controlTransfer method with the correct parameters but I can't find the correct ones – Enrique Sanchez Mar 19 '18 at 09:37
  • It looks like the Linux source code shows some details on how to communicate with the chip: https://sparks.gogo.co.nz/assets/_site_/downloads/CH340_LINUX.zip – Robert Mar 19 '18 at 11:20

0 Answers0