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,874

qsrtech

Active Member
Licensed User
Longtime User
Hmmm, my printer does seem to get some data but it seems to be garbage. I use getbytes("ascii") so i'm pretty sure that part is fine. Well I guess if I can't find a solution then I'll just have to spec a "pl2303" cable to be used with any "serial" printer. Unfortunately I don't have an actual USB based printer atm, one is on it's way. Hopefully the regular USB lib will work with these, w/o any additional headaches.
 

microbox

Active Member
Licensed User
Longtime User
Connecting android(3.2) to PL2303 adapter

Hello everyone, forgive me if this may sound simple...but I'm really having a hard time making my phone to communicate with Pl2303 module. I appreciate anyone who can give help me on this. My hardware setup is I have a microcontroller attached to the USB adapter(e-Gizmo USB-UART Serial TTL converter) pl2303 then to android tab. The application in the mcu is simply sending "Hello World" and have it display to android.
I installed the example program made by agraham, when pressing the button 'Open' gives me 'No PL2303 device found' every time. I tried also Slick USB to see if there are any incoming data but there are none...can any one point what I'm missing and kindly check my hardware connection...thanks in advance.


regards,
microbox
 

Attachments

  • Pl2303.png
    Pl2303.png
    7.4 KB · Views: 290

agraham

Expert
Licensed User
Longtime User
Your tablet needs to support USB host mode and if it does you will need an On The Go (OTG) cable, sometimes called a Camera cable, to switch the tablet USB port into host mode. This cable will have a mini-USB to plug into the tablet and a female USB socket so you can connect the tablet to the USB adaptor with a standard USB A-B cable.
 

microbox

Active Member
Licensed User
Longtime User
Thanks agraham for the response, I hope can get one today and try it again.

Regards,
microbox
 

microbox

Active Member
Licensed User
Longtime User
Hi..I finally got the mcu and android talking..OTG cable was the key. But I have another question:sign0104:...I insert an EditText to show the data("Hello World") in the following code..
B4X:
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
       EditText1.Text = rxdata
   Log("RxDataEvent total bytes = " & totalrxbytes)
   If totalrxbytes = 4100 Then
      Msgbox(totalrxbytes, "Success")
   End If
End Sub
this is the output I have [data=[B@4109e10,bytecount=20,IsInitialized=false
I read it needs the ByteConverter library but to be honest I'm not really sure how to use it...:(
 

microbox

Active Member
Licensed User
Longtime User
Hi, I'm having difficulty filtering/capturing my data sent via my MCU to android using USBPL2303, hope anyone here have time to point me what I'm doing wrong.
Hardware setup:
Microcontroller (with 1 button) connected to USB Pl2303 to Android(version 4). When Button is pressed it send message to port ":Hello World message 1,:Hello World message 2,:" + CHR(13).
I'm able to display the characters to editText1..but I need it to display them like in the form..
editText2.text = "Hello World message 1"
editText3.text = "Hello World message 2"
I tried to play with the following code but not working as I needed.
B4X:
Sub Button1_Click
Dim MyValue() As String
If EditText1.Text.Length > 20 Then 
  MyValue=Regex.Split(",",EditText1.Text)      
  MyValue(0)=MyValue(0).SubString(MyValue(0).IndexOf(":")+1)
  MyValue(1)=MyValue(1).SubString(MyValue(1).IndexOf(":")+1)
  editText1.text = MyValue(0) 
  editText2.text = MyValue(1)
End If 
End Sub

Thanks in advance and sorry if I'm not making clear :sign0104:
 

NewB4a

Member
Licensed User
Longtime User
Hello,

I've got the following Error:

Parsing code. Error
Error parsing program.
Error description: Unknown type: byteconverter
Are you missing a library reference?
Occurred on line: 132
Dim conv As ByteConverter

What to do?
 

NewB4a

Member
Licensed User
Longtime User
Problem with Byteconverter library

Sorry agraham,

first thanks for the hint to the library. As a beginner in b4a I have a very simple question:
I load byteconverter library and put the .Jar and.xml file in the libraries folder of b4a. But when compiling the example USBpl203 again I receive the error:

Error description: Unknown type: byteconverter
Are you missing a library reference?
Occurred on line: 132
Dim conv As ByteConverter

What changes are to do in the manifest?
 

rboeck

Well-Known Member
Licensed User
Longtime User
You have to make one click in the Libs tab at Byteconverter: you should see the version (1.10) behind the name. You dont need anything in the manifest.
Greetings
Reinhard
 

NewB4a

Member
Licensed User
Longtime User
One Click ?

Sorry, but I don't understand this.

What do you mean by 'Libs Tab' and what by 'one click'?

Thanks
Tom
 

agraham

Expert
Licensed User
Longtime User
Right hand pane of the IDE. Tab row at bottom. Select Libs. Check ByteConverter. You should keep the B4A Libraries folder for the official libraries that come with each release. Tools -> Configure Paths -> Additional libraries lets you define a folder for extra libraries. You should not put it in Ptrogram Files as that is a protected folder on Vista and later.
 

NewB4a

Member
Licensed User
Longtime User
Thanks,

indeed I've put it in the wrong path. My library has the Version 1.1.
Did i overwrite the original version? If so, how can i correct this?

And: What path is conveniant? Should I create a new folder parallel to the original libraries folder?

Besides: The Error now is:
Error description: Unknown type: reflector
Are you missing a library reference?
Occurred on line: 188
Dim Obj1 As Reflector

Thanks,Tom
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Version 1.1 is correct. There was no original version as this is an additional library not part of the official release. You can put the additional libraries folder anywhere you want but, as I already said, I would suggest not in Program Files. For example mine is in C:\Android\Basic4android\Basic4androidLibraries as I have a whole structure of Android SDK, Eclipse and Basic4android projects and library source code under C:\Android
 

NewB4a

Member
Licensed User
Longtime User
Sorry Agraham, when wasting your time.

I have extracted both files 'ByteConverter' to
C:\Android\Basic4Android\AdditionalLibraries

and I changed the correspondend path as neccessary.
But the error is still:
Error description: Unknown type: reflector
Are you missing a library reference?
Occurred on line: 188
Dim Obj1 As Reflector

Could you hel me again?

Thanks Tom
 
Top