Inability to Send Raw Binary Bytes Over TCP with rawSocket.sendMessage(String)

We are trying to integrate Hubitat with a TCP Device.
We have the API for the Device, and it's pretty straight forward.

Expects Hexadecimal commands. Basic Example:
"23 31 11 A9" = Turn on
"23 39 55 E5" = Turn off

We are using the "interfaces.rawSocket.connect(ip, port)" method in HE.
The method only accepts strings, so I believe that the message is getting corrupted when trying to send it as an Hex.

If I want to send the following 4 bytes to a device over TCP:
0x23 0x31 0x11 0xA9
I construct a byte[] from the hex string:
byte[] bytes = hexStringToByteArray("233111A9")

Then I try to convert it into a String that preserves the binary values:

String raw = bytes.collect { (char)(it & 0xFF) }.join()
interfaces.rawSocket.sendMessage(raw)

However, this causes the byte 0xA9 to be UTF-8 encoded into two bytes:
0xC2 0xA9

So the device receives:

0x23 0x31 0x11 0xC2 0xA9 ← incorrect

We tried many different approaches to convert the string before sending, but nothing worked.
Looks like the best thing would add support for sending byte[] directly over rawSocket

interfaces.rawSocket.sendBytes(byte[] bytes)

Or have allow sendMessage(String message, boolean isBase64)

interfaces.rawSocket.sendMessage(base64EncodedString, true)

Where the true flag tells Hubitat to Base64-decode before transmission.

All approaches involving non-ASCII bytes are UTF-8 encoded and corrupted on transmission.

Any ideas? @bobbyD

Have you tried the ‘byteInterface: true’ option?

interfaces.rawSocket.connect(settings.ipAddress, API_PORT_NUMBER, byteInterface: true)

4 Likes

You are the best! It worked. I will post the driver later. Thanks my friend!

1 Like

Hi @hhorigian and/or @kkossev ,

I'm having a similar problem trying to connect to another device (JVC projector)
Can you state the steps you took to make it work?
starting from the hex commands you have ("23 31 11 A9")
did you first do a hexStringToByteArray and than send the that?
or first hexStringToByteArray, and then the convert to the string Raw as above and send that?

and i think the projector requires just ASCII for the handshake, but hex for the operational commands. can you easily switch between both (and how?)

see also my own topic Driver for JVC projector (NP5)

Thanks for any advice!

Highly appreciated!

kind regards, serge

Nevermind, fixed it! :smiley:

1 Like