B4A Library PL2303 USB to RS232 adaptor driver

This is a driver for PL2303 based USB to RS232 adaptors. The PL2303 is a chip produced by Prolific Technology Inc. of Taiwan and is a very common chip to find in such adaptors. The driver requires version 0.92 or later of the USB Host library and needs a Honeycomb 3.1 or later device with USB host support .

This driver is not up to the standard that I would normally expect of myself. However it is unavoidably crippled by problems with the Android implementation of USB support and has been produced by reverse engineering a Linux driver as Prolific only provide chip operation details to their OEMs and do not publish an operation manual. Erel has suggested that I post it anyway as it is probably usable in at least some situations.

The specific problems that beset the Android USB support concern data transfer. There are two ways to transfer data in and out, asynchronously by queueing Requests and synchronously using the BulkTransfer method. Of the four possibilities two are broken in Android. Asynchronous reads will transfer fixed length arrays of data but there is no way of telling you how many bytes of the returned arrays contain valid data! I have also found a problem with synchronous writes. BlockTransfer is supposed to return a value of how many bytes it has written, or a -ve number if a fault occurred. However I was getting return values of -1 even though the data has been successfully sent! So we have a USB implementation that can't tell you how many bytes you have received nor how many bytes you have sent! :(

This driver implementation therefore has to use asynchronous writes and synchronous reads invoked by a Timer in order to provide some semblance of functionality. The use of synchronous reads limits the data rates that can be reliably supported without dropping data. If you have a PL2303 based adaptor then have a play yourself.
 

Attachments

  • USBpl2303_1.0.zip
    12.2 KB · Views: 2,722

dmtulsa

Member
Licensed User
Longtime User
FTDI Andriod USB-RS232

I don't know if anyone makes a USB-232 using this chip yet but I'd guess some company does. I'll look into it, If not I may design one myself.

FTDI Andriod info :FTDI Android
 

phoenix032

New Member
Licensed User
Longtime User
Hi,

I can't set UART data. I try to set and get data. It return only '0' all (rate,stopbits,parity,databits). This cable is work because I try to use with another demo app from play store already.

Thanks,
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Help with using PL2303 adpter

Hi there,

I am new on everything here, I wonder if i can get a help on the following:

I am in need to write an application for Android to handle serial comunication with a specific class of device. However i am having problems to make Agraham example to work.

I am using Genesis Skyworth tablet. Android 4. 2 usb (1 normal size 1 mini)

Android Usb port worked with: mouse, pendrive, connection to pc.

I have got a Serial to USB cable with PL2303 chip. I have connected on a fresh intallation of windows 7 and it detected as prolific 2303 and installed everything fine and recognized as serial.

I have pluged this cabe to the main usb of the Android. Compiled the USB PL 2303 demo with B4A 2.02 and Android level 15 api. Installed it on the android thru B4a-Bridge succesfully.

When opening it, it says no PL2303 found. I have tried with the mini usb same problem.

I have installed USB Host Controller 0.44 and it give me:
S3C USB Host dirver NOT found!

Root hub present, USB Host mode is active!

on the tab USB I got 2 active usb devices connected:
3-1 2303:067b (the prolific chip)
4-1 8176:0bda Realtek 802.11n WLAN Adapter


I would like to get a direction or ideas on what can be done.

I have not updated the Android on this tablet yet. Not sure if it is possible to install any kernel update etc. or something that I can do on the basic program.

Many thanks,

Eduardo Elias
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Thanks for reply!

The devices.getdevices return 0

Sorry, mean manager.GetDevices on Sub Find: return 0 (devices.length)
 
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Googling around I have found this:

"Enable USB-Host Controller on any Android
This app only checks usb host on your device.
To enable USB host API support you should add a file named android.hardware.usb.host.xml
and containing the following lines:
<permissions>
<feature name="android.hardware.usb.host">
</feature></permissions>
into folder /system/etc/permissions
in that folder find file named handheld_core_hardware.xml or tablet_core_hardware.xml
and add <feature name="android.hardware.usb.host"> into <permissions> section
Reboot your device. Usb host api should work."

of course for an Android 3.1 and higher.

I found that in my Tablet, besides it is a 4.0.3 the following:

/system/etc/permissions

file android.hardware.usb.accessory.xml

<permissions>
<feature name="android.hardware.usb.accessory"/>
<library name="com.android.future.usb.accessory" file="/system/framwork/com.android.future.usb.accessory.jar"/>
</permissions>


according the comments i have found i should have on the tablet_core_hardware.xml an entry about USB and host mode.

Does it mean that my tablet is not configured for USB host mode? If I change this will enable this feature, if eventually supported by the hardware?

