Trouble in parsing text

microbox

Active Member
Licensed User
Longtime User
Hi everyone,
I'm using Erel's excellent SerialExample code. I'm trying read incoming bluetooth data which has a format of "Data receive:125" where the numeric value after "Data receive:" increments and start from 0-255. I'm trying to separate the numeric part and display on EditText1 using the following code.:sign0104:
B4X:
Sub Button1_Click
    Indx1 =txtLog.Text.IndexOf ("Data receive:") ' Indx1 as global variable
    EditText1.Text =txtLog.Text.SubString2(Indx1,5)  
End Sub
But I'm getting error, please see attached file.

Thanks in advance,
microbox
 

Attachments

  • ErrorParsing.png
    ErrorParsing.png
    21.1 KB · Views: 161

microbox

Active Member
Licensed User
Longtime User
Hi,
Can you do a:
B4X:
Log(txtLog.txt)
Log(Indx1)
between those two code lines.
It looks like Indx1 comes up empty because it doesnt find the text.

I got the following working...
B4X:
Sub Button1_Click
    Indx1  =txtLog.Text.IndexOf (":") ' I change this
    txtSend.Text = Indx1
    EditText1.Text =txtLog.Text.SubString2(Indx1+1,Indx1+4)  
End Sub

But I have another question, is there a simple way to get the numeric values from this example string "Device receive:123Data2:10Data3:6Data4:255" where numeric values range from 0-255?

regards,
microbox
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi this works,
B4X:
Dim BluetoothData As String
   Dim DataStart, DataEnd, EndIndex As Int
   BluetoothData = "Device receive:123Data2:10Data3:6Data4:255"
   EndIndex = BluetoothData.LastIndexOf(":")      'end marker
   Dim List1 As List
   List1.Initialize
   
   DataStart = BluetoothData.IndexOf(":")
   DataEnd = BluetoothData.IndexOf2("Data",DataStart)
   Do While DataStart <> -1
      List1.Add(BluetoothData.SubString2(DataStart + 1,DataEnd))
      DataStart = BluetoothData.IndexOf2(":",DataEnd)
      If DataStart <> EndIndex Then
         DataEnd = BluetoothData.IndexOf2("Data",DataStart)
      Else
         DataEnd = BluetoothData.Length
      End If
   Loop
The List1 will contain all of the data values
 
Upvote 0

microbox

Active Member
Licensed User
Longtime User
Hi lagore,
Thanks for the code, works fine here...just need time to study it more..:)

regards,
microbox
 
Upvote 0
Top