Xamarin Android USB - using UsbSerialForAndroid from xamarin for a cp21x driver with two ports/ interfaces?

38 views Asked by At

am using the UsbSerialForAndroid,https://github.com/anotherlab/UsbSerialForAndroid for talking to devices over serial usb.

So far it worked fine and there were no issues with all differnt kinds if ports. Now, I’m trying to work with a device which uses CP21xx driver which has two ports - “standard” and “enhanced”, my messages work on “standard” and I have changed the part of code in Android/drivers/CP21xx…cs to set the interface to the one with “Standard”, but my messages don’t get a response from the device.

Now, if I use a 3rd party app “Serial usb terminal” https://play.google.com/store/apps/details?id=de.kai_morich.serial_usb_terminal&hl=en_CA&gl=US

, whose source code I don’t have,and thensend my messages, get the reply from the device, and then use the UsbSerialForAndroid example app again, I now get responses to all my messages.

I tried setting it to always the "standard" interface by checking the name but that still doesn't work unless I use the third party app. I have also tried using a usb to ttl to see what the third party app sends as a message and what my app sends and they are identical.

Note that the code or configuration doesn’t change for the UsbSerialForAndroid. Also, once working, even if I close the app or send messages after a very long time , I still get replies, till the time I don’t remove the physical usb connection.

If I unplug and reply the usb or power cycle the device that uses the CP21x driver, then I again have to use the third party app first to get the UsbSerialForAndroid sample app working again. I don’t know why it needs that third party app to connect and send the messages first although I don’t change anything in the UsbSerialForAndroid app between those two scenarios.

this is the open function for the cp21xx driver, I edited it o not use enhanced port

public override void Open(UsbDeviceConnection connection)
{
    if (mConnection != null)
    {
        throw new IOException("Already opened.");
    }

    mConnection = connection;
    Boolean opened = false;
    try
    {
        for (int i = 0; i < mDevice.InterfaceCount; i++)
        {
            UsbInterface usbIface = mDevice.GetInterface(i);
            if (mConnection.ClaimInterface(usbIface, true))
            {
                Log.Debug(TAG, $"claimInterface {i} SUCCESS");
            }
            else
            {
                Log.Debug(TAG, $"claimInterface {i} FAIL");
            }
        }

        UsbInterface dataIface = mDevice.GetInterface(mDevice.InterfaceCount - 1);
        if (dataIface.Name.Contains("Enhanced"))
        {
            dataIface = mDevice.GetInterface(mDevice.InterfaceCount - 2); ;
        }
        for (int i = 0; i < dataIface.EndpointCount; i++)
        {
            UsbEndpoint ep = dataIface.GetEndpoint(i);
            if (ep.Type == (UsbAddressing)UsbSupport.UsbEndpointXferBulk)
            {
                if (ep.Direction == (UsbAddressing)UsbSupport.UsbDirIn)
                {
                    mReadEndpoint = ep;
                }
                else
                {
                    mWriteEndpoint = 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);
        //            setParameters(DEFAULT_BAUD_RATE, DEFAULT_DATA_BITS, DEFAULT_STOP_BITS, DEFAULT_PARITY);
        opened = true;
    }
    finally
    {
        if (!opened)
        {
            try
            {
                Close();
            }
            catch (IOException e)
            {
                // Ignore IOExceptions during close()
            }
        }
    }
}

I don't know if there's anywhere else to set it to one of two interfaces in the UsbSerialForAndroid "UsbSerialForAndroid" app.

Has anyone ever worked with CP21xx drivers that have multiple ports/ interfaces with Android and usb serial ? Or anything similar that has two ports/ interfaces ?

looking for how to work with xamarin android usb serial to work with a port driver having two interfaces/ ports

0

There are 0 answers