Android Question Wait for total receipt of data, NewData

Amateurtje

Member
Licensed User
Longtime User
I need to send a couple bytes of data, receive the answer, send again, receive again etc, etc.

What I do not seem to be able to do is wait for the reiceipt of the data. I tried (some variations of):
Wait For (Starter.Wifiastream) NewData(Buffer() As Byte)
But it keeps on hanging on this point.

Can anybody hlp me along? I have more experience with vb.net and proton picbasic, it is difficult to handle the fact that a lot of action do not always go sequentially... I also just tried to do some waiting loops for receiving the data but that also does not work..
 

OliverA

Expert
Licensed User
Longtime User
You're not giving us enough code. What is Starter.Wifiastream? Where is this code line?
B4X:
Wait For (Starter.Wifiastream) NewData(Buffer() As Byte)
Can you upload a small example project of what you are trying to do? What you are expecting it to do?
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
I want to send data and then wait on the reation before sending new data.. But it just does not send the second (and more) data. I have made the project small towards the problem. Sometimes that process also helps to see the problem (or think of a work around) but it did not. A solution for this, will probably also help me in other parts of teh program :).

Thanks for helping me!
 

Attachments

  • send_and_wait_for_Data.zip
    12.9 KB · Views: 210
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Not needed and is a mistake:
B4X:
CallSub2("","Connect", General.WifiIpAdress)
'Change to:
Connect(General.WifiIpAdress)

Not exactly sure what you are doing but it looks like you need to use B4XBytesBuilder from B4XCollections.

There are many mistakes in the code flow. Start with this tutorial: [B4X] Resumable subs that return values (ResumableSub)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Does the other device you are communicating with understand prefix mode? Prefix mode prepends the data with 4 bytes, indicating the amount of data that follows.
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Not needed and is a mistake:
B4X:
CallSub2("","Connect", General.WifiIpAdress)
'Change to:
Connect(General.WifiIpAdress)

Not exactly sure what you are doing but it looks like you need to use B4XBytesBuilder from B4XCollections.

There are many mistakes in the code flow. Start with this tutorial: [B4X] Resumable subs that return values (ResumableSub)

That is what I had before but it was one of the tests of I could let the code wait with the callsub or callsubdelayed command. This all did not work. I was confused with another part. Sorry. But this is only to connect and it worked. I thought there was no difference between
connect(general.wifiadress)
and
callsub2("","Connect",general.wifiipaddress)
Making the connection is not the problem. Sending up and down 1 block of information also works good.

I read the resumable susbs part but it did not help solving my problem of letting the code wait for a response.

PS, there is no serialiser or anythign needed to send thsi code. I just need to send osme bytes, and I will get an asnwer of several bytes back..

PPS, I did try also a wait for with a resumable sub (for the Wifiastream_NewData) and a return value but but this did not let the command wait either for a return anwer.
PPS. This is only for when I have this working, but of course there must be a way to timeout the wait for command while when there is no answer, the programm must not get stuck...
 
Last edited:
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Does the other device you are communicating with understand prefix mode? Prefix mode prepends the data with 4 bytes, indicating the amount of data that follows.
the other device is an esp8266 (with the example code of erel, to forward incoming data to the serial port). So it should understand the priefix. If I send 1 piece of data it works perfectly..

But if the B4R script (ESP8266) knows how much is going to arrive, it will not let the B4A code wait untill an answer is cming back to its question?
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
B4X:
Private Sub Wifiastream_NewData (Buffer() As Byte)
Sleep(10)
return 1

end sub

If I want to make the receival of data a resumable sub with a return value to wait for it like above, I get a red line under "Return 1" telling me that it can not return a value from this sub.

Can I make a general.xxx value indicating the the receival is correct and then just:


B4X:
  General.recword(ID(0))
  For i=0 To 10
        Wait For (Starter.Wifiastream) NewData complete
        if general. receivecomplete = true then break     '' Value is set in receival sub
  Next
 
Upvote 0
Top