Help with binary data - serial binding 3.1

I have a set of rules that worked fine in Openhab 2 running the Serial binding (Openhab V1 binding), but I am having trouble getting it to work with Openhab3 and the new serial binding.

The item Icomcontrol is a string that receives binary data. The old item description appears here:

String Icomcontrol "Icom Control" { serial="COM7,CHARSET(ISO-8859-1),BASE64" }

So, for the Serial binding in Openhab 3, I performed the configuration in the UI. I selected COM7 for the serial port, ensured the baud rate (9600) and parameters (8N1) were the same as for the prior version that was working. I selected ISO-8859-1 in the Charset option for the Thing, and for the Item, I linked Icomcontrol to the Binary Data channel with the default profile selected.

The data coming in have a lot of extra characters attached to them. Namely, my log file is showing:

2021-09-25 22:05:42.879 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:3120b580f4:data triggered PRESSED
2021-09-25 22:05:42.880 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Icomcontrol' changed from NULL to data:application/octet-stream;base64,/v4AmAAQABAYAP3+/gCYAGAAEBgA/f7+AJgAQAEQGAD9/v4AmAAgAxAYAP3+/gCYAFAEEBgA/f7+AJgAgAUQGAD9/v4AmABgBxAYAP3+/gCYAHAIEBgA/f7+AJgAcAkQGAD9/v4AmABgEBAYAP3+/gCYAGAREBgA/f7+AJgAkBIQGAD9/v4AmAAgFBAYAP3+/gCYAGAVEBgA/f7+AJgAIBcQGAD9/v4AmABQGBAYAP3+/gCYAIAZEBgA/f7+AJgAgBkQGAD9/v4AmABQGRAYAP3+/gCYAEAYEBgA/f7+AJgAcBYQGAD9/v4AmABQExAYAP3+/gCYAEAQEBgA/f7+AJgAEAcQGAD9/v4AmABgBBAYAP3+/gCYAJACEBgA/f7+AJgAYAAQGAD9/v4AmACQmAkYAP3+/gCYAGCXCRgA/f7+AJgAkJYJGAD9/v4AmAAQlgkYAP3+/gCYAICVCRgA/f7+AJgAAJYJGAD9/v4AmABQlgkYAP3+/gCYAGCXCRgA/f7+AJgAAJkJGAD9/v4AmABQABAYAP3+/gCYAJABEBgA/f7+AJgAEAQQGAD9/v4AmACABRAYAP3+/gCYAJAGEBgA/f7+AJgAYAcQGAD9/v4AmAAACBAYAP3+/gCYAIAIEBgA/f7+AJgAEAkQGAD9
2021-09-25 22:05:44.018 [INFO ] [openhab.event.ChannelTriggeredEvent ] - serial:serialBridge:3120b580f4:data triggered PRESSED

The extra information appears to be “data:application/octet-stream;base64”, and the characters do not make sense. They are not the hex-coded bytes I was expecting to receive. My rule does not process them correctly:

rule icomstring
	when
	 Item Icomcontrol received update
	 then
	 if (autoant.state == ON) {
	 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)
	 if(outString.contains("FEFE009800")){
	 val icomband = transform("REGEX", ".*(..)00FD.*", outString)
 	 logInfo("rule check values", "command: " + icomband)
	 if (icomband == "01") {
				sendCommand(Antcontrol, "2:2:160\n")
				postUpdate(antband,"160 Meters")
			}
			else if (icomband == "03") {
			sendCommand(Antcontrol, "2:2:80\n")
				postUpdate(antband,"80 Meters")
			}
			else if (icomband == "07") {
			sendCommand(Antcontrol, "2:2:40\n")
				postUpdate(antband,"40 Meters")
			}
			else if (icomband == "10") {
			sendCommand(Antcontrol, "2:2:30\n")
				postUpdate(antband,"30 Meters")
			}
			else if (icomband == "14") {
			sendCommand(Antcontrol, "2:2:20\n")
				postUpdate(antband,"20 Meters")
			}
			else if (icomband == "18") {
			sendCommand(Antcontrol, "2:2:17\n")
				postUpdate(antband,"17 Meters")
			}
			else if (icomband == "21") {
			sendCommand(Antcontrol, "2:2:15\n")
				postUpdate(antband,"15 Meters")
			}
			else if (icomband == "24") {
			sendCommand(Antcontrol, "2:2:12\n")
				postUpdate(antband,"12 Meters")
			}
			else if (icomband == "28" || icomband == "29") {
			sendCommand(Antcontrol, "2:2:10\n")
				postUpdate(antband,"10 Meters")
			}
}
}
end

The rule is looking for the bytes FEFE009800 to then identify BCD elements to extract and then act upon. I suspect I have a simple setting wrong. Can anyone help me with this please?

My platform: Openhab 3.1 on Windows 10.

Many thanks,

-Mark

Try replacing line:

val byte[] bytes = DatatypeConverter::parseBase64Binary(Icomcontrol.state.toString())

With:

val byte[] bytes = RawType.valueOf(Icomcontrol.state.toString()).getBytes()

Mike-

Thank you very much for your help. That correction fixed my problem right away!

Many thanks,

-Mark

1 Like

This suggestion was solve my issue thanks for sharing this is useful.