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

pixelpop

Active Member
Licensed User
Longtime User
I will +1 this one...right now it's just irritating from a development standpoint, but my project ideally will be able to use wifi connections concurrently with USB usage.

I think the problem is that those devices that assign the Realtek WLAN driver to the USB port allow wireless connections until you reassign that USB port to some other device. That's why Bridge works until an app that reassigns the USB port is run and then the wireless connection is lost. Some very smart people here know more about the technical details, but based on what I've seen, I'm not sure this issue can be resolved without possibly doing a custom ROM. And unfortunately, most (if not all) of these inexpensive tablet do not include Bluetooth. So the question then becomes: how do you ever develop an app that uses the USB interface for input? Bridge wireless disconnects, there's no Bluetooth and (of course) the USB port is in use by what you are developing for.
 

gtoner

Member
Licensed User
Longtime User
Its interesting that after the wifi is disabled I an go to the settings menu, disable the wifi which is indicated as being on and after the tablet responds by disabling the wifi I can turn it back on, it reconnects to my network and everything works correctly until I restart my program from the beginning where it asks for permission to use the usb.
 

agus mulyana

Member
Licensed User
Longtime User
Dear

Execuse me, i have been using PL23203 with OTG and connect it to LG Mobile Phone, i , but it still not working properly, please help me to solve this problem,, thank you
 

osasigbinedion

Member
Licensed User
Longtime User
I am having the same issue as pin71...
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.nio.ByteBuffer.isDirect()' on a null object reference
at android.hardware.usb.UsbRequest.dequeue(UsbRequest.java:156)
at android.hardware.usb.UsbDeviceConnection.requestWait(UsbDeviceConnection.java:230)
at anywheresoftware.b4a.objects.usb.UsbDeviceConnectionWrapper$Reader.run(UsbDeviceConnectionWrapper.java:146)
at java.lang.Thread.run(Thread.java:818)
My Timer code is
Sub PauseTimer_Tick
'Handle tick events
If Main.connected = True Then
If setup = False AND Main.usbActive = False Then
PauseTimer.Enabled = True
Else
PauseTimer.Enabled = False
End If
SendNextCommand
Else
Service_Destroy
End If
End Sub

Send Next Command sends the out request (queues the command)

In the new data sub I continue Listening...any clue on what could be happening.
 
Last edited:

kuosda

Active Member
Licensed User
Longtime User
Hello, everyone
My terminal equipment connected using RS485 ,interface chip is PL2303, use PL2303 USB to RS232 adaptor driver has found a problem that must be SetDtrRts (True, True) sends command and then SetDtrRts (True, False) to receive RS485 Data, equipment manufacturers tell me Not required control RTS, however, I found not so I do not know whether someone tell me why?
FIG. 123
 

Attachments

  • 1.jpg
    1.jpg
    27.2 KB · Views: 130
  • 2.jpg
    2.jpg
    143.8 KB · Views: 139
  • 3.jpg
    3.jpg
    144.9 KB · Views: 132
Top