Android Question StringBuilder "Append" not working in the correct order.

ThiagoRodrigues

Member
Licensed User
Longtime User
Hello,

I am using the GPS_NMEA event to collect raw NMEA strings ($GPRMC), and I have to append the string ",AUTO" at the end of it before sending it over a socket connection.

Code (network code no shown):

Sub GPS_NMEA (TimeStamp As Long, Sentence As String)
If Sentence.Contains("$GPRMC") Then
sb1.Initialize
sb1.Append(Sentence).Append(",").Append("AUTO")
tw.Write(sb1)
tw.Flush
End If
End Sub

When sending the sb1 string to a label, the order is correct, "AUTO" is at the end.
But when sending to a TCP socket, the "AUTO" comes first.

I´ve never seen this behaviour...
Any ideas?

Regards,
 

ThiagoRodrigues

Member
Licensed User
Longtime User
No good...

Log on PC side:

Received from 192.168.1.101: ',AUTO$GPRMC,,V,,,,,,,,,,N*53'
Received from 192.168.1.101: ',AUTO$GPRMC,,V,,,,,,,,,,N*53'
Received from 192.168.1.101: ',AUTO$GPRMC,,V,,,,,,,,,,N*53'
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
interesting...
try
B4X:
dim s as string = sb1.ToString
log(s)
tw.write(s)

that way you can tell if it is the string builder or the sending routine
 
Upvote 0

ThiagoRodrigues

Member
Licensed User
Longtime User
Yep, I did something similar to that previously...
No success...

I guess this issue is related to the GPS_NMEA event... I have used this same method of TCP sockets tranmissions several times and never seen that happening... even with GPS_LocationChanged it works...

Complete code:

Sub GPS_NMEA (TimeStamp As Long, Sentence As String)
If Sentence.Contains("$GPRMC") Then
If Socket1.Connected = False Then
Socket1.Connect("192.168.1.100", 1024, 2000)
Else
Dim s As String
tw.Initialize(Socket1.OutputStream)
sb1.Initialize
sb1.Append(Sentence).Append(",").Append("AUTO")
s = sb1.ToString

tw.WriteLine(s)
lblLon.Text = s
tw.Flush

End If

End If
End Sub

Sorry, it lost the indentation...
 
Last edited:
Upvote 0

ThiagoRodrigues

Member
Licensed User
Longtime User
Erel, thanks for the suggestion.

I tried using AsyncStreams, and no success...

Received from 192.168.1.102: '$GPRMC,004400.31,V,,,,,,,161013,,,N*7B'
Received from 192.168.1.102: ',AUTO'
Client disconnecting: 192.168.1.102 Duration: 00:00:07



Sub GPS_NMEA (TimeStamp As Long, Sentence As String)
If Sentence.Contains("$GPRMC") Then
Label1.Text = Sentence
s = Sentence & ",AUTO"

End If

End Sub

Sub Button1_Click

Socket1.Connect("192.168.1.100",1024,1000)

End Sub

Sub Socket1_Connected(Connected As Boolean)As Boolean
ToastMessageShow("Connected",True)
Dim buffer() As Byte
AStreams.Initialize(Socket1.InputStream,Socket1.OutputStream,"Astreams")
buffer = s.GetBytes("UTF8")
AStreams.Write(buffer)

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
AsyncStreams (not in prefix mode) by itself will not help.

The issue you are experiencing is explained in AsyncStreams tutorial. You cannot assume that messages sent over the network will arrive as a single message.

This is why you should use prefix mode if you can. In that mode every message is prefixed with its length and AsyncStreams takes care of "building" the message correctly.
 
Upvote 0
Top