Auto Modbus Binding based on codesys xml file


From codesys i get the following modbus.xml file. Is there a way to force an automated binding with OpenHab2?

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

I’m not sure what you mean by “force an automated binding” but you can use the HTTP binding or sendHttpGetRequest Action from a Rule to retrieve the XML and then use the XPATH or XSLT transformations to extract data from the XML.

You could write some kind of script that analyzed your XML and generated a Things file or REST Thing creation code. Possibly you would want to also add Items creation and linking, via file or REST.

Your script would have to do some careful analysis to optimize the creation of efficient poller Things, depending on register address ranges and times.

It’s going to be a lot quicker to do it by hand.

Thanks, for this hint!
Until now Modbus, has zero self-identify capability, but the Codesys xml file is an industry standard to self-identify Things and i would like to develop a codesys2openhab import script to make life easier for other codesys developers.
Can you give me an example what kind of analysis is needed to auto bind for example that Thing (it’s a simple switch)

<generic_array_var data_type=“BOOL” num_elements=“4” default_value="" var_retain=“false”>
<generic_params name=“xNewVar1” comment="" expanded=“false” mb_iotype=“output” mb_access=“wo” var_byteorder=“B1B2B3B4”/>
<mb_address explicitAccess=“false” FCRead=“1” FCWrite=“15” ReadMBAddress=“514” WriteMBAddress=“514” ReadBitOffset=“0” WriteBitOffset=“0”/>

Via the register services the states of the complex and digital I/O modules can be determined or changed.
So my idea is to poll the modbus watchdog registers instead of the things to analyse the device state.

By the way, all codesys Things and Sensors have a channel or array mode. I would prefere the array mode if it is supported by openhab2. Example: to set all 4 switches to on i would send the value 15 instead sending the value 1 to all channels.

You could make a start by looking at the Modbus binding docs, to see what information it needs.

Presumably this “discovery” XML is obtained by interrogating some pre-defined address. But not Modbus, which does not support XML. How does it work with serial devices and gateways?

For a typical device, you configure a Bridge Thing, which specifies TCP or serial, ports, addresses, baud etc.
It also allows tuning of parameters like how long to keep TCP connections alive, these depend on devices and environment, so I don’t suppose you can select those based on this discovery. There are of course defaults that work, but may be sub-optimal.
That’s the easy one.

You’d have one or more Poller Things, these define a “block” of registers of one modbus type to read at intervals that you specify. Your script would need to aggregate individually discovered registers and work out the most effective layout here, this might be the hard part.

You’d normally have many Data Things, each describes a register, or two, or four, or part of a register, and how to interpret them - single bits, 16-bit or 64-bit numbers, etc. Each data Thing may have read and/or write defined, and exceptionally read and write definitions may be quite different and linked to different registers.
Transformations may be applied to read or write Data Things, allowing simple conversions like divide-by-ten an integer register to obtain a decimal number. I don’t know if your discovery could allows selection of that kind of detail. It would get more complicated by the need to install transformation services and scripts.

Each Data Thing has a range of channel types - Number, Switch, Contact, etc. that so that the user can link to an appropriate openHAB Item depending on usage. Presumably your discovery can determine and create suitable Item types.

All of these Things and Items need unique names, clearly it’s helpful if they are meaningful.

I’d suggest you work through your example XML by hand and draw up a Things and Items configuration, to get an idea what is involved.

Thanks again, that’s too complex for me to develop ;-( so i will do the modbus binding manually.