input hex or bin data

For the whr930 control plug in i am facing the following querrie;

the outgoing data is based on the following command;

hex 07 F0 ///////// 070F
data = string.char(7) … string.char(240) … //////// … string.char(7) … string.char(15)
sending it goes with sendCommand(command) and with a function using luup.io.write(command) …

and the outgoing signal is working and the instructions are carried out.

i am struggeling to get the incomming signal in to the program
the data returned is byte coded and is one line with end with (hex 07F3 or hex 07F0)
and the length is maximum 80 (hex) digits long

i have a part in the program in the section

if i use a program called RealTerm as terminal and if i sent the data pure as hex string it works
if i sent it as text it also works

my question how to collect the single group of data, put it in a string and convert it into hex data so it can be procesed

any idea? or help or examples of other plugs?

regards
huib

There are plenty of examples here - just pick one that looks like it may receive data and check its I_***.XML file:

The DSC Alarm Panel is an example:

http://code.mios.com/trac/mios_dscalarmpanel/browser/trunk/I_DSCAlarmPanel1.xml

Search for ‘incoming’ and be careful that you spell it correctly.

I use the following to encode/decode URL data.

----------------------------------------------------
local function to_hex(spec_char)
   return string.format("%%%2X", spec_char:byte(1))
end
----------------------------------------------------
local function escape(in_string)
   return string.gsub(in_string, "%W", to_hex)
end
----------------------------------------------------
function from_hex(chars)
   return(string.char(tonumber(string.sub(chars,2), 16)))
end
----------------------------------------------------
function unescape(in_string)
   return string.gsub(in_string, "%%%x%x", from_hex)
end

huib, the Caddx alarm plugin (also on code.mios.com) is an example of a plugin that builds incoming strings one byte at a time. The Caddx communication protocol sends a message length in the second byte, there is no “end of message” character, but you could adapt it to your needs.

Essentially you are coding a state machine, and one of the transitions is “message is complete”, at which point you process the string you’ve built so far, and then empty out the string for the next incoming byte (which will be part of the next message).

One concern about your original post is that you are conflating “hex” with raw bytes (octets). There is no need to convert your incoming bytes to hexadecimal. If you really want to stringify your incoming bytes into hexadecimal characters (say, for debugging), then you can use RichardTSchaefer’s sample functions, but you can and should operate directly on the bytes as the Caddx plugin does.

Please feel free to copy what you need from my plugins; they are all GPL (and I can relicense anything that is incompatible with your preferred licence).