B4J Question Receiving Data in Background

MList

Active Member
Licensed User
Hi All,
I have an B4X Project, which is receiving Data via Serial Lib and AsyncStreams.
The data are measuring value which come continiusly (200kHz) approx. 15 values every second.
The incoming data is very difficult to handle and i have problems with handling the gui.
How can I manage to put this Sub AStream_NewData or the whole serial communitcation to Background.
Sorry never worked with background tasks ... just have no idea.

Should later on also work in B4i and B4a...
Thanks a lot for your help

Marion List
 
Solution
Thanks, i know that. But my problem ist when user presses a button and my program does things ( like reading a table or other things) it is always interrupted by the incoming data and goes to AStream_NewData. And doesnt return correctly to the things he was doing before.
There is no such thing. The problem is somewhere else.

Num3

Active Member
Licensed User
Longtime User
For the situation you reported, I would use threads (parallel running task). One thread to capture the data, another to process the data, all outside the main app scope.
I cannot advice on how to do it in B4J, it is something i have not needed to use yet, so i have not learned it.
Also, i think this option will not be easy to implement on mobile devices, due to the lifecycle of mobile apps.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi All,
I have an B4X Project, which is receiving Data via Serial Lib and AsyncStreams.
The data are measuring value which come continiusly (200kHz) approx. 15 values every second.
The incoming data is very difficult to handle and i have problems with handling the gui.
How can I manage to put this Sub AStream_NewData or the whole serial communitcation to Background.
Sorry never worked with background tasks ... just have no idea.

Should later on also work in B4i and B4a...
Thanks a lot for your help

Marion List
The AsyncStreams reading and writing are done with two separate threads. it works in the background.
I think the problem is how you handle the incoming data rather than manage it to background.
 
Upvote 0

MList

Active Member
Licensed User
I suppose it is consider as background. You can add a Log to see the data without doing anything with the GUI.

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim data As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
    Log(data)
End Sub
Thanks, i know that. But my problem ist when user presses a button and my program does things ( like reading a table or other things) it is always interrupted by the incoming data and goes to AStream_NewData. And doesnt return correctly to the things he was doing before.
It seems to me he starts the routine where he was interrupted at the beginning and not at the point he was interrupted...
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thanks, i know that. But my problem ist when user presses a button and my program does things ( like reading a table or other things) it is always interrupted by the incoming data and goes to AStream_NewData. And doesnt return correctly to the things he was doing before.
There is no such thing. The problem is somewhere else.
 
Upvote 0
Solution

MList

Active Member
Licensed User
What does the 200 kHz refer to? Is the serial link running at 230.4 kbps?


Why? What format is it in? Like, is it readable ASCII, perhaps separated into lines, or is it a fixed-length packet containing fixed-length binary data?


Maybe you don't have to update the gui for each incoming packet - eg, if your gui is a meter or numeric display of the incoming value, and you receive a burst of several values, then perhaps it is enough to just update the meter/display to the most-recent value, and ignore/discard the skipped values (unless you're also keeping track of peak value or average value etc)
I really dont know the exact speed, but it seems very fast. There is no problem with reading or displaying the incoming data. It is just that the incomimg data
ist interrupting my routines the user called (by pressing a button) and it seems to me after receiving data he doesnt return to the correct point where he was interruptet.
There is no such thing. The problem is somewhere else.
 
Upvote 0

MList

Active Member
Licensed User
I really dont know the exact speed, but it seems very fast. There is no problem with reading or displaying the incoming data. It is just that the incomimg data
ist interrupting my routines the user called (by pressing a button) and it seems to me after receiving data he doesnt return to the correct point where he was interruptet.
Ok thanks Erel, perhaps i am wrong. I will try to show it with a code sample. Can take some days because i just have other things to do. Thanks a lot
 
Upvote 0

MList

Active Member
Licensed User
The AsyncStreams reading and writing are done with two separate threads. it works in the background.
I think the problem is how you handle the incoming data rather than manage it to background.
Yes but when debugging. For example when my programm is in a loop doine something, and then data is coming then it goes to asyncstreamsnewdata and comes back and begins the routine where my loop was at the beginning... That was what i saw.. in my opinion he had to finish the loop where he left it. But i am trying to prove it with an exmple . thanks a lot
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Yes but when debugging. For example when my programm is in a loop doine something, and then data is coming then it goes to asyncstreamsnewdata and comes back and begins the routine where my loop was at the beginning... That was what i saw.. in my opinion he had to finish the loop where he left it. But i am trying to prove it with an exmple . thanks a lot
I guess the problem is caused by the loop in the sub-routine. I don't know what it's doing, I suggest trying to add Sleep(100) in the loop block of the sub-routine.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes but when debugging. For example when my programm is in a loop doine something, and then data is coming then it goes to asyncstreamsnewdata and comes back and begins the routine where my loop was at the beginning... That was what i saw.. in my opinion he had to finish the loop where he left it. But i am trying to prove it with an exmple . thanks a lot
If there is no call to Sleep / Wait For inside the loop then it will not leave the loop until it ends.
If there is such a call then the event can be raised, however it will later resume from the correct point.
 
Upvote 0

MList

Active Member
Licensed User
If there is no call to Sleep / Wait For inside the loop then it will not leave the loop until it ends.
If there is such a call then the event can be raised, however it will later resume from the correct point.
OK, thats the problem, there is a call to sleep inside the routine, because it also communicates with the device and is waiting for answer.
If he is waiting with sleep and in that moment there is incoming data(other incoming data, not that he is waiting for) ? Will he not be coming back to that sleep statement ?
 
Upvote 0

MList

Active Member
Licensed User
If there is no call to Sleep / Wait For inside the loop then it will not leave the loop until it ends.
If there is such a call then the event can be raised, however it will later resume from the correct point.
Thanks a lot Erel, you were absolutely right, it was a problem with my code.
 
Upvote 0
Top