Code example for UsbDeviceConnection

Methods: controlTransfer

2
        // horrific magic numbers gleaned from calculations based on the Linux kernel driver source. 
        // look for ch341.c, All the repetition of sending etc... is also based on the behaviour of ch341.c 
 
        //configure CH341: 
        byte buffer[] = new byte[8];
        connection.controlTransfer(USB_CONTROL_IN,  0x5f, 0x0000, 0x0000, buffer, 8, 0); //0x27 0x00
        connection.controlTransfer(USB_CONTROL_OUT, 0xa1, 0x0000, 0x0000, null, 0, 0);
 
        // set the baud rate to 57600 calculations used ch341_set_baudrate 
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x1312, 0x9803, null, 0, 0);
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x0f2c, 0x0010, null, 0, 0);
 
        connection.controlTransfer(USB_CONTROL_IN,  0x95, 0x2518, 0x0000, buffer, 8, 0); //0x56 0x00
        connection.controlTransfer(USB_CONTROL_OUT, 0x95, 0x2518, 0x0050, null, 0, 0);
 
        //Get Status: 
        connection.controlTransfer(USB_CONTROL_IN,  0x95, 0x0706, 0x0000, buffer, 8, 0); //0xff 0xee
 
        connection.controlTransfer(USB_CONTROL_OUT, 0xa1, 0x501f, 0xd90a, null, 0, 0);
 
        // set the baud rate to 57600 calculations used ch341_set_baudrate 
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x1312, 0x9803, null, 0, 0);
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x0f2c, 0x0010, null, 0, 0);
 
        // handshake: 
        connection.controlTransfer(USB_CONTROL_OUT, 0xa4, 0x00ff, 0x0000, null, 0, 0); // or maybe 0xffff?
 
        // Adaptronic would like 8-N-1, however there's no data on how to set it, the device defaults to 8n1, so hopefully it'll be OK :/ 
 
        //Get Status: 
        connection.controlTransfer(USB_CONTROL_IN,  0x95, 0x0706, 0x0000, buffer, 8, 0); //0x9f 0xee
 
        // handshake: 
        connection.controlTransfer(USB_CONTROL_OUT, 0xa4, 0x00ff, 0x0000, null, 0, 0); // or maybe 0xffff?
 
        // set the baud rate to 57600 calculations used ch341_set_baudrate 
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x1312, 0x9803, null, 0, 0);
        connection.controlTransfer(USB_CONTROL_OUT, 0x9a, 0x0f2c, 0x0010, null, 0, 0);
 
        return true; 
 
    } 
}