Search in sources :

Example 1 with ModbusChannel

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) UnknownHostException(java.net.UnknownHostException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Example 2 with ModbusChannel

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Example 3 with ModbusChannel

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) Value(org.openmuc.framework.data.Value) Record(org.openmuc.framework.data.Record) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Example 4 with ModbusChannel

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");
        }
    }
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 5 with ModbusChannel

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;
}
Also used : ModbusChannel(org.openmuc.framework.driver.modbus.ModbusChannel) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ScanException(org.openmuc.framework.config.ScanException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException) ModbusException(com.ghgande.j2mod.modbus.ModbusException) ChannelValueContainer(org.openmuc.framework.driver.spi.ChannelValueContainer) ModbusException(com.ghgande.j2mod.modbus.ModbusException)

Aggregations

ModbusChannel (org.openmuc.framework.driver.modbus.ModbusChannel)8 ModbusException (com.ghgande.j2mod.modbus.ModbusException)6 ModbusIOException (com.ghgande.j2mod.modbus.ModbusIOException)6 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)6 ScanException (org.openmuc.framework.config.ScanException)6 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)6 Record (org.openmuc.framework.data.Record)3 Value (org.openmuc.framework.data.Value)3 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)3 ChannelValueContainer (org.openmuc.framework.driver.spi.ChannelValueContainer)3 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2