Hi all,
I have a device that implements a binary communications protocol over a serial interface. Due to the lack of serial interfaces (and usb-to-serial converters don’t work reliably for this device) on my host, I have used a serial-to-ethernet device such as the MOXA N5110 with puts the serial messages into udp packets and sends them to my host.
I have set up the TCP/UDP binding to receive these packets and would like to implement the binary communications protocol based on rules processing.
The communications protocol requires me to parse the binary incoming packets to extract some status information (which I will put into corresponding items) and respond with a binary packet that basically translates into a “Confirm_ACK” message to the device so that it continues to send its status messages periodically.
So far I have successfully set up the UDP binding to get an input and pass it as a string item to my rule. As a first step, I would like to convert this string into a set of bytes representing the binary input data in order to do bitwise extractions.
To verify the working of this, I would like to display the extracted bytestream.
To explain the setup:
The configuration of the tcp/udp binding in openhab.cfg:
udp:port=4115
udp:buffersize=110
udp:itemsharedconnections=true
udp:bindingsharedconnections=true
udp:directionssharedconnections=false
udp:addressmask=false
udp:preamble=
udp:postamble=/r/n
udp:blocking=false
udp:updatewithresponse=false
udp:refreshinterval=250
udp:charset=ISO-8859-1
Item definition:
String C400_InputString “C400 Input String [%s]” {udp=„<[device_ip:source_port:’REGEX((.*))']"}
String C400_decString „decoded Input String [%s]“
rule:
`import org.openhab.core.library.types.*
rule “parse_C400_InputString”
when
Item C400_InputString received update
then
if (C400_InputString.state instanceof StringType) {
var value = C400_InputString.state as StringType
var valueLength = value.toString.length() as Integer
val byte bytes = value.toString.getBytes(„ISO-8859-1“)
postUpdate(C400_decString, bytes.toString())
}
end
I was hoping that the bytes array contains the ASCII codes of the individual characters in the string, and wanted to output the ascii codes concatenated as a string for debugging purposes.
However:
I know that my device sends a binary stream of the following hex data:
„68 02 02 68 40 02 42 16“
What i get in the decString is:
„B@„ and then seemingly random data that varies from each input to the next, even tough the input string is always the same.
So clearly, the input parsing doesn’t work as planned. This could be due to the used charset, which I tried to put down as ISO-8859-1, so that I cover the full 8 bit per character. Or my rule ist simply wrong.
Any ideas? Or is the udp binding simply not capable of handling binary data?
Kind regards,
Thomas