Rule to convert ASCII to HEX

I’m trying to figure out a way to take an ASCII string being output by a serial device and then convert it to HEX so I can try and extract bits of data…
Anyone done something like this before? Or can I make the Serial binding some some of this?

I saw some recommendations to make the Serial binding record the data as CHARSET(ISO-8859-1) so I have done that now…

So now looking for a way to convert this into something more usable :slight_smile:

UyJQAlQOAEhBVgNNAQ==

Assuming this follows on from your earlier thread

It looks like what you have coming in is a horrid mixed-mode stream - a set of ascii coded “key” characters, each followed by a binary byte representing a numeric value.
e.g.
S PTHIVM displayed as ascii would be bytes (with decimal values)
83 15 80 02 84 185 72 73 86 03 77 00

They suggest you get a temperature like 17.5 but there’s no evidence of that yet in your capture, maybe you just get 17. We’ll see.

So you just want an array of bytes. Because the format is fixed - you know the S P T H V M bytes - you can save processing and just take 2nd byte value as “speed”, etc.

So first step then, grab that serial packet as a stream of bytes.for a close inspection.

rule "display serial packet"
when
   Item Serial01 changed
then
   var bytesarray = Serial01.state.getBytes()
   val StringBuilder decdisplay = new StringBuilder
   bytesarray.forEach[b |
      var int x = b.intValue.bitwiseAnd(255) // this converts -128 to 127 to 0 to 255
      decdisplay.append(Integer::toString(x) + ", ")
      ]
   logInfo("test", "decimal bytes " + decdisplay)
end

Thanks mate, sorry I though a new topic was better for a slightly different question :slight_smile:

Anything special I need to do the make the “GetBytes” work?

Rule 'display serial packet': 'getBytes' is not a member of 'org.eclipse.smarthome.core.types.State';

Didn’t think so last time I played with it - but it is a string method. Maybe that was
OH1.8 and Item states are a little different now. Try

Serial01.state.toString.getBytes()

Bingo!

2019-06-30 14:31:26.833 [vent.ItemStateChangedEvent] - Serial01 changed from S	PTH<VM to SPTH=VM
2019-06-30 14:31:28.311 [INFO ] [smarthome.model.script.Serial01 test] - decimal bytes 83, 8, 80, 1, 84, 15, 0, 72, 61, 86, 1, 77, 1,

Will head to my original post and see how we can make this work further to now interpret this data :slight_smile:

Could you post your Serial binding settings, then this thread will make a handy little
“Capture a serial data packet”
how-to.