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

NewB4a

Member
Licensed User
Longtime User
Hello again,

with the given helps now the application runs!!

But when i connect an OTG-cable and a HAMA usb--serial-adapter ( with PL-2303 HXD) I get the error 'Oops! No pl2303 device attached.'
Does this happen because I haven't yet plugged the measurement device (unfortuanately not at home) to the PL2303- cable or have I forgotten anything else?

Tom
 

NewB4a

Member
Licensed User
Longtime User
It reports 'No device attached'.
It also does'nt recognize the USB-Stick, which was recognized some days ago.

And when I connect my Windows PC to the Tablet it now claims for a driver Software, although this wasn't necessary some days ago, where the tablet was recognized from windows.

Any ideas what's going wrong? :BangHead:
 

agraham

Expert
Licensed User
Longtime User
It reports 'No device attached'.
Sounds like the tablet doesn't support host mode for programs. I assume your device has Android 3.1 or later. Check that your /system/etc/permissions folder contains a file named android.hardware.usb.host.xml.
http://www.basic4ppc.com/forum/addi...03-usb-rs232-adaptor-driver-5.html#post121125


It also does'nt recognize the USB-Stick, which was recognized some days ago.
If you mean UsbDetails doesn't see it it won't. Android doesn't pass on to apps any USB device it can use itself.

And when I connect my Windows PC to the Tablet it now claims for a driver Software, although this wasn't necessary some days ago, where the tablet was recognized from windows
Have you turned on USB debugging on the device since then? If so then it may be asking for the ADB Interface driver otherwise I don't know. B4A-Bridge is an alternative to connect to the IDE instead of USB.
http://www.basic4ppc.com/forum/basi...8-b4a-bridge-new-way-connect-your-device.html
 

NewB4a

Member
Licensed User
Longtime User
Hello agraham,
Android 4.0, USB debugging now disabled.

I file android.hardware.usb.host.xml doesn't exist in /system/etc/permissions

Following your suggestions I have to do

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


But how to write, add and edit a file?
And how to reboot my device?

Promised: Last questions for today. After this it should run

Thanks
Tom
 

NewB4a

Member
Licensed User
Longtime User
Hello agraham,

because I do need a tablet, on which I can use your apllication on rs232 with my measurement device

Which facilities must a tablet have, in order to run your example?
Are root permissions enough? Or must I look for 'use of host mode by apps'?

Do you know one or better some (cheap) ones, on which the example runs?

Greetings Tom
 

agraham

Expert
Licensed User
Longtime User
You don't need a rooted device. Root is only necessary if the manufacturer omitted the USB host permission files and you need to add them.

The tablet needs to be running Android 3.1 or higher and the hardware and the software needs to support host mode. I don't know of a way to tell if a tablet does this without trying.

My Motorola Xoom running Android 4.1.2 works fine. I guess the other big name tablets like Nexus and Samsung would be OK. It's probably the cheap Chinese brands that give trouble.
 

Rigsby

Member
Licensed User
Longtime User
Hello agraham,
because I do need a tablet, on which I can use your apllication on rs232 with my measurement device
Greetings Tom

If it's any use to you I bought an Acer Iconia Tab A500 off ebay secondhand. I paid £150 here in the UK. It has a full sized USB port, and works straight away with agrahams library & FTDI & Prolific chips without any messing about with rooting etc.
 

NewB4a

Member
Licensed User
Longtime User
Hi agraham,
I now have tested it with a real serial connection.
Where in your example have I to specify the additional property of the serial connection called 'flow controll' or anything similiar. This always has to be specified with a standard hyperterminal connection togehther with the other parameters like stopbits, parity, which I've changed to the correct values in your example.

Tom
 

agraham

Expert
Licensed User
Longtime User
From the xml for the UsbSerial object
Note that access to the modem control signals is not implemented for any of the above devices
owing to the lack of technical data about the actual chips which the manufacturers deem proprietary
information made available only to their OEM customers.
So flow control is "None".
 

NewB4a

Member
Licensed User
Longtime User
Hello agraham,
I'm hopefull because there's no error-message when connecting. Its probably just a matter of the correct use of the writedata-function and the definition of the testdata variable.

