use of org.openmuc.framework.driver.modbus.ModbusChannel in project OpenMUC by isc-konstanz.
the class ModbusRTUTCPConnection method write.
@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException, ConnectionException {
for (ChannelValueContainer container : containers) {
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.WRITE);
try {
writeChannel(channel, container.getValue());
container.setFlag(Flag.VALID);
} catch (ModbusIOException e) {
logger.error("ModbusIOException while writing channel:" + channel.getChannelAddress(), e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE);
} catch (Exception e) {
logger.error("Exception while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.UNKNOWN_ERROR);
}
}
return null;
}
use of org.openmuc.framework.driver.modbus.ModbusChannel in project OpenMUC by isc-konstanz.
the class ModbusTCPConnection method write.
@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException, ConnectionException {
for (ChannelValueContainer container : containers) {
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.WRITE);
try {
writeChannel(channel, container.getValue());
container.setFlag(Flag.VALID);
} catch (ModbusIOException e) {
logger.error("ModbusIOException while writing channel:" + channel.getChannelAddress(), e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE);
} catch (Exception e) {
logger.error("Exception while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.UNKNOWN_ERROR);
}
}
return null;
}
use of org.openmuc.framework.driver.modbus.ModbusChannel in project OpenMUC by isc-konstanz.
the class ModbusTCPConnection method read.
@Override
public Object read(List<ChannelRecordContainer> containers, Object containerListHandle, String samplingGroup) throws UnsupportedOperationException, ConnectionException {
// reads channels one by one
if (samplingGroup.isEmpty()) {
for (ChannelRecordContainer container : containers) {
// TODO consider retries in sampling timeout (e.g. one time 12000 ms or three times 4000 ms)
// FIXME quite inconvenient/complex to get the timeout from config, since the driver doesn't know the
// device id!
long receiveTime = System.currentTimeMillis();
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.READ);
Value value;
try {
value = readChannel(channel);
if (logger.isTraceEnabled()) {
logger.trace("Value of response: {}", value.toString());
}
container.setRecord(new Record(value, receiveTime));
} catch (ModbusIOException e) {
logger.error("ModbusIOException while reading channel:" + channel.getChannelAddress() + " used timeout: " + timeoutMs + " ms", e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while reading channel: " + channel.getChannelAddress(), e);
container.setRecord(new Record(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE));
} catch (Exception e) {
// catch all possible exceptions and provide info about the channel
logger.error("Exception while reading channel: " + channel.getChannelAddress(), e);
container.setRecord(new Record(Flag.UNKNOWN_ERROR));
}
if (!connection.isConnected()) {
throw new ConnectionException("Lost connection.");
}
}
} else // reads whole samplingGroup at once
{
readChannelGroupHighLevel(containers, containerListHandle, samplingGroup);
if (!connection.isConnected()) {
throw new ConnectionException("Lost connection.");
}
}
return null;
}
use of org.openmuc.framework.driver.modbus.ModbusChannel in project OpenMUC by isc-konstanz.
the class ModbusTcpChannelTest method testValidWriteAddresses.
@Test
public void testValidWriteAddresses() {
ArrayList<String> validAddresses = new ArrayList<>();
validAddresses.add("0:COILS:0:BOOLEAN");
validAddresses.add("0:HOLDING_REGISTERS:0:SHORT");
validAddresses.add("0:HOLDING_REGISTERS:0:INT16");
validAddresses.add("0:HOLDING_REGISTERS:0:FLOAT");
validAddresses.add("0:HOLDING_REGISTERS:0:DOUBLE");
validAddresses.add("0:HOLDING_REGISTERS:0:LONG");
for (String channelAddress : validAddresses) {
try {
ModbusChannel channel = new ModbusChannel(channelAddress, EAccess.WRITE);
String testString = concatenate(channel.getAccessFlag(), channel.getPrimaryTable(), channel.getDatatype());
if (!validAddressCombinations.contains(testString.toUpperCase())) {
fail(testString + "is not a valid paramaeter combination");
} else {
System.out.println(channelAddress + " and resulting " + testString.toUpperCase() + " are valid.");
}
} catch (Exception e) {
e.printStackTrace();
fail("unexpected exception");
}
}
}
use of org.openmuc.framework.driver.modbus.ModbusChannel in project OpenMUC by isc-konstanz.
the class ModbusRTUConnection method write.
@Override
public Object write(List<ChannelValueContainer> containers, Object containerListHandle) throws UnsupportedOperationException, ConnectionException {
for (ChannelValueContainer container : containers) {
ModbusChannel channel = getModbusChannel(container.getChannelAddress(), EAccess.WRITE);
try {
writeChannel(channel, container.getValue());
container.setFlag(Flag.VALID);
} catch (ModbusIOException e) {
logger.error("ModbusIOException while writing channel:" + channel.getChannelAddress(), e);
disconnect();
throw new ConnectionException("Try to solve issue with reconnect.");
} catch (ModbusException e) {
logger.error("ModbusException while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.DRIVER_ERROR_CHANNEL_NOT_ACCESSIBLE);
} catch (Exception e) {
logger.error("Exception while writing channel: " + channel.getChannelAddress(), e);
container.setFlag(Flag.UNKNOWN_ERROR);
}
}
return null;
}
Aggregations