Modbus: how to read strings?

I have a modbus device which stores a string in consecutive holding registers which i’m trying to read.

Right now, I would configure a modbus slave to read 8 unit16 values (for a 16 char string). How do I go about reading those registers and putting the result into a string?

Obviously

String mystringhere “[%s]” {modbus=“slave:0”}

will not work.

The best idea I would come up at the moment would be to read 16 individual int8 bytes and write a rule to put these into a string variable.
Can you imagine reading 10 of those strings, though? 160 items?

Yup. Modbus was never intended to handle strings.

You could log an enhancement request for the binding, I guess that would be for known fixed-length strings, packed into ASCII bytes? Any other sensible options? What should happen about writes?

Hmm, ideally, one would define a valuetype=string or char in the slave definitions and simply read the number of characters into a string item.

writes could work the same way, with padding ether 0x0 or 0x20 (space) if the string item holds fewer characters than the slave definition specifies.

Well, there are different character encodings, as well as the perennial big-endian/little-endian word split debate, if the characters are packed. And non-packed options, for a char per word.
And a padding choice for writes, as you pointed out.

Length would be covered by existing config parameter.

Someone would come up with a requirement for that be a maximum length, and terminate string building when encountering a control character, e.g. CR or null. Some might want to include control chars, others strip them - but I think the new transformation capability would take care of that?

Certainly worth making the enhancement request with a real use-case, that should go forward to OH2 binding even if it never happens for OH1.

@ssalonen are there plans to implement string types in the new binding?

Hi @mal

It’s completely doable and there’s actually even some core functionality there to support reading strings from registers. Wouldn’t be too big deal actually.

Currently, personally I have no plans to work on this but I welcome all contributions :slight_smile:

Best,
Sami