Four possibilities that jump out at me:
*** 1 ***
A complete message from the other phone might be split up into smaller chunks by the time it arrives at Astreams_NewData. It depends on the timing of the incoming bytes, versus the timing of Astreams checking for incoming bytes and delivering them to you via Astreams_NewData.
Worst case is you might receive each byte individually, one at a time, and the line "txtLog.Text = txtLog.text & msg & CRLF" will add a CRLF after each of those bytes.
Another scenario is that you might receive the word "id" split across two Astreams_NewData events, with the "i" at the end of one msg and the "d" at the start of the next msg, and your code will add a CRLF in-between the "i" and the "d", which means that the line "k2 = txtLog.Text.IndexOf("id")" will not find it.
*** 2 ***
I can see where you assemble the received chunks back into a single message with "txtLog.Text = txtLog.text & msg & CRLF"
but I cannot see where stuff is being removed from txtLog.Text after it has been processed,
thus it seems txtLog.Text would get longer and longer and longer.
*** 3 ***
This possibility is also a result of not receiving the entire packet all-at-once. What happens if you have received up to the word "id" but have not yet received
everything after "id"?
Example: the other phone sent "314159 id=hello, world" but so far we have only received "314158 id=hell", and the rest of it "o, world" is still in the pipeline somewhere.
The line "txtLog.Text = txtLog.text & msg & CRLF" will add a CRLF to the end, so now we have "314159 id=hell" & CRLF
The line "k2 = txtLog.Text.IndexOf("id")" will find "id" at offset 7
The line "txttavoloarrivi.Text = txtLog.Text.SubString2(k2 + 3, txtLog.Text.Length)" will set txttavoloarrivi.Text to "hell" & CRLF
And then your earlier line "creatavolo.Initialize(File.OpenOutput(File.DirRootExternal, txttavoloarrivi.Text.trim &".txt",True))" will use that string, which contains a CRLF, to construct the filename "hell" & CRLF & ".txt"
I am guessing this is not what you want
*** 4 ***
The "-2" in the line "txtchiocciola.Text = txtLog.Text.SubString2(o -2 , txtLog.Text.Length)" makes me nervous...
What does .SubString do if variable o is 0 or 1, and thus offset "o -2" is -2 or -1?