Android Question AsyncStreams

Ale_resource

Member
Licensed User
Hi I have a connection to a device in tcp to which I send a command and then I get a response.
I send the command like this:
B4X:
AStreams1.Write2(b,0,b.Length)
and then I wait for the answer like this:
B4X:
Wait For AStreams1_NewData(BufferProg() As Byte)
Dim Text As String = BytesToString(BufferProg, 0, BufferProg.Length, "UTF8").Trim
the problem is that I never get the full answer and I can't understand why.
This is the answer I get from the code above :
V000000000000000000000000000000000000000000000
V001000000000000000000000000000000000000000000
V002040000000000000000000000000000000000000000
While in reality it should be this:
V000000000000000000000000000000000000000000000
V001000000000000000000000000000000000000000000
V002040000000000000000000000000000000000000000
V003000000000000000000000000000000000000000000
V004000000000000000000000000000000000000000000
V005000000000000000000000000000000000000000000
V006000000000000000000000000000000000000000000
V007000000000000000000000000000000000000000000
V008000110000000000000000000000000000000000000
V009000210000000000000000000000000000000000000
V010000310000000000000000000000000000000000000
V011000510000000000000000000000000000000000000
V012000610000000000000000000000000000000000000
V013000000000000000000000000000000000000000000
V014000000000000000000000000000000000000000000
V015000000000000000000000000000000000000000000
V016000000000000000000000000000000000000000000
V017000000000000000000000000000000000000000000
V018000000000000000000000000000000000000000000
V019000000000000000000000000000000000000000000
V020000000000000000000000000000000000000000000
V021000000000000000000000000000000000000000000
V022000000000000000000000000000000000000000000
V023000000000000000000000000000000000000000000
V024000000000000000000000000000000000000000000
V025000000000000000000000000000000000000000000
V026000000000000000000000000000000000000000000
V027000000000000000000000000000000000000000000
V028000000000000000000000000000000000000000000
V029000000000000000000000000000000000000000000
V030000000000000000000000000000000000000000000
V031000000000000000000000000000000000000000000
V032000000000000000000000000000000000000000000
V033000000000000000000000000000000000000000000
V034000000000000000000000000000000000000000000
V035000000000000000000000000000000000000000000
V036000000000000000000000000000000000000000000
V037000000000000000000000000000000000000000000
V038000000000000000000000000000000000000000000
V039000000000000000000000000000000000000000000
It seems that reception is interrupted or I don't know what else , thanks
 

DonManfred

Expert
Licensed User
Note that you should expect the packets to be splittet. Not all data can come in one event...
 
Upvote 0

Ale_resource

Member
Licensed User
Your code will only handle the NewData event once. Don't use Wait For here. Implement it in a different sub.
I tried to put the following code twice:
B4X:
Wait For AStreams1_NewData(BufferProg() As Byte)
Wait For AStreams1_NewData(BufferProg() As Byte)
but doing so does not execute subsequent lines of code. So I figured the package is sent all at once.
Isn't there some time or byte limitation?
 
Upvote 0

Ale_resource

Member
Licensed User
This code is plainly wrong. You cannot assume the number of times that it will be called in non-prefix mode.

First step is to implement the event in its own sub.
How should I do it then? Making a loop from 1 to 100 for example and putting inside the call to the new_data?
Can you give me an example? thank you
 
Upvote 0

Ale_resource

Member
Licensed User
B4X:
Sub AStreams1_NewData(BufferProg() As Byte)
Log(BufferProg.Length)
End Sub

Delete the Wait For AStream1...
I did so
B4X:
....
AStreams1.Write2(b,0,b.Length)
....

Sub AStreams1_NewData (Buffer() As Byte)
    Log(Buffer.Length)
End Sub
but on the log I don't see anything
1618211734986.png
 
Upvote 0

Ale_resource

Member
Licensed User
1. Switch to B4XPages. This is especially important when dealing with communication.
2. Remove HttpUtils2 and add OkHttpUtils2.
3. Please read my short answers more carefully:

View attachment 111510
Yes, the changes you indicated to me I made in the original project but not in the demo that I sent you sorry ... all done except the transition to B4XPages
but unfortunately the result does not change
 
Upvote 0

Chris2

Active Member
Licensed User
As well as @Erel's points, I think you may have missed @DonManfred 's...
Note that you should expect the packets to be splittet. Not all data can come in one event...
The reply you're expecting will come in bits, so you might need to let it build up...
B4X:
Private sb As StringBuilder        <--- in Globals

Sub AStreams1_NewData (Buffer() As Byte)
    sb.Append(BytesToString(BufferProg, 0, BufferProg.Length, "UTF8")
    Dim s as string = sb.ToString
    Log(s)
    ....
    ....
End Sub
You can then check if string s has the complete message using regex, or its length, or whatever.
You will need to know something about the reply you're expecting though in order to know when it is complete.

AsyncStreamsText might be useful also....
 
Upvote 0
Top