Need to parse a long string

All-

I am trying to parse a long string that was created from hex binary input from the serial port. The string reads FEFE009800XXXXXX1400FD, where XX represents bytes that are variable. These strings will be coming in with other input that all begins with FEFE0098 but is then of variable length. Therefore, I have included an if statement to only work on strings that begin with FEFE009800 and ignore the others. I want to extract the information contained in the third last byte: 14 in this case. I would like to put that information into a two digit number or string. It will come in as either 01, 03, 07, 10, 14, 18, 21, 24 or 28, and I want it to drive a set of relays depending on which of those values comes in. My rule is as follows:

rule icomstring
when
 Item Icomcontrol received update
 then
 val char[] hexArray = "0123456789ABCDEF".toCharArray()
 val byte[] bytes = DatatypeConverter::parseBase64Binary(Icomcontrol.state.toString())
     val char[] hexChars = newCharArrayOfSize(bytes.length() * 2)
 for (var j = 0; j < bytes.length(); j++) {
 var int v = bytes.get(j).bitwiseAnd(0xFF)
 hexChars.set(j * 2, hexArray.get(v >>> 4))
 hexChars.set(j * 2 + 1, hexArray.get(v.bitwiseAnd(0x0F)))
 }
 val outString = new String(hexChars)
 logInfo("rule check values", "serial value: " + outString)
 if(outString.contains("FEFE009800")){
 for (String command : outString.split("FEFE009800")) {
 logInfo("rule check values", "command: " + command)

}
}Preformatted text

Do you have ideas on how I can parse the string after the if statement to extract the required information to a variable called freq? Icomcontrol is the string that is attached to the serial port and receives the long string of bytes.

Many thanks,

-Mark

Why don’t you use REGEX Transformation?

Udo-

That looks like a good solution. My issue is that the data I want to capture is the two characters before 00FD (which are the concluding bytes). The data preceding those two characters can be anything. REGEX looks like it works best when finding specific characters before the data you want to capture. I will study it a bit more (no time this morning), but if you have a quick idea of how I can do this working from the end of the string that would be helpful.

Many thanks,

-Mark

It should work both ways.

.*(..)00FD.*

That should capture the two characters before 00FD just fine. Though I’ve not actually tested it. When using a tester site make sure to remember that OH requires the expression match the entire string and the transformation returns the first group ( i.e. parens).

All-

Thank you for your help. This worked perfectly. The help on this forum is incredible, and I very much enjoy using openHAB!

Many thanks,

-Mark