USBSerial&Arduino

koep

Member
Licensed User
Longtime User
Hi All!

I'm trying to connect to Arduino Uno with B4A and my chinese tablet running Android4.0.3, but keep getting "Error opening USB port"

Arduino works with VB.net and PC so problem is not on arduino.

Tablet recognizes mouse when plugged in so USB host is ok, I think:sign0104:

I have this same problem with the example app.

Has anyone any ideas where to look for cure?

Rgds,
Tapio

Add/Edit:

I read that android should show dialog asking if app is permitted to access usb port but my app not rhe provided sample trigger this dialog
 
Last edited:

koep

Member
Licensed User
Longtime User
Hi Erel!

I'm using your UsbSerial example, which is as simple as possible, I think.

I suppose my tablet does not recognize arduino since I get no dialog about connecting to the device. The unfiltered logs report "error at opening USB"
I can try and post the error message when I get home.

I have so far checked multiple times that the set-up is as per the example.
Can this be hardware related i.e. just not possible with my €50,- tablet?

rgds,
Tapio
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Android is supposed to take control of USB devices it recognises, like keyboards and mice, so applications will never see these. It is supposed to make unrecognised devices available to applications but it seems some devices do not do this. If your device is not passing a known to be working device to a known working application then unfortunately I don't think there is anything you can do about it.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Erel!

Thank You for posting the thread and pointing the way. I rooted and added the missing xml's. Now my tablet recognizes arduino.
Next problem is that when I load this code to the arduino:
static int counter = 0;
void setup() {
Serial.begin(115200);
}

void loop() {
Serial.print("Tick #");
Serial.print(counter++, DEC);
Serial.print("\n");

if (Serial.peek() != -1) {
Serial.print("Read: ");
do {
Serial.print((char) Serial.read());
} while (Serial.peek() != -1);
Serial.print("\n");
}
delay(1000);
}
I get the communication ie B4A app reads the ticks from arduino and receives the returned 1 or 0 sent from app.

But when I load this to arduino:
int SerialValue = 0;

void setup(){

pinMode(7, OUTPUT);
Serial.begin(115200);

}

void loop(){
SerialValue = Serial.read();
if(SerialValue == 1){
digitalWrite(7, HIGH);
}
if(SerialValue == 0){
digitalWrite(7, LOW);
}
}
I don't get any change in pin state. Arduino receives a message since the rx light is blinking. the above code works ok with this VB.Net:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
s.Close()
s.PortName = "com12" 'will need to change to your port number
s.BaudRate = 115200
s.DataBits = 8
s.Parity = Parity.None
s.StopBits = StopBits.One
s.Handshake = Handshake.None
s.Encoding = System.Text.Encoding.UTF8 'very important!
s.Open()
s.RtsEnable = True
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "On" Then
s.Write(Chr(1))
Button1.Text = "Off"
Else
s.Write(Chr(0))
Button1.Text = "On"

End If
End Sub

but not with this B4A which is your example, apart of 0ne button and label:
Sub Astreams_NewData (Buffer() As Byte)
Log("NewData")
Log(BytesToString(Buffer, 0, Buffer.Length, "UTF-8"))
Label2.text =BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
End Sub

Sub btnClose_Click
astreams.Close
Label1.text = "port closed"
Label2.text = "Label2"

End Sub

Sub btnSend_Click
astreams.Write("1".GetBytes("UTF-8"))
End Sub

Sub AStreams_Error
Log("Error: " & LastException)
astreams.Close
End Sub
Sub Astreams_Terminated
Log("Terminated")
astreams.Close
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub Off_Click
astreams.Write("0".GetBytes("UTF-8"))

End Sub

I'm somewhat clueless, hope that you or anyone else can point out what I'm missing here

Rgds,
Tapio
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Erel and Agraham!

Thank you for your answers. the arrays as byte() fixed my problem so now I can blink the leds.
The part of vb.net code that receives changes from arduino is not there because listener hangs the vb code up. I used the vb just to verify that arduino code works.


Next questions are:

-Do I send this "P15, i19200, [128, 0, 127, (255 & 0x7F)]" the same way?

-How would I set mouse cursor position on screen?
I want to make sure that cursor is on correct position after program start.

- Even though not relevant at the moment: Is it possible to connect to bus for example to two arduinos at the same time?

Rgds,
Tapio
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Your original use of "GetBytes" was correct to send a string, you were just looking for the wrong thing in the Arduino.

I'm not sure what you mean by "set mouse cursor position on screen". I assume you mean the PC, If you are using Windows Forms then you could use Control.Focus() to set the focus to the required control or use the Cursor.Position property to directly set the co-ordinates of the cursor.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi!

No, I mean on my Android tablet. I'm builind a motor driven actuator and I'm using tablet as HMI. I'd like to use mouse button as
GO/Start but for this I'd need to be able to make sure that cursor is on correct view/button on screen.
It looks like the mouse cursor setting is just not possible in android so I have to figure something else out.


Meanwhile I'd like to setDTR(true) with usbserial. how is this done?
My code now works correctly with arduino but I can't get the port open with my roboclaw.( http://www.basicmicro.com/USB-RoboClaw-2x15A_p_281.html )
The supplied example app&driver work on pc and my tablet recognizes the roboclaw when connected but port open causes "not initialized"-error.

Rgds,
Tapio
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
The modem controls signals don't seem to be exposed by either UsbSerial or UsbSerial3. You can try using reflection which should work but I haven't tested it as I don't have an FTDI device.
B4X:
Dim UART AS USbSerial3

UART.Open(9600)

Dim R as Reflector
R.Target = UART
R.Target = R.getField("driver")

R.RunMethod2("setDTR", true, "java.lang.boolean")
If this works you may already know that the driver supports

