B4R Question How start X subs, but all - with delay ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

If work with multichannel hardware with the variable channels qty = X - it needs to process each asynchronously.
Say, to call X subs with different parameter = channel number.
Each start - with delay.

How to do such variable qty of calls?

B4X:
CallSubPlus("PostCheckPort", WaitLimitSec * 1000, portnum) 'Does not work multiply and asynchronous, only the latest one is finished OK

Creating 16 or 32 subs - not smart way, i think, needs smarter.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
It is most stupid way... Impossible another ?!

B4X:
Sub TrackPort(portnum As Byte)
    Log("Tracking post started:", portnum)
    Switches(portnum) = 0    'start of counting
    Select portnum
        Case 0
            CallSubPlus("PostCheckPort0", WaitLimitSec * 1000, portnum)
        Case 1
            CallSubPlus("PostCheckPort1", WaitLimitSec * 1000, portnum)
        Case 2
            CallSubPlus("PostCheckPort2", WaitLimitSec * 1000, portnum)
        Case 3
            CallSubPlus("PostCheckPort3", WaitLimitSec * 1000, portnum)
        Case 4
            CallSubPlus("PostCheckPort4", WaitLimitSec * 1000, portnum)
        Case 5
            CallSubPlus("PostCheckPort5", WaitLimitSec * 1000, portnum)
    End Select
End Sub

Sub PostCheckPort0(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort0: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub

Sub PostCheckPort1(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort1: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub

Sub PostCheckPort2(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort2: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub

Sub PostCheckPort3(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort3: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub

Sub PostCheckPort4(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort4: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub

Sub PostCheckPort5(tag As Byte)
    'time is out, it needs to check the ventile state
    Log("PostCheckPort5: ", tag)
    If Switches(tag) < 2 Then    ' ventile trouble
        webapi.Send_Ventil_Fault(tag + 1)    'number of channel starting 1
    Else
        Log("Switched OK !: ", tag)
    End If
End Sub
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
How about an AddLooper with a sub and in the sub check if millis since last check are over the interval. Afterwards if it is true for each case, you can call the same sub with different Args since the code in PostCheckPortX is the same? It is pseudo-multitasking... Also instead of webapi, use MQTT (it is more lightweighted).
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Hmm, thanks, indeed checking the array of time intervals in the single sub is better.

But for MQTT it needs a separate host with the selected port number. But there is only one host and standard 80 port is needed, but it is already required for PHP web-interface. So, PHP-based API over HTTP 80 port (or 443 of HTTPS) is used (many mobile internet operators are blocking some of free ports, sure excepting 80\443).
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…