Find a control character i a string

Philip Prins

Active Member
Licensed User
Longtime User
Hello

I use the Asyncstream to receive a serial string from a device.The device starts the string with a STX (Hex 02) and ends the string with ETX (Hex 03)
How can i build a string from the received characters starting from STX and ETX?

Thanks in advance
Philip
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code:
B4X:
'data is the bytes array
Dim start = -1, endPos = -1 As Int
For i = 0 To data.Length - 1
   If data(i) = 0x2 Then
      start = i
   Else If data(i) = 0x3 Then
      endPos = i
   End If
Next
If endPos > start  + 1 AND start > -1 Then
   Dim s As String = BytesToString(data, start + 1, endPos - start - 1, "ASCII")
End If
 
Upvote 0
Top