B4A Library UsbSerial library 2.0 - supports more devices

drponciano

Member
Licensed User
One more question. I made my program to work with USBSERIAL2.4 library changing usbtimeout to 1600 but, at 230400 bauds I get "newdata" from my connected device every 8000 bytes or so. Can I reduce the buffer size so I get less bytes every "newdata"? Thanks for your help.
 

rtek1000

Active Member
Licensed User
Well, that I know, the data is received automatically, asynchronously, and they are not collected every second,

have you ever tried to modify the way your application shows the data?
 

rtek1000

Active Member
Licensed User
Hi,

I changed the example, and now we can see how much data is received (bytes / second).

You will see that the data continues to be received, only one activity is slow to show all the data, so you need to find a way to do the data processing before show.

To test, I use an Arduino Nano V3 with this code:

B4X:
int count = 0;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  count++;
 
  Serial.println(count);
}
My test shows an average of 475 bytes / s
 

Attachments

rtek1000

Active Member
Licensed User

drponciano

Member
Licensed User
Ok. Even if my app does nothing but notify the amount of serial data received (no graphics, no display, no nothing), "newdata" responds every 8k bytes or so. I do the same thing as you suggested with Wi-Fi and the "chuncks" of data are 1K so the "refresh" of signal display is smooth. Both apps work fine, display data correctly, the problem is the 8k. Thanks for your answers.
 

drponciano

Member
Licensed User
This is the simple app. When connecting to USB1, 230,400 bauds en receiving aprox 5 Kbytes/seg, Astreams1_newdata return when it gets 8kbytes.

B4X:
Sub Process_Globals
    ' Demo has been changed to support Ver_2.4 JeanLC
    Dim usb1 As UsbSerial   
    Dim usb2 As UsbSerial   
    Dim astreams1 As AsyncStreams
    Dim astreams2 As AsyncStreams
End Sub

Sub Globals

    Dim btnSend, btnOpen, btnClose As Button
    Private btnOpen2 As Button
    Private btnClose2 As Button
    Private btnSend2 As Button
    Dim nrecibidos As Int
    Dim let_fc As Label
    Private btnExit As Button
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    btnClose.Enabled = False
    btnSend.Enabled = False
   
    btnClose2.Enabled = False
    btnSend2.Enabled = False
    nrecibidos=0
    let_fc.Initialize("")
   

End Sub

Sub btnOpen_Click
    If usb1.UsbPresent(1) = usb1.USB_NONE Then    ' Ver_2.4
        Log("Msgbox - no device")
        Msgbox("No USB device or accessory detected!", "Error")
        Log("Msgbox - returned")
        Return
    End If
    Log("Checking permission 1")
    If (usb1.HasPermission(1)) Then    ' Ver_2.4
        Msgbox(usb1.DeviceInfo(1), "Device Information 1")    ' Ver_2.4
        Dim dev As Int
        'dev = usb.Open(115200, 1)        ' Ver_2.4
        dev = usb1.Open(230400,1)  '9600, 1)        ' Ver_2.4
        If dev <> usb1.USB_NONE Then
            Log("Connected successfully! 1")
            btnOpen.Enabled = False
            btnClose.Enabled = True
            btnSend.Enabled = True           
            usb1.UsbTimeout=1600
            astreams1.Initialize(usb1.GetInputStream, usb1.GetOutputStream, "astreams1")
        Else
            Log("Error opening USB port 1")
        End If
    Else
        usb1.RequestPermission(1)  ' Ver_2.4
    End If
End Sub

Sub btnOpen2_Click

End Sub

Sub Astreams1_NewData (Buffer() As Byte)
    nrecibidos=nrecibidos+Buffer.Length
    Log(nrecibidos)
    Label1.Text=nrecibidos
    'Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub Astreams2_NewData (Buffer() As Byte)

End Sub

Sub btnClose_Click
    astreams1.Close
    usb1.Close   
    btnOpen.Enabled = True
    btnClose.Enabled = False
    btnSend.Enabled = False
End Sub

Sub btnClose2_Click

End Sub

Sub btnSend_Click
    astreams1.Write("TR".GetBytes("UTF8"))
End Sub

Sub btnSend2_Click
   
End Sub

Sub btnExit_Click
    ExitApplication
End Sub

Sub AStreams1_Error
    Log("Error: " & LastException)
    astreams1.Close
End Sub
Sub Astreams1_Terminated
    Log("Terminated")
    astreams1.Close
End Sub

Sub AStreams2_Error
    Log("Error: " & LastException)
    astreams2.Close
End Sub
Sub Astreams2_Terminated
    Log("Terminated")
    astreams2.Close
End Sub


Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 

rtek1000

Active Member
Licensed User
It's ok,

You can see the data frame with another application, so you can check if you have a pause between data transmissions,

Try running putty.exe for PC and get the data, if it works fine, you can make a movie and pass me the link, I will try to repeat the data transmission time to test the sample code better.

Maybe you will need to use an interface to process the data before sending it to Android.

