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