Search in sources :

Example 1 with WriteableModbusRange

use of io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange in project openems by OpenEMS.

the class ModbusProtocol method addRange.

public void addRange(ModbusRange range) {
    // check each range for plausibility
    checkRange(range);
    // fill writableRanges
    if (range instanceof WriteableModbusRange) {
        WriteableModbusRange writableRange = (WriteableModbusRange) range;
        writableRanges.put(writableRange.getStartAddress(), writableRange);
    }
    // fill readRanges Map
    readRanges.put(range.getStartAddress(), range);
    // fill channelElementMap
    for (ModbusElement<?> element : range.getElements()) {
        if (element.getChannel() != null) {
            // ignore Elements without Channel (DummyChannels)
            channelElementMap.put(element.getChannel(), element);
        }
    }
}
Also used : WriteableModbusRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange)

Example 2 with WriteableModbusRange

use of io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange in project openems by OpenEMS.

the class ModbusDeviceNature method createModbusProtocol.

private void createModbusProtocol() {
    try {
        this.protocol = defineModbusProtocol();
        for (ThingChannelsUpdatedListener listener : this.listeners) {
            listener.thingChannelsUpdated(this);
        }
        if (this.parent instanceof ModbusDevice) {
            ModbusDevice parent = (ModbusDevice) this.parent;
            if (parent.getBridge() instanceof ModbusBridge) {
                ModbusBridge bridge = (ModbusBridge) parent.getBridge();
                // create WriteTasks
                writeTasks = Collections.synchronizedList(new ArrayList<>());
                for (WriteableModbusRange range : protocol.getWritableRanges()) {
                    writeTasks.add(new ModbusBridgeWriteTask(parent.getModbusUnitId(), bridge, range));
                }
                // create ReadTasks
                readTasks = Collections.synchronizedList(new ArrayList<>());
                otherReadTasks = Collections.synchronizedList(new ArrayList<>());
                for (ModbusRange range : protocol.getReadRanges()) {
                    otherReadTasks.add(new ModbusBridgeReadTask(parent.getModbusUnitId(), bridge, range));
                }
            } else {
                log.error("Invalid Bridge Type. The bridge needs to inherit from ModbusBridge.");
            }
        } else {
            log.error("Invalid Device Type. The Device needs to inherit from ModbusDevice");
        }
    } catch (ConfigException e) {
        log.error("Failed to define modbus protocol!", e);
    } catch (Throwable t) {
        log.error("Some error occured while create ModbusProtocol", t);
    }
}
Also used : WriteableModbusRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange) ModbusRange(io.openems.impl.protocol.modbus.internal.range.ModbusRange) ArrayList(java.util.ArrayList) ConfigException(io.openems.api.exception.ConfigException) ThingChannelsUpdatedListener(io.openems.api.thing.ThingChannelsUpdatedListener) WriteableModbusRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange)

Aggregations

WriteableModbusRange (io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange)2 ConfigException (io.openems.api.exception.ConfigException)1 ThingChannelsUpdatedListener (io.openems.api.thing.ThingChannelsUpdatedListener)1 ModbusRange (io.openems.impl.protocol.modbus.internal.range.ModbusRange)1 ArrayList (java.util.ArrayList)1