Android USB Serial Library incorrect data is sent to Arduino (signed byte issue?)

61 views Asked by At

So I'm using this https://github.com/mik3y/usb-serial-for-android library for Android connect to Arduino to the prone using USB OTG.

Android code to connect and send data to Arduino:

fun openUsbSerial(device: UsbDevice) {
    val driver = CdcAcmSerialDriver(device)
    val usbDeviceConnection: UsbDeviceConnection
    try {
        usbDeviceConnection = Utils.usbManager.openDevice(device) ?: return
    } catch (e: IllegalArgumentException) {
        e.printStackTrace()
        return
    }
    deviceUsbSerial = driver.ports[0]
    try {
        deviceUsbSerial?.let {
            it.open(usbDeviceConnection)
            it.setParameters(9600, 8, UsbSerialPort.STOPBITS_1, UsbSerialPort.PARITY_NONE)
            it.dtr = true // for arduino
            it.rts = true
        }
    } catch (e: IOException) {
        e.printStackTrace()
    }
}

fun writeDataToUsbSerial(buf: ByteArray) {
    deviceUsbSerial?.let {
        val state = it.write(buf, 1000)
    }
}

How I use it (need to pass two Int values):

val x = 0 // can be -100..100
val y = 0 // can be -100..100

val bytes = ByteBuffer.allocate(8) // int - 4 bytes in Java
   .putInt(x)
   .putInt(y)
   .array()

usbDeviceControl.writeDataToUsbSerial(bytes)

But when I check what it receives on Arduino side I get some wrong values, for example 40 40 40 40 40 40 40 40 (hex) when x = 0 and y = 0 instead of zeros:

enter image description here

enter image description here

What has to be change to correctly send data to such device?

0

There are 0 answers