Android Question Wait For Astream.write?

techknight

Well-Known Member
Licensed User
Longtime User
I have a couple of Astream.Write commands in sequence, one of them sending something different than the other. But both have to be sent back to back.

So I am doing This:

B4X:
WriteCommand
Do some things
Astream.Write(XXXXXX)


Sub WriteCommand
Do some things
Astream.Write(Result of some things)
Sleep(100)
End Sub

But it doesnt work. Soon as it encounters the Sleep(100), it returns and starts completeing everything that in the routine that called WriteCommand.

As you can imagine, this clobbers the serial data going on since its not really "waiting"

any ideas?
 

techknight

Well-Known Member
Licensed User
Longtime User
Grasping at straws here.

Now I am trying this:

B4X:
        Dim WaitTimer As Timer
        WaitTimer.Initialize("WaitTimer", 5000)
        SendCommandCTNC(0,0,1) 'Boot the TNCs
        WaitTimer.Enabled = True
        Wait For WaitTimer_Tick
        WaitTimer.Enabled = False

This doesn't work either. execution resumes immediately after this. So the Wait For isnt doing anything. I need something that halts this particular subroutine and further execution of it until something completes or after a specific time period and then picks up execution.

Seems everything I do wants to continue execution in the same exact sub even after I try sleep or wait for.... Ugh

any ideas?
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Seems the only thing I got to work properly was this:

Its only an educated guess as I have no idea how long the transmission takes.

B4X:
        SendCommandCTNC(0,0,1) 'Boot the TNCs
        WaitTimedOut = False
        WaitTimer.Interval = 100
        WaitTimer.Enabled = True
        Do Until WaitTimedOut = True
            Sleep(1)
        Loop

This only works as long as I keep it like that

if I do a subroutine called Delay, and put that code in a Delay subroutine, it still has the same problem. it resumes code execution after the call to the subroutine BEFORE the subroutine is complete.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Watch the resumeable subs Video.
You need to understand how it works.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Is there any documentation physically on how it works that would point out my mistake?

I cant watch videos here at work.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Also, is it possible to add a SendComplete event or something to the Astreams.write handler?

I dont know how reading/writing is handled internally. Just curious.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
It took forever but I finally "got it".

Any resumable sub feature such as sleep keeps returning back to the parent callback, UNLESS.... its expected to return a value. ;-)

Wow I am slow.
 
Upvote 0

Similar Threads

Top