Android Question AsyncStreamsText parse string

Declan

Well-Known Member
Licensed User
Longtime User
I have a string that is received via BT that is of variable length.
Each string can be between 18 and 24 characters in length and has a Prefix character ($) and a suffix character (#).
The strings do not arrive individually, but can often be appended to each other.
I would like to be able to capture the each string between the Prefix character ($) and the suffix character (#), something like: "$..................#"

I have edited the astreams_NewData so that I can see the end of the string (#).
I have also tried:
B4X:
Dim pos1, pos2 as Int
Dim newString as String
pos1 = strRX.IndexOf("$")
pos2 = strRX.IndexOf("#")
newString =  (strRX.SubString2(pos1, pos2))
But no success

ast_NewText:
B4X:
Sub ast_NewText(strRX As String)
    lblMonitor.Text = strRX

      Dim sep1, sep2, sep3, sep4, sep5, sep6, sep7 As Int ' index of separators
    Dim rackNo, zoneNo, alarmStatus, alarmSet, tempNow, setPoint As String
  
      If strRX.StartsWith("$") Then
            sep1 = strRX.IndexOf(",")
            sep2 = strRX.IndexOf2(",", sep1 + 1)
            sep3 = strRX.IndexOf2(",", sep2 + 1)
            sep4 = strRX.IndexOf2(",", sep3 + 1)
            sep5 = strRX.IndexOf2(",", sep4 + 1)
            sep6 = strRX.IndexOf2(",", sep5 + 1)
            sep7 = strRX.LastIndexOf(",")
      
            rackNo = (strRX.SubString2(sep1 + 1, sep2))
            zoneNo = (strRX.SubString2(sep2 + 1,sep3))
            alarmStatus = (strRX.SubString2(sep3 + 1, sep4))
            alarmSet = (strRX.SubString2(sep4 + 1,sep5))
            tempNow = (strRX.SubString2(sep5 + 1,sep6))
            setPoint = (strRX.SubString2(sep6 + 1, sep7))
       End If

astreams_NewData:
B4X:
Private Sub astreams_NewData (Buffer() As Byte)
    Dim newDataStart As Int = sb.Length
    'Log ("newdatastart = " & newDataStart)
    sb.Append(BytesToString(Buffer, 0, Buffer.Length, charset))
    Dim s As String = sb.ToString
    'Log("length of s =  " & s.Length)
    Dim start As Int = 0
    For i = newDataStart To s.Length - 1
        Dim c As Char = s.CharAt(i)
            If i = 0 And c = Chr(35) Then '#
            start = 1 'might be a broken end of line character
            Continue
        End If
        If c = Chr(35) Then '#
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            start = i + 1
        Else If c = Chr(13) Then '\r
            CallSubDelayed2(mTarget, mEventName & "_NewText", s.SubString2(start, i))
            If i < s.Length - 1 And s.CharAt(i + 1) = Chr(10) Then '\r\n
                i = i + 1
            End If
            start = i + 1
        End If
    Next
'    Main.LogMessage(s)
    'Log("scl = " & sb)
    If start > 0 Then sb.Remove(0, start)
End Sub
 

Declan

Well-Known Member
Licensed User
Longtime User
Is the NewText event raised?

What is the output of Log(strRX)?
Here is the log:
B4X:
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,025,202,
$,1,3,0,1,022,203,
$,255,4,0,1,022,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,024,202,
$,1,3,0,1,022,203,
$,255,4,0,1,022,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,025,202,
$,1,3,0,1,022,203,
$,255,4,0,1,022,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,025,202,
$,1,3,0,1,022,203,
$,255,4,0,1,021,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,024,202,
$,1,3,0,1,021,203,
$,255,4,0,1,021,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,
$,1,1,0,1,026,201,
$,255,2,0,1,024,202,
$,1,3,0,1,021,203,
$,255,4,0,1,021,204,
$,255,5,0,1,022,205,
$,255,6,0,1,021,206,
$,255,7,0,1,024,207,
$,255,8,0,1,024,208,
$,255,9,0,1,023,209,

The "255" just tells be a parameter has yet to be set
 
Upvote 0
Top