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

pin71

Member
Licensed User
Longtime User
It's quite hard to post the code since it quite long, anyway I think I have found the issue.
To make the connection to work better ( getting all the data correctly) I have changed these numbers:

TIMEOUT ( it was 1, I put 40)
TIMER_INTERVAL ( it was 100, I put 400)

With these numbers my app gets all data received correctly and since I need to get a checksum from the device I'm connected to, this turns out to be the only way to make my app read it.

But, by changing those numbers I saw that many time it crash. With the original numbers no, but I can't get the data that I want.

What do you think?

Thanks
 

tonga

Member
Licensed User
Longtime User
Hi Agraham,
I'm using USBpl2303_1.0.zip. I send a string to a device: all ok. The device replies with a string, but I'm not able to decode this string. How can I do?
Thanks
 

tonga

Member
Licensed User
Longtime User
yes, I used that library to send the string, all ok. To receive the string (20 bytes) I use the Sub RxDataEvent (is right?). Where I must look to find the bytes transmitted by the remote device?
 

agraham

Expert
Licensed User
Longtime User
Look at Sub RxDataEvent in the demo main module. The data is received in an RxDataBlock which is a Type declared at line 68 in the USBpl2303 module. The data is in a byte array in RxDataBlock.data and the number of valid data bytes in the array, starting from index 0, is in RxDataBlock.bytecount which is usually leass than the actual length of the array.
 

devragland

Member
Licensed User
Longtime User
PL2303 to RS232 driver implementation questions

Greetings,

First off, great work by agraham on the PL2303 driver. our company is trying to implement a solution wherein we can connect an android device to a medical instrument (KR8900) a topcon device which does have an RS232 port.

I was able to recieve the output in a pc running windows 7 (i wrote a small c# program to read serial output) .

In my windows test environment i used a serial to usb converter which had a prolific chip in it, i identified the chip version as PL-2303 XA/HXA.

Now i would want to have this done in android, but i have a few questions before i can proceed with agraham's solution.


1. What kind of usb to serial converters is recommended, what should the chip version be ?

2. Can anyone recommend a low cost tablet that has full usb host mode that i could use for this implementation (Does it need to be rooted?)


I would greatly appreciate any help from you guys.


notes:

Prolific has no further support for XA/HXA chips, meaning it wont be compatible with windows 8.

Check the link below, prolific claim that they would not be supporting XA/HXA chips for android implementations.

http://www.prolific.com.tw/UserFiles/files/PL2303%20Android%20brochure%2020121101.pdf

Thanking you,

Dev
 
Last edited:

agraham

Expert
Licensed User
Longtime User
1) I have a couple of cheapo unbranded PL2303 converters and they both work fine (as far as I can recall and at as least as fine as the broken Android USB support allows) on my Motorola Xoom with my driver. However only one of them works with the Prolific PL2303HSimpletest apk. I've no idea which PL2303 chips either converter uses.

2) No idea. A second hand original Xoom or Galaxy Tab?

Using the Prolific driver would probably be a better solution than mine. Unfortunately it looks like it is only available to Prolific customers and not to end users. This is the same policy as they have with the data about the PL2303 chip itself which is why my driver is incomplete in some ways.
 

devragland

Member
Licensed User
Longtime User
testing results

I tested with a micromax funbook tablet Micromax :: Funbook

it came rooted so i was able to do the steps mentioned to copy a file in /system/etc/permission etc.

i rebooted the device after said steps and connected it to my RS232 device, when i pressed on 'open' i got an alert message telling me 'requesting permission' after allowing it i was able to connect and a toast message came up 'UART 0 status 16' (iam not sure what this refers to)

When i tried the 'print option' in the RS232 device nothing happened though (its supposed to send RS232 data to the tablet) but when i pressed on the 'send' option in the demo application i got a confirmation 'RS232 successful' from the RS232 device (it has a lcd panel built in).

My motive is to read data.

I further went on and installed 'terminal' from googleplay and did an 'lsusb' to verify.

the product id and device id where 076b and 02303.

Any help is much appreciated.
 

devragland

Member
Licensed User
Longtime User
sorry my bad, i did mistype it i think i'l just display that in a toast message or a msgbox and try again later (the device is a few blocks off and i'd test again tomorrow) Thank you agraham for your assistance.

Edit:
i guess this is the sub that writes to a logging service i can ofcourse output in a toast or a msgbox and see if i get data. Correct me if am wrong, thanks once again.

Sub RxDataEvent
Dim rxdata As RxDataBlock
For i = 0 To USBpl2303.rxdata.size - 1
rxdata = USBpl2303.rxdata.Get(0)
USBpl2303.rxdata.RemoveAt(0)
totalrxbytes = totalrxbytes + rxdata.bytecount
Next
Log("RxDataEvent total bytes = " & totalrxbytes)

