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
:icon_clap::icon_clap::icon_clap::icon_clap:All Hail Andrew!!!! It Connects!!!:icon_clap::icon_clap::icon_clap:
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi All!

I'm lost again with converting bytes and chars.

With this on Arduino:
Serial.print((char)CurrentValue);

This B4A:
Log("NewData")
Dim received As String
Log(BytesToString(Buffer, 0, Buffer.Length, "UTF-8"))
received = BytesToString(Buffer, 0, Buffer.Length, "UTF-8")
Label2.text = received
shows one or two squares on the log and nothing on Label2.

When I change Arduino code to: Serial.print(CurrentValue);

I start seeing "0" or "1" on the log and on Label2 as I wanted.

My first question is:
How should the above B4A be that this on Arduino:
Serial.print((char)CurrentValue);
returns/displays 0 or 1 as int ?


Second question:
Does the AsyncStreams_NewData return the newest/latest line/message in buffer?

Third question expands the above to RoboClaw:

The RoboClaw C#.net example app has this:
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_SerialPort"))

And this:
serialPort.PortName = usbDevice.DeviceID;
serialPort.BaudRate = 2000000;
serialPort.Open();
serialPort.Write(new byte[] { 0x80, 21 },0,2);
String versionstr="";
try{
versionstr = serialPort.ReadLine();
}
catch (TimeoutException){
}

if(versionstr.Substring(0,13)=="USB Roboclaw "){
versionstr = versionstr.Substring(13);

connect.Text = "Disconnect";
connected = true;
And this:
serialPort.Write(new byte[] { 0x80, 55 }, 0, 2);
UInt32 p = (UInt32)serialPort.ReadByte() << 24;


As soon as I open the port to RoboClaw I start getting a very long line of squares.
In the RoboClaw C#.net app this datastream is used to update datafields indicating temperature and battery voltages.

I don't read C# well if at all but I think from the above that the RoboClaw uses 32-bit charset/encoding.


So the third question is: how do I get 32-bit charset in B4A?


Rgds
Tapio

- I'll attached the full C# code as rtf file
 
Upvote 0

koep

Member
Licensed User
Longtime User
And the RoboClaw code as zip since the .txt was too large

Rgds
Tapio
 

Attachments

  • robocs.zip
    3.6 KB · Views: 361
Upvote 0

agraham

Expert
Licensed User
Longtime User
From what I can find the Roboclaw has a command response protocol that is mainly transferring bin4ary data in a packet format protected by a checksum. You will have to implement this yourself. BytesToString will not work. To see the value of a Byte just assign it to a string and Basic4android will convert it. Remember bytes are treated as signed values in Java so are sign extended when converted to other types.
B4X:
Label2.text = Buffer[0] ' values are -128 to 127

Dim i as Int
i = Bit.AND(buffer[0], 255) label2.text = i ' values are 0 to 255


AsyncStreams delivers the data in the order of arrival.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Andrew!

I mailed with the guys that make roboclaw and they say the problem is most likely on the driver

I noticed that a new version of usb-serial-for-android was uploaded few days ago, saying the bug with stop bits was fixed.

I've been trying to wrap this new code to a library, but I end up with a .jar that looks ok but results in:

Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example1
main_process_globals (java line: 375)
java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.UsbSerial
at b4a.example1.main._process_globals(main.java:375)
at b4a.example1.main.initializeProcessGlobals(main.java:342)
at b4a.example1.main.afterFirstLayout(main.java:85)
at b4a.example1.main.access$100(main.java:16)
at b4a.example1.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

Can you guess what I'm doing wrong?

If not too much to ask could you try and wrap the latest source with roboclaw Pid?

Rgds,
tapio
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I've no idea what you are trying to do when you say "wrap this new code to a library" nor why you would want to. The library already has the Roboclaw VendorID and Product ID in it and it recognises and connects to it according to your post #24. You need to look at the data you are getting and try to work out what to do. First off check that the baud rate is correct.
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi Andrew!

No I was not using the UsbSerial2.2:sign0013:.
To be honest I was as unaware of this new library as any given football hooligan.

I'll have to keep poking with it. For some reason my B4A bridge started acting up. drops connection and gives timedout ping.

Now the messagebox return device info

for some reason both:
astreams.Write(Array As Byte(128,82)) and
astreams.Write2(Array As Byte(128,82),0,2)

give me io error. Any ideas why?

Rgds,
Tapio
 
Upvote 0

koep

Member
Licensed User
Longtime User
Hi!

I'm still stuck with this RoboClaw connection.

With:
Sub Astreams_NewData (Buffer() As Byte)
Log("NewData")
Log(Buffer(0))
End Sub

I get:
NewData
0

repeatedly until I stop the connection.
Since all write attempts result in io error and viewing the code doesn't seem to help, I figured I better start from very basics.

I replicated Erel's library tutorial and since even that does not work in my B4A I concluded that I have something fundamentally wrong.

Question is: Which Eclipse should be used for library development?

I'm trying with the one that comes in adt-bundle-windows-x86 and jre1.7

rgds,
tapio

added stacktrace.txt
 

Attachments

  • stacktrace.txt
    5.1 KB · Views: 424
Last edited:
Upvote 0

koep

Member
Licensed User
Longtime User
Hi!

I cleared the log and started new from the top:

-Installed the app
-click OK in the msgbox
-click start
-click send, which has

Sub btnSend_Click
astreams.Write(Array As Byte(0x80,0x52))
End Sub

-clicked stop

attached the log for all above
 

Attachments

  • stacktrace.txt
    5.1 KB · Views: 268
Upvote 0

agraham

Expert
Licensed User
Longtime User
Looks like the Endpoints on the Roboclaw are the opposite way round to what they are in a proper CDC ACM device so despite having an Atmel VendorID it behaves differently to the Atmel devices on Arduinos. Try the attached UsbSerial that checks the endpoint directions and swaps them. If this doesn't work then I don't think the library can support Roboclaw.

EDIT:- Removed bad attachment
 
Last edited:
Upvote 0

koep

Member
Licensed User
Longtime User
Hi!

Not to worry! I want to thank you for your input so far.

Rgds,
Tapio

as last thought for tonight I directed the device infor from msgbox to log.
Attached logs when connected first to roboclaw and then to arduino uno

T
 

Attachments

  • stacktrace2.txt
    2.8 KB · Views: 280
Last edited:
Upvote 0
Top