From windows I tested the device by sending "goto 5" with the standard hyper terminal and it worked.
I defined the following:

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
'For i = 0 To testdata.Length - 1
'testdata(i) = i
'Next
testdata(0)=103
testdata(1)=111
testdata(2)=116
testdata(3)=111
testdata(4)=32
testdata(5)=53
'testdata(6)=13
'testdata(7)=10
End Sub

and later on:

Sub btnSend_Click
' toggle signals to force status change
If DTR Then
DTR = False
RTS = True
Else
DTR = True
RTS = False
End If
USBpl2303.SetDtrRts(DTR, RTS)
Dim mS As Long
mS = DateTime.Now
totalrxbytes = 0
Dim length(4) As Byte
length(1) = 0x10
USBpl2303.WriteData(length, 4)
USBpl2303.WriteData(testdata, testdata.length)
'USBpl2303.WriteData(testdata, testdata.length)
'USBpl2303.WriteData(testdata, testdata.length)
'USBpl2303.WriteData(testdata, testdata.length)
' falsch: USBpl2303.writedata("goto 5",6)
Log("Writing for " & (DateTime.Now - mS) & " mS")

Dim status As String
status = Bit.ToBinaryString(Bit.AND(USBpl2303.GetStatus, 0xff))
Do While status.length < 8
status = "0" & status
Loop
Log("Status " & status)

End Sub

Unfortunately it didn't work.

Thank you for your patience Tom
 

agraham

Expert
Licensed User
Longtime User
Did you read this comment in the demo
B4X:
' PUBLIC Get the present status of the UART. At the moment it will return 0 until a status change occurs
' this is because status is only sent on a change and I haven't found a way to prod it to get the initial value
It works in the demo because RTS is looped back to CTS and setting DTR earlier kicks off a status change.
B4X:
' we are assuming a PL2303 connected in loopback TX->RX, DTR->DSR, RTS->CTS
If you haven't had a status change since initialising the PL2303 then nothing will happen.

If you can manage without the modem control lines then UsbSerial might be easier to use.
 

NewB4a

Member
Licensed User
Longtime User
It runs !!

Hello agraham,
it runs! :icon_clap:

When sendig 'goto 5' the real device does what's expected.
a had made two simple errors:
1. To send "goto 5" (=7 Bytes+ 0xOD) to the device I had to set within the process_globals: Dim size As Int : size = 7
2. In Activity_Create the values for testdata must be hexadecimal. :BangHead:

Now for the simple rest I will change your example more common, so that I can enter for example 'goto 5' and the size and the testdata are computed automatically. That's indeed simple.

Again thank you very much for your library and your example!!
And: B4a was a wonderful purchase.
Tom
 
Last edited:

NewB4a

Member
Licensed User
Longtime User
Ft232rl

Hello,
because of my success (#134) with a PL2303 connector, I've to make the same with a measurement device with usb-Input followed by a FT232RL Chip. Unformtunately I don't have the device at home.
Does anyone know, if USBPL2303 Library will run with this FTDI-Chip? Has anyone made experiences with this?
Thanks
Tom
 

agraham

Expert
Licensed User
Longtime User
No, this driver will not work with FTDI chips. But the new UsbSerial 2 library will work with both Prolific and FTDI chips. It is simpler to use than this PL2303 driver and the same application would work with either without change. The only downside is that there is no support for the modem control signals.
 

NewB4a

Member
Licensed User
Longtime User
USB Sewrial and FTDI

Thanks agraham,
but there is a problem. When running the example for usbserial with libv2.3 then the activity starts. But if I press button 'start' there comes the following error:
Program paused at line 28
if usb.open(19200)

With PL2303 USB to RS232 I use the same device as before, where it was running. Whats going wrong?
 
Last edited:

NewB4a

Member
Licensed User
Longtime User
Without debugger Iget:
IF usb.open(19200) then
java.lang.RuntimeException: Cannot parse 1 as boolean

???
 

NewB4a

Member
Licensed User
Longtime User
usp.open() Problem

I try again:

HTML:
Sub btnOpen_Click
    Dim dev As Int
   dev=usb.Open(19200)
   If dev <> usb.USB_NONE Then
      Log("Connected successfully!")
      astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
      usb.SetParameters(19200, usb.DATABITS_8, usb.STOPBITS_1, usb.PARITY_NONE)
   Else
      Log("Error opening USB port")
   End If
End Sub

Why do I always get 'Error opening USB port' ?

I use the same device as before, where it was running With PL2303 USB to RS232.
 
Top