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:
It seems that reception is interrupted or I don't know what else , thanks
 

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
 
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…