B4J Question Regex.Split with control characters?

techknight

Well-Known Member
Licensed User
Longtime User
I have a string that has a few control characters like these in it:

<SYN> = 0x16
<SOH> = 0x01
<STX> = 0x02
<EOT> = 0x04
<ETB> = 0x17

The data in between is actually string/ASCII data.

I was wondering how I could use Regex.Split to separate everything in between those characters?

The data is coming into the program via a serial port. So it may even be possible to do it there.

any ideas?
 

sz4t4n

Member
Licensed User
Longtime User
Maybe something like this ( example for <EOT> = in ascii table this is dec 4)

B4X:
Dim data As String
data = 'string received from your serial
Dim dataList() As String
dataList= Regex.Split(chr(4), data)
Dim l As List
l.Initialize2(numbers)
Log(l)
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Thats what I was toying with. Seems that would work.

However I am running into a problem where not all of the packet has arrived at the same time, and sometimes it will have a full and a half packet on the next event raise.

So I need to figure out how to grab each packet from the NewData event.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I figured it out. Now I am getting complete packets.

But the problem is I need to be able to split the packet using all those characters at the same time, not just one. So I assume a Regex expression would be needed here, but I dunno what.
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Perfect. got it all working.

B4X:
Sub AStreams_NewData (Buffer() As Byte)
    Dim Data As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8") 'Grab the current chunk in buffer
    DataBlock = DataBlock & Data 'Append the data to the exising running buffer. 
    If DataBlock.Contains(Chr(22)) = True And DataBlock.Contains(Chr(23)) = True Then 'We at least have once full packet. 
        RxPacket = DataBlock.SubString2(DataBlock.IndexOf(Chr(22)), DataBlock.IndexOf(Chr(23))+1) 'Grab the detected packet.
        DataBlock = DataBlock.SubString(DataBlock.IndexOf(Chr(23))+1) 'Remove the first detected command from the running buffer.
        Dim Splits() As String = Regex.Split("[\x16\x01\x02\x04\x17]", RxPacket) 'Separate the packet into its individual components. 
        For Each Item In Splits
            Log(Item)
        Next
    End If   
End Sub
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
I dont think there is any "correct" way here. My way works fine I haven't run into a problem yet.

I think it all comes down to efficiency. Your method may be more efficient. Its like handwriting, every programmer is different.

In the meantime, I will have to look up bytesbuilder and figure out what it is. Its probably similar to StringBuilder?
 
Upvote 0
Top