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();
}
}
}
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;
}
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;
}
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);
}
}
Aggregations