I made a system in a church tower that works from one year ago, 24 hours, and never stops.

But I did a slow system with protocol that sends the data at 4800 bauds and has a range of 100ms between transmissions.
 
Last edited:

rtek1000

Active Member
Licensed User
This is a part of my code, you can see that the data frame has a terminator (CR LF) and I can discard data that does not fit the format.

The pause between data transmissions allows the activity to be updated in another step.

B4X:
Sub Astreams_Serial_NewData (buffer() As Byte)
    Dim str1 As String
  
    str1 = BytesToString(buffer, 0, buffer.Length, "UTF8")

    str_data1 = str_data1 & str1
  
    connect_status = True
  
    cnt_timeout_uc_not_respond = 0
  
    If(CountChar(str_data1, Chr(13)) > 0) Then  
        If log_update Then
            log_com("Rec: ", str_data1)
        End If

        If(PositionChar(str_data1, Chr(13)) == 53) Then
        
          ' 2016/09/19 1 19:33:32 23-06 0 00 0 0 0 30 21 2208
          '  
          ' year date           0-4
          ' year date           1
          ' year date           2
          ' year date              3
          ' "/"                    4
          ' month date          5-7
          ' month date          6
          ' "/"                    7
          ' day date            8-10
          ' day date            9
          ' " "                    10
          ' day week            11-12
          ' " "                    12
          ' hour time           13-15
          ' hour time           14
          ' ":"                    15
          ' minute time         16-17
          ' minute time         17
          ' ":"                    18
          ' second time         19-21
          ' second time         20
          ' " "                    21
          ' hour silence init     22-24
          ' hour silence init     23
          ' "-"                    24
          ' hour silence end      25-27
          ' hour silence end      26
          ' " "                    27
          ' bell status          28-29
          ' " "                    29
          ' bell manual play       30-32
          ' bell manual play       31
          ' " "                    32
          ' amplif status        33-34
          ' " "                    34
          ' rf433 status          35-36
          ' " "                    36
          ' rf433 btn             37-38
          ' " "                    38
          ' temperature            39-41
          ' temperature            40
          ' " "                    41
          ' index received        42-44
          ' index received        43
          ' " "                    44
          ' prog bell num start 45
          ' prog bell num start 46
          ' " "                    47
          ' checksum            48-52
          ' checksum            49
          ' checksum            50
          ' checksum            51
          ' lf                    52
          ' cr                    53
          '
        
            date_year_uc = get_substring(str_data1, 0, 4)

        End If
   
      str_data1 = ""
      
      serial_send(Array As Byte(13)) ' ack for uc non restart screen
    End If
End Sub
 
Last edited:

rtek1000

Active Member
Licensed User
Yes, I tested in release mode.

The library 2.0 did not work more than 38400 baud (And the library fel does not find my cable usb)

---

I found several terminals for usb serial cable in the playStore.

All freeze a screen, when many data is sent, some begin to freeze when still at 9600 bauds.

Even on the PC, too much data per serial port and a problem too, the terminal application for PC serial port also freezes.

---

I do not know why, but the FTDI application can receive data even at 921600 bauds, but also go freeze with lots of data.

---

I use a code that changes a baud rate of the microcontroller each time I press reset.

It is best for me to operate up to 115200 baud for my microcontroller, even though it was able to send data up to 921600.

I know a microcontroller can get a lot of data on 230400 bauds, no problem, but if a PC (Intel I5) can not, then an Android can not.

---

I know there is another way to operate with the FTDI chip, for direct access to the USB device, for PC there is a DLL, but for Android I do not know if it is possible.
http://www.ftdichip.com/Drivers/D2XX.htm
 

rtek1000

Active Member
Licensed User
Sorry, FTDI direct access to the USB device is available for Android, at same link above.
 

drponciano

Member
Licensed User
Perhaps I have not made myself clear enough. My uProcesor is transmitting about 5300 bytes/sec at 230400 bauds. The same rate is used when transmiitting over WiFi whitout problems receiving or processiong the info even in debug mode. My problem is: when using WiFi, Astreams1_newdata is triggered every 1kbytes or so, and makes my display routine refresh the graphs 5 times per second which, more or less, makes it look "real time". When I'm reading from the USB serial port, Astreams1_newdata is triggered every 8k bytes so my display routine has to wait almost 2 secons to refresh the graphs which makes it look "unreal". I could make my display routine to compensate this and make it look less "chopped" but it would have a 2 second delay anyway which is bad for heart monitoring. :)
 

Syd Wright

Well-Known Member
Licensed User
This USB library works very well. I am using it to connect a Contour ShuttleExpress rotary control (it does not work with the Fel library). However, I keep having to give permission after unplugging and re-plugging this USB device.
I have followed all the instructions in thread #1, but no success.

By the way, this problem also occurs when using the "OTG Disk Explorer Lite" app: permission is never remembered.
Is this a problem of my Nexus-7 (2012) device, or is there a way to resolve it?
 
Top