If totalrxbytes = 4100 Then
Msgbox(totalrxbytes, "Success")
End If
End Sub
 
Last edited:

devragland

Member
Licensed User
Longtime User
finally got everything working, my RS232 device returns a success but i have some issues with the recieving code am really confused here

:BangHead::BangHead:

B4X:
Sub RxDataEvent

Msgbox("procedure for recieving data called","sucess!")

   Dim rxdata As RxDataBlock
   For i = 0 To USBpl2303.rxdata.size - 1
      rxdata = USBpl2303.rxdata.Get(0)
      ToastMessageShow ("data block " & USBpl2303.RxData.Get(0),True)
      
      USBpl2303.rxdata.RemoveAt(0)
      totalrxbytes = totalrxbytes + rxdata.bytecount

      'Msgbox(totalrxbytes,"info")
   Next   


   'Log("RxDataEvent total bytes = " & totalrxbytes)
   
   If totalrxbytes = 4100 Then
      Msgbox(totalrxbytes, "Success")
   End If
End Sub

is the data output in the code (line 6) i do get some data but not wot is expected.

B4X:
ToastMessageShow ("data block " & USBpl2303.RxData.Get(0),True)

do i have to convert this to get my final output?

:sign0163:
 

agraham

Expert
Licensed User
Longtime User
It's all there in the code if you look
B4X:
Dim rxdata As RxDataBlock
rxdata = USBpl2303.rxdata.Get(0)
RxDataBlock is a Type defined in the USBpl2303 module.

B4X:
Type RxDataBlock (data() As Byte, bytecount As Int)
Do what you will with the data it contains.
 

devragland

Member
Licensed User
Longtime User
works like a charm

got the data converted with agraham's byteconverter and it works now, i tested this library with a couple of cheap USB to serial converters and tablets. my setup cost me less than 250 bucks, if anyone is experimenting on a cheap solution i'l be more than happy to share my test scenario with you guys

Regards,

Dev

and great work agraham :sign0098:
 

nprabakaran

New Member
How to avoid USB access user permission?

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?

Even I have the same problem, I don't want to app to ask USB access user permission, because no user will be present in our product. It is a shield board and NO screen present. How can I avoid USB access permission prompt?
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Greetings,

First off, great work by agraham on the PL2303 driver. our company is trying to implement a solution wherein we can connect an android device to a medical instrument (KR8900) a topcon device which does have an RS232 port.

I was able to recieve the output in a pc running windows 7 (i wrote a small c# program to read serial output) .

In my windows test environment i used a serial to usb converter which had a prolific chip in it, i identified the chip version as PL-2303 XA/HXA.

Now i would want to have this done in android, but i have a few questions before i can proceed with agraham's solution.


1. What kind of usb to serial converters is recommended, what should the chip version be ?

2. Can anyone recommend a low cost tablet that has full usb host mode that i could use for this implementation (Does it need to be rooted?)


I would greatly appreciate any help from you guys.


notes:

Prolific has no further support for XA/HXA chips, meaning it wont be compatible with windows 8.

Check the link below, prolific claim that they would not be supporting XA/HXA chips for android implementations.

http://www.prolific.com.tw/UserFiles/files/PL2303%20Android%20brochure%2020121101.pdf

Thanking you,

Dev

2. Can anyone recommend a low cost tablet that has full usb host mode that i could use for this implementation (Does it need to be rooted?)

You can try using TAblet GENESIS GT-7220S It is an android 4, already rooted, USB host mode supported, but need to be configured the XMLs, and I am using it with PL2303 cable and this library working very well. It is a very good tablet, fast, good touch panel good looking, and really good price.

Have fun

Eduardo
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
Question about Permissions on PL2303 library

Hi there,

Everytime a new version of the apk is uploaded to the tablet is asked for the USB permission

USBpl2303.RequestPermission

That works fine. For development is a little annoying, but my concern is when the user needs to upgrade version and it will ask again these permissions and eventually they will ask no, when should be asking yes. (I deal with stupid users sorry, no other way to say them to READ the dialog)

My question is: is there a way to get the USB or device permission request and hold that for the application even with upgrade?
 

EduardoElias

Well-Known Member
Licensed User
Longtime User
You can add an XML file as done in the UsbSerial library.
It will only ask for a permission when the USB is first attached.

THanks,

I have added:

B4X:
AddActivityText(main, <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />)

and compiling give me the following error:

AndroidManifest.xml:37: error: Error: No resource found that matches the given name (at 'resource' with value '@xml/device_filter').

I know nothing about this manifest file and have no idea from where to start......

Thanks

Eduardo
 
Top