Search in sources :

Example 1 with ModbusCoilWriteChannel

use of io.openems.impl.protocol.modbus.ModbusCoilWriteChannel 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)

Aggregations

InvalidValueException (io.openems.api.exception.InvalidValueException)1 ModbusCoilWriteChannel (io.openems.impl.protocol.modbus.ModbusCoilWriteChannel)1 CoilElement (io.openems.impl.protocol.modbus.internal.CoilElement)1 ModbusProtocol (io.openems.impl.protocol.modbus.internal.ModbusProtocol)1 ModbusCoilRange (io.openems.impl.protocol.modbus.internal.range.ModbusCoilRange)1 ModbusRange (io.openems.impl.protocol.modbus.internal.range.ModbusRange)1 WriteableModbusCoilRange (io.openems.impl.protocol.modbus.internal.range.WriteableModbusCoilRange)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1