getCD(), getCTS(), getDSR(), getDTR() and getRI() which all return a Boolean

as well as setRTS(Boolean).
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Agraham!

That did not help. I started thinking that maybe I'm barking up wrong tree, so I run your USB device details and got this:

Device 0details
Manufacturer: not available
Product: not available
Serial: not available

DeviceName: /dev/bus/usb/001/007
DeviceClass: USB_CLASS_COM(communication device)
DeviceSubClass:0
Device ID:3EFh
ProductId:2404h
VendorId:3EBh

B4AInterfaceNumber:0
InterfaceClass: USB_CLASS_COM(communication device)
InterfaceSubClass:2
InterfaceProtocol:1

EndpointNumber:2
EndPointDirection:In
EndPointType:USB_ENDPOINT_XFER_INT(interrupt)
EndPointAttribute:3
EndPointInterval:16
EndPointMaxPacketSize:64

B4AInterfaceNumber:1
InterfaceClass: USB_CLASS_CDC_DATA(CDC device)
InterfaceSubClass:0
InterfaceProtocol:0

EndpointNumber:1
EndPointDirection:In
EndPointType:USB_ENDPOINT_XFER_BULK(bulk)
EndPointAttribute:2
EndPointInterval:0
EndPointMaxPacketSize:64

EndpointNumber:2
EndPointDirection:Out
EndPointType:USB_ENDPOINT_XFER_BULK(bulk)
EndPointAttribute:2
EndPointInterval:0
EndPointMaxPacketSize:64

The roboClaw works with PC but not with the same virtual com port driver as arduino. I had to install the supplied driver which says it's Atmel CDC Virtual ComPort.

I'm starting to think my B4A should talk USB to the RoboClaw, just don't know yet where to start with tweaking your code to be actually able to send and receive with the RoboClaw.

Rgds,
Tapio
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I had to install the supplied driver which says it's Atmel CDC Virtual ComPort.
That looks like a similar driver to the one I installed on my PC to talk to the Mega Arduino ADK which uses an Atmel microcontroller as a virtual COM port. The Mega Arduinos (and some others) use an Atmel controller while other Arduinos seem to use an FTDI FT232.I don't think that either the UsbSerial library or my PL2303 service will be able to talk to it but you could try changing the Device and Vendor IDs in my PL2303 driver on the off chance that something might happen, but I don't hold out much hope. Data transfers should work but first you have to get the chip set up properly and that will most likely be different to the Prolific and FTDI chips.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hello All!

I spent Easter weekend poking around with JavaDecompiler and found out that the reason I can't connect to my RoboClaw board is simple:
The usb-serial-for-android driver has a hardcoded list of supported devices (VID/PID) and if the device is not on the list the driver returns null ie does not connect.

I tried adding my board to this list(total of three entries in two classes) but did not fully succeed.

After lot of trial and error I managed to get Eclipse to make .JAR files of both driver and B4A library and the xml but now I get "java.lang.noclassdeffounderror" in log and "USBSerial example has stopped" messagebox on android tab

I have been testing with arduino so the app works when I use the original library but crashes with my packages so fault must be in my packaging.

Does someone know what I am doing wrong (ie has someone been in the same situation)?:sign0104:

I followed this to make the library: http://www.b4x.com/forum/libraries-developers-questions/8665-create-basic4android-libraries.html
with the exception that I have JavaSE1.7.

the driver source I used as it comes (android project) when I point Eclipse import to here: https://code.google.com/p/usb-serial-for-android/

If anyone skilled is willing to make the packages I'll zip and upload the source with the Vid/Pid added

Rgds,
Tapio
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
I'm working on a new UsbSerial library but here's the existing one with the Roboclaw ProductID added. The vendorID is already present as it is Atmels'. No gurantees it will work but it's worth a try. If you look with jd-gui you will see that I added ATMEL_ROBOCLAW to UsbId and to getSupportedDevices in CdcAcmSerialDriver.
 

Attachments

  • UsbSerialTemp.zip
    29 KB · Views: 422
Last edited:
Upvote 0

AndyDroid2012

Member
Licensed User
Longtime User
Koep,
Good detective work ! However I think this is a variant of the "cant write to extsd media" problem. The Java/Unix code by default has been written that way. Replacing the kernel with something like the Cyanogen version seems to allow some phones to see the USB is another example.

What is also apparent is that some apps like Deskpets and also USB Host Controller are able to communicate with their target device regardless of the resident code in the android - so that means its possible to write an app which does not rely on the android layer for USB and Wifi comms.

USB Host Controller on my chinese Allwinner A13 tablet with kernel 3.0.8 and 4.0.4 android OS can see and report details of almost any device I stick in the USB OTG but none of the b4a apps here can see anything, presumably because they rely on the Java libraries.

Please post your code. I would like to see what you did.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Andrew!

- Tried the library out- did not work:
when I load UsbSerialTemp and
change "Dim usb As UsbSerial" to "Dim usb As UsbSerialTemp"

It stays red ie Dim usb As UsbSerialTemp

The usb-serial-for-android .JAR was not in your zip Should it be there?

To my eyes the code looks just like what I managed to put out except for the .classpath and .project which I didn't include to export since the USBSerial3 library that I viewed as an example didn't have those.

@AndyDroid2012

By 2am last night I had my code so badly messed up that I deleted everything and figured I'll start fresh over if I get any hints on what I did wrong:sign0089:

Rgds,
Tapio
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The object is still called UsbSerial, only the library name changed. Just untick UsbSerial and tick UsbSerialTemp in the libraries pane, the code stays the same.

It doesn't need usb-serial-for-android .jar, the code is included in the library, look with jd-gui.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…