Sorry, everthing is new for me on this platform, and I got B4A because this unique library PL2303 I could not found in any other language to be used.

Any Idea?

Eduardo Elias
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Found the solution

Here is guys:

The mentioned solution of changing the /system/etc/permissions worked!

The PL2303 demo found the usb->serial cable, at first time asked for permission to take the usb, i have accepted. Next time it found the pl2303 and could comunicate with pc. I have put a cable linking tablet and pc with teraterm and could receive the data sent by PL2303 demo

So, in my case, I was lucky my hardware was prepared and my Android4.0.3 came already rooted from factory. I changed the files and worked.

Eduardo Elias
 
D

Deleted member 103

Guest
Hi eelias,

this is the content of my file, can you tell me what am I to change.

android.hardware.usb.accessory.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- This is the standard feature indicating that the device supports USB accessories. -->
<permissions>
<feature name="android.hardware.usb.accessory" />
<library name="com.android.future.usb.accessory"
file="/system/framework/com.android.future.usb.accessory.jar" />
</permissions>

Ciao,
Filippo
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Hi eelias,

this is the content of my file, can you tell me what am I to change.

android.hardware.usb.accessory.xml


Ciao,
Filippo

You do not change this file at all, follor these steps:

1 - add a file named android.hardware.usb.host.xml on /system/etc/permissions
2 - containing the following lines:
<permissions>
<feature name="android.hardware.usb.host">
</feature></permissions>
3 - open in /system/etc/permissions file named handheld_core_hardware.xml or tablet_core_hardware.xml and add <feature name="android.hardware.usb.host"/> into <permissions> section
4 - Reboot your device. Usb host api should work

You have to have root access to these files. After the changes and reboot it worked for me.
 
Last edited by a moderator:

realblue

Member
Licensed User
Longtime User
Great info, saved my life. Thanks.

You do not change this file at all, follor these steps:

1 - add a file named android.hardware.usb.host.xml on /system/etc/permissions
2 - containing the following lines:
<permissions>
<feature name="android.hardware.usb.host">
</feature></permissions>
3 - open in /system/etc/permissions file named handheld_core_hardware.xml or tablet_core_hardware.xml and add <feature name="android.hardware.usb.host"> into <permissions> section
4 - Reboot your device. Usb host api should work

You have to have root access to these files. After the changes and reboot it worked for me.
 

pin71

Member
Licensed User
Longtime User
Hi,

I'm new to B4A and I'm coming from VisualBasic. I downloaded your example for the USB2Serial connection and it works.
Now I would like to add some changes and I would need to send out strings for modem commands like "AT" and so on.

I'm trying using USBpl2303.writedata("AT",2 ) but it doesn't work.
Is it possible to have any kind of help?

Thanks
 

pin71

Member
Licensed User
Longtime User
I have a question for Andrew:

Looking into your example I saw that the maximun read packet length is 64.

Is it possible to make it bigger ?

Thanks
 

agraham

Expert
Licensed User
Longtime User
No,the actual packet size is determined by the USB device but 64 bytes is the maximum allowed
Maximum Data Payload Size

An endpoint used for a control transfer specifies the maximum data payload size that it can accept or transmit to the bus. The allowable maximum data payload sizes depend on the speed of the device:
High-speed device endpoints may only select a maximum data payload size of 64 bytes.
Full-speed device endpoints may select a maximum data payload size of 8, 16, 32, or 64 bytes.
Low-speed device endpoints may only select a maximum data payload size of 8 bytes
 

Vinay Chaddha

New Member
Licensed User
Longtime User
how to bypass requestPermission() for USB HOST

I have been able to use the PL2303 library and every thing is working (sending and receiving data). However my application needs standalone operation. Now every time I switch on the device and run the app, it asks for permission to allow USB Access, In my application this is not possible as application works in standalone mode.

Any way to bypass the permission request or get the permission to use USB HOST without users acceptance?
 

pin71

Member
Licensed User
Longtime User
Timer cause the app to crach

I'm developing my app using this USB driver and it seems to work fine. But now I need to use a timer in my app to hold few secs and everytime I set the time enable the app crash.

this is what i'm getting on the log tab

java.lang.NullPointerException
at android.hardware.usb.UsbRequest.dequeue(UsbRequest.java:155)
at android.hardware.usb.UsbDeviceConnection.requestWait(UsbDeviceConnection.java:156)
at anywheresoftware.b4a.objects.usb.UsbDeviceConnectionWrapper$Reader.run(UsbDeviceConnectionWrapper.java:146)
at java.lang.Thread.run(Thread.java:856)


Any idea?

Thanks
 
Top