Android Question Can felUsbSerial use ResumableSub?

kuosda

Active Member
Licensed User
Longtime User
Because _DataAvailable is an event postback, how do I collect data back to the upper layer

The ResumableSub is passed back to the parent layer instead of being passed back to his upper layer. What should I do?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You must start with watching the resumable subs video tutorial: https://www.b4x.com/etp.html

Example of waiting for more than 100 bytes to be available:
B4X:
Dim bb As BytesBuilder
bb.Initialize
Do Until bb.Length >= 100
   Wait For fel_DataAvailable (Buffer() As Byte)
   bb.Append(Buffer)
Loop
Log("there are 100 bytes or more")
 
Upvote 0

kuosda

Active Member
Licensed User
Longtime User
Erel
This method I tried but stopped at the end
→ Wait For fel_DataAvailable (Buffer() As Byte)
Is it okay?

And I still have a question
How to call the data after the main thread can't finish reading at one time Use callsub or calldellaysub

It is a bunch of characters, or sometimes there is no character return and the character that is returned is needed to retrieve. My app is used to collect equipment data. The customer will ask for "one touch" when using it. It's also my trouble. It's a thread that needs to send and read.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This method I tried but stopped at the end
→ Wait For fel_DataAvailable (Buffer() As Byte)
Is it okay?
It depends on the event name that you set and what you are trying to do.

The only way to use resumable subs is to understand how they work. As I already told you several times you should watch the video tutorial and learn about this concept.
 
Upvote 0

kuosda

Active Member
Licensed User
Longtime User
Suppose Buffer has no data and bb.Length < 100 will stop on this thread
How can I do it?
I have tried it ?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will not stop the thread. As I asked you 8 times now you should learn about resumable subs.

How can I do it?
The code I posted was just a small example. I really don't know what you are trying to do. It is up to you to decide what to do if data is not arriving.

However the main thread will not be stopped. All other code will continue to run. This is main concept behind resumable subs.
 
Upvote 0

kuosda

Active Member
Licensed User
Longtime User
I do the collection and display of hydrological equipment data. When writing vb6, mscomm32 has input and event methods that can be used. vb.net has SerialPort ReadByte() which can be used, so I am accustomed to using synchronous read data back to do judgment, synchronous reading Taking too deep roots makes me unable to escape this kind of thinking.
Wait until my project ends up using ResumableSub. I know this is what you've been reminding me.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is not possible to make synchronous readings in Android.
1. The USB API is asynchronous.
2. More importantly the OS will kill your app after 5 seconds if the main thread is not free to process the message queue (ANR).

While you made many posts about it, you still haven't clearly explained what you are trying to do.

Do you want to get all the available data every x milliseconds? This is very simple with a timer and BytesBuilder.
 
Upvote 0

kuosda

Active Member
Licensed User
Longtime User
In the era of USB2.0 OTG often causes poor contact, so there will be CheckSum mechanism. In the past felUsbSerial event was triggered after ... End Sub _DataAvailable (Buffer () As Byte) is responsible for collecting fragment data, that DataAvailable There will be a large section in the analysis of data and display, if the byte is missing, it needs to be re-executed.

You can collect members opinions that need to interact with the device like me.

I still use the usb component BulkTransfer in the loop and it can immediately give me feedback and only collect data without judgment until the byte is read.

My trouble is that PL2303 will not lose long collections of bytes, but FTDI will lose.

When this project is over, I can try out the ResumableSub that you have always emphasized.
 
Upvote 0

kuosda

Active Member
Licensed User
Longtime User
felUsbSerial code example reference
This is just a reference and not completely correct
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private usbserial As felUsbSerial
    Private manager As UsbManager
    Private r_id As Int = 0x6001
    Private r_vd As Int = 0x403
    Private Button1 As Button
    Private Timer1 As Timer
    Private Button2 As Button
    Private TimerOutSec As Int
    Private bbL As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Main")
    manager.Initialize
    Timer1.Initialize("Timer1",1)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
    U.Connect
End Sub

Sub Button1_Click
    Dim UsbAccessory() As UsbAccessory = manager.GetAccessories
    Dim UsbDevices() As UsbDevice = manager.GetDevices
    If UsbDevices.Length = 0 Then
        ToastMessageShow("No connected usb devices.", True)       
    Else
        For z = 0 To UsbDevices.Length - 1
            Dim UsbDvc As UsbDevice
           UsbDvc = UsbDevices(z)
            If (UsbDvc.ProductId = r_id) And (UsbDvc.VendorId = r_vd) Then           
                 If manager.HasPermission(UsbDvc) = False Then
                    manager.RequestPermission(UsbDvc)
                    Log(manager.USB_CLASS_HUB)
                 Else
                    usbserial.Initialize("serial", UsbDvc, -1)
                    usbserial.BaudRate = 57600
                    usbserial.Parity = usbserial.PARITY_NONE 
                    usbserial.DataBits = usbserial.DATA_BITS_8
                    usbserial.StopBits = usbserial.STOP_BITS_1
                    usbserial.StartReading                                               
                End If
            End If
        Next
    End If
End Sub

Sub Button2_Click
    TimerOutSec = 500
    bbL.Initialize
    Dim SendByte() as byte
    usbserial.Write(SendByte)
    SecondSub
    Wait For SecondSub_Complete    ' ←Start again here
    If bbL.Size > 0 Then
        Dim bc As BytesBuilder
        '↓Processing data
        For i = 0 To bbL.Size  - 1 
            Log(bc.HexFromBytes(bbL.Get(i)))
        Next
        '↑Processing data
    End If        
End Sub

Sub SecondSub
    Timer1.Enabled = True
    Do While True
        Wait For (usbserial) serial_DataAvailable (Buffer() As Byte) '←This code cannot leave this loop
        bbL.Add(Buffer)
        TimerOutSec = 500
    Loop
End Sub

Sub Timer1_Tick
    TimerOutSec = TimerOutSec - 1
    If TimerOutSec <= 0 Then
        Timer1.Enabled = False
        CallSubDelayed(Me, "SecondSub_Complete") '←Return to the main program "SecondSub_Complete"
    End If
End Sub
 
Upvote 0
Top