Hi,
Has anyone implemented a UDP Listener that can consume Hex, or put in a layer to translate Hex to an ascii string that can then be processed in the rules ?
Thanks
Paul
Hi,
Has anyone implemented a UDP Listener that can consume Hex, or put in a layer to translate Hex to an ascii string that can then be processed in the rules ?
Thanks
Paul
What do you mean by “Hex”? Do you mean a representation of binary data using ASCII digits representing base 16 (0-F) or do you want to translate raw binary data?
Hi,
Sorry, its a 12 byte format that needs to be broken down and parsed.
9.1 General UDP Packet format
The UDP Packet Format is formed as follows:
Field
Hi Paul
While I don’t have a ready made solution for you, I’ve been working with UDP sockets/packets for a binding I’m working on.
If you read in the bytes from the socket into an array, then pass that byte array into the constructor for a new string then you should have the ascii representation of the data (not Hex characters).
If I haven’t got the full picture then let me know.
Hi Daniel,
Thanks for the reply. Are you using the TCP/UDP binding or are you writing a new binding from scratch ? How do you intercept the UDP packet into a new constructor to translate into the string?
Paul
No I didn’t use the TCP/UDP binding, I wrote the network code pretty much from scratch (+ some google searching) using DatagramSockets.
snip
byte[] buffer = new byte[256]; DatagramPacket p = new DatagramPacket(buffer, buffer.length); try { socket.receive(p); if (!p.getAddress().equals(InetAddress.getLocalHost())) { byte[] data = Arrays.copyOf(p.getData(), p.getLength()); String message = Utils.toHexString(data); // this gives you the Hex string such as 686400067161 String ascii = new String(data); // this gives you ascii such as hd\00\06qa
snip
Here’s the toHexString() method if you need it:
public static String toHexString(byte[] array) { return DatatypeConverter.printHexBinary(array).toUpperCase(); }
Now that you’ve mentioned the TCP/UDP binding, is there any reusable core functionality for this kind of stuff instead of having to roll your own code each for each binding?
I’ve not started yet, but had been reading this (am planning to use openhab2)
https://www.eclipse.org/smarthome/documentation/development/bindings/how-to.html
but…
could also adapt this, as it seems compatible with 2
Had a look but the network code seems pretty integrated into the binding itself, not sure how easy/difficult it would be to separate the two.
If you go down the route of trying to adapt I’d be interested to see how you get on.
thanks - will have to see if I can get some time to play around, may just clone and try to update around here, but won’t be for a while
Cheers
Paul
The line you highlighted is essentially the same as my String ascii = new String(data) line. You receive the byte buffer somehow and pass it to a String constructor.