Android Question USBSerial with Freetronics Leostick

bdunkleysmith

Active Member
Licensed User
Longtime User
I am trying to connect my Samsung tablet to a Leostick (Arduino) board from Freetronics via USB.

I am utilising the USBSerial library with the following code:

B4X:
Sub GetData_Click
    If usb.UsbPresent = usb.USB_NONE Then
        Log("Msgbox - no device")
        Msgbox("No USB device or accessory detected!", "Error")
        Return
    End If
    Log("Checking permission")
    If (usb.HasPermission) Then
        Msgbox(usb.DeviceInfo, "Device Information")
        Dim dev As Int
        dev = usb.Open(115200)       
        If dev <> usb.USB_NONE Then
            Msgbox("Connected successfully!", "Info")       
            astreams.Initialize(usb.GetInputStream, usb.GetOutputStream, "astreams")
        Else
            Msgbox("Error opening USB port", "Info")
        End If
    Else
        usb.RequestPermission
    End If
End Sub

However while the device information is displayed by the first msgbox command, there appears to be a compatibility issue because "Error opening USB port" is displayed by the third msgbox command.

So is the Leostick board from Freetronics compatible with the USBSerial libray?

Do I need to use the usb.SetCustomDevice command and if so, what parameters would I use given usb.DeviceInfo throws up:

Device ID: 0x3EB
ProductId: 0x8002
VendorId: 0x26BA
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Thanks Erel.

I used MSGBox rather than log as I simply don't know where the log is written and MSGBOX at least gave me an indication of the flow as the code was executed.

I actually tried the felUsbSerial library initially, achieving partial functionality and so thought I'd try USBSerial to see if I could overcome the shortcomings with that.
This is the code I used:

B4X:
Sub GetData_Click
     DataDisplay.Text = ""  
   If manager.GetDevices.Length = 0 Then
     Log("No connected usb devices.")
     DataDisplay.Text = "No connected usb devices"
   Else
     Dim device As UsbDevice = manager.GetDevices(0) 'assuming that there is exactly one device
     If manager.HasPermission(device) = False Then
       ToastMessageShow("Please allow connection and click again.", True)
       manager.RequestPermission(device)
     Else
       usbserial.Initialize("serial", device, -1)
       usbserial.BaudRate = 115200
       usbserial.DataBits = usbserial.DATA_BITS_8
       usbserial.StartReading
     End If
   End If
End Sub

Sub    serial_DataAvailable (Buffer() As Byte)
    Dim msg As String
    msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    DataDisplay.Text = msg
End Sub

The problem I had using felUsbSerial was that the received data was not blocked in the same form as the sent data. I am getting out of my depth, but presume I have a mismatch in data types: Byte, String, etc..

Four lines of data is sent from the Arduino USB port each second using this Arduino code:

B4X:
// Subroutine to send scoreboard data to serial port
void sendSerial(){
  // Send scoreboard data - if value is 15 (1111) then character is blank rather than show leading zero
  Serial.print("T");        // T prefix to identify (game) Time data
  if (minsMSD != 15){
    Serial.print(minsMSD);  // minsMSD becomes MSD seconds when game clock is <1m
  }
  else
  {
    Serial.print(" ");
  }
  if (minsLSD != 15){
    Serial.print(minsLSD);  // minsLSD becomes LSD seconds when game clock is <1m  
  }
  else
  {
    Serial.print(" ");
  }
  if (secsMSD != 15){       // secsMSD is blank (=15) when game clock is <1m
    Serial.print(":");      // Minutes & seconds separator when game clock is >1m
    Serial.print(secsMSD);
    Serial.println(secsLSD);        
  }
  else
  {
    Serial.print(".");      // Seconds & 10th seconds separator when game clock is <1m
    Serial.print(secsLSD);  // secsLSD is 10th second when game clock is <1m
    Serial.println(" ");
  }
  Serial.print("F");        // F prefix to identify team Foul data
  if (lFouls != 15){
    Serial.print(lFouls);  
  }
  else
  {
    Serial.print(" ");
  }
  Serial.print(" Fouls ");       // Team fouls separator
  if (dFouls != 15){
    Serial.println(dFouls);  
  }
  else
  {
    Serial.println(" ");
  }
  Serial.print("S");        // S prefix to identify team Score data
  if (lScore1 == 1){
    Serial.print(lScore1);
  }
  else
  {
    Serial.print(" ");
  }
  if (lScoreMSD != 15){
    Serial.print(lScoreMSD);  
  }
  else
  {
    Serial.print(" ");
  }
  if (lScoreLSD != 15){
    Serial.print(lScoreLSD);  
  }
  else
  {
    Serial.print(" ");
  }
  Serial.print(" Vs ");      // Score separator
  if (dScore1 == 1){
    Serial.print(dScore1);
  }
  else
  {
    Serial.print(" ");
  }
  if (dScoreMSD != 15){
    Serial.print(dScoreMSD);  
  }
  else
  {
    Serial.print(" ");
  }
  if (dScoreLSD != 15){
    Serial.println(dScoreLSD);  
  }
  else
  {
    Serial.println(" ");
  }
  Serial.print("H");              // H prefix to identify Shot Timer data
  if (shotTimerMSD != 15){
    Serial.print(shotTimerMSD);  
  }
  else
  {
    Serial.print(" ");
  }
  if (shotTimerLSD != 15){
    Serial.println(shotTimerLSD);  
  }
  else
  {
    Serial.println(" ");
  }
}

So a typical block of data would be:

T 9:35
F1fouls2
S 34Vs 21
H12

But DataDisplay.Text is displaying bits and pieces of the block of data, with it occasionally showing it formatted correctly in full.

I am already consuming this data via USB from the Arduino successfully with a PC based B4J app which uses asyncstreamstext:

Sub ast_NewText(Text As String)

and that's why I thought USBSerial may be better since it too uses asyncstreams.
 
Last edited:
Upvote 0

bdunkleysmith

Active Member
Licensed User
Longtime User
Well it's pleasing to know I was on the right track and so thanks once again Erel for pointing me to the appropriate reference.

Based on that post I've a functioning prototype which I now just need to clean up!
 
Upvote 0
Top