Search in sources :

Example 1 with ModbusRange

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

the class ModbusDeviceNature method setAsRequired.

@Override
public /**
 * Sets a Channel as required. The Range with this Channel will be added to ModbusProtocol.RequiredRanges.
 */
void setAsRequired(Channel channel) {
    ModbusRange range = getProtocol().getRangeByChannel(channel);
    Iterator<BridgeReadTask> i = otherReadTasks.iterator();
    while (i.hasNext()) {
        BridgeReadTask task = i.next();
        if (((ModbusBridgeReadTask) task).getRange().equals(range)) {
            this.readTasks.add(task);
            i.remove();
        }
    }
}
Also used : WriteableModbusRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange) ModbusRange(io.openems.impl.protocol.modbus.internal.range.ModbusRange) BridgeReadTask(io.openems.api.bridge.BridgeReadTask)

Example 2 with ModbusRange

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

the class WagoFBInput method defineModbusProtocol.

@Override
protected ModbusProtocol defineModbusProtocol() throws ConfigException {
    List<ModbusCoilRange> ranges = new ArrayList<>();
    HashMap<String, List<String>> channels;
    try {
        channels = WagoFB.getConfig(ip.value());
        for (String key : channels.keySet()) {
            switch(key) {
                case "DI":
                    {
                        List<CoilElement> elements = new ArrayList<>();
                        int count = 0;
                        for (@SuppressWarnings("unused") String channel : channels.get(key)) {
                            ModbusCoilReadChannel ch = new ModbusCoilReadChannel(Integer.toString(count), this);
                            this.channel.add(ch);
                            elements.add(new CoilElement(count, ch));
                            count++;
                            if (count % 63 == 0) {
                                ranges.add(new ModbusCoilRange(elements.get(0).getAddress(), elements.toArray(new CoilElement[elements.size()])));
                                elements.clear();
                            }
                        }
                        if (this.channel.size() > 0) {
                            ranges.add(new ModbusCoilRange(elements.get(0).getAddress(), elements.toArray(new CoilElement[elements.size()])));
                        }
                    }
                    break;
            }
        }
    } catch (InvalidValueException e) {
        log.error("Ip-Address is Invalid", e);
    }
    ModbusProtocol protocol = new ModbusProtocol(ranges.toArray(new ModbusRange[ranges.size()]));
    return protocol;
}
Also used : InvalidValueException(io.openems.api.exception.InvalidValueException) CoilElement(io.openems.impl.protocol.modbus.internal.CoilElement) ModbusCoilRange(io.openems.impl.protocol.modbus.internal.range.ModbusCoilRange) ModbusCoilReadChannel(io.openems.impl.protocol.modbus.ModbusCoilReadChannel) ModbusRange(io.openems.impl.protocol.modbus.internal.range.ModbusRange) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol)

Example 3 with ModbusRange

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

the class WagoFBOutput method defineModbusProtocol.

@Override
protected ModbusProtocol defineModbusProtocol() throws ConfigException {
    List<ModbusCoilRange> ranges = new ArrayList<>();
    HashMap<String, List<String>> channels;
    try {
        channels = WagoFB.getConfig(ip.value());
        for (String key : channels.keySet()) {
            switch(key) {
                case "DO":
                    {
                        List<CoilElement> elements = new ArrayList<>();
                        int count = 0;
                        for (@SuppressWarnings("unused") String channel : channels.get(key)) {
                            ModbusCoilWriteChannel ch = new ModbusCoilWriteChannel(Integer.toString(count), this);
                            this.channel.add(ch);
                            elements.add(new CoilElement(512 + count, ch));
                            count++;
                            if (count % 63 == 0) {
                                ranges.add(new ModbusCoilRange(elements.get(0).getAddress(), elements.toArray(new CoilElement[elements.size()])));
                                elements.clear();
                            }
                        }
                        if (this.channel.size() > 0) {
                            ranges.add(new WriteableModbusCoilRange(elements.get(0).getAddress(), elements.toArray(new CoilElement[elements.size()])));
                        }
                    }
                    break;
            }
        }
    } catch (InvalidValueException e) {
        log.error("Ip-Address is Invalid", e);
    }
    ModbusProtocol protocol = new ModbusProtocol(ranges.toArray(new ModbusRange[ranges.size()]));
    return protocol;
}
Also used : ModbusCoilRange(io.openems.impl.protocol.modbus.internal.range.ModbusCoilRange) WriteableModbusCoilRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusCoilRange) WriteableModbusCoilRange(io.openems.impl.protocol.modbus.internal.range.WriteableModbusCoilRange) ModbusRange(io.openems.impl.protocol.modbus.internal.range.ModbusRange) ArrayList(java.util.ArrayList) ModbusCoilWriteChannel(io.openems.impl.protocol.modbus.ModbusCoilWriteChannel) InvalidValueException(io.openems.api.exception.InvalidValueException) CoilElement(io.openems.impl.protocol.modbus.internal.CoilElement) ArrayList(java.util.ArrayList) List(java.util.List) ModbusProtocol(io.openems.impl.protocol.modbus.internal.ModbusProtocol)

Example 4 with ModbusRange

use of io.openems.impl.protocol.modbus.internal.range.ModbusRange 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

ModbusRange (io.openems.impl.protocol.modbus.internal.range.ModbusRange)4 ArrayList (java.util.ArrayList)3 InvalidValueException (io.openems.api.exception.InvalidValueException)2 CoilElement (io.openems.impl.protocol.modbus.internal.CoilElement)2 ModbusProtocol (io.openems.impl.protocol.modbus.internal.ModbusProtocol)2 ModbusCoilRange (io.openems.impl.protocol.modbus.internal.range.ModbusCoilRange)2 WriteableModbusRange (io.openems.impl.protocol.modbus.internal.range.WriteableModbusRange)2 List (java.util.List)2 BridgeReadTask (io.openems.api.bridge.BridgeReadTask)1 ConfigException (io.openems.api.exception.ConfigException)1 ThingChannelsUpdatedListener (io.openems.api.thing.ThingChannelsUpdatedListener)1 ModbusCoilReadChannel (io.openems.impl.protocol.modbus.ModbusCoilReadChannel)1 ModbusCoilWriteChannel (io.openems.impl.protocol.modbus.ModbusCoilWriteChannel)1 WriteableModbusCoilRange (io.openems.impl.protocol.modbus.internal.range.WriteableModbusCoilRange)1