Search in sources :

Example 1 with InputRegister

use of com.ghgande.j2mod.modbus.procimg.InputRegister in project openems by OpenEMS.

the class StreetscooterTest method main.

public static void main(String[] args) {
    ModbusTCPMaster master = new ModbusTCPMaster("localhost", 502, 10000, true);
    try {
        master.connect();
        // Register[] registers = master.readMultipleRegisters(30011, 2);
        InputRegister[] registers = master.readInputRegisters(100, 6, 4);
        for (InputRegister register : registers) {
            System.out.println(register);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        master.disconnect();
    }
}
Also used : ModbusTCPMaster(com.ghgande.j2mod.modbus.facade.ModbusTCPMaster) InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister)

Example 2 with InputRegister

use of com.ghgande.j2mod.modbus.procimg.InputRegister in project OpenMUC by isc-konstanz.

the class ModbusChannelGroup method setChannelValues.

public void setChannelValues(InputRegister[] inputRegisters, List<ChannelRecordContainer> containers) {
    for (ModbusChannel channel : channels) {
        // determine start index of the registers which contain the values of the channel
        int registerIndex = channel.getStartAddress() - getStartAddress();
        // create a temporary register array
        InputRegister[] registers = new InputRegister[channel.getCount()];
        // copy relevant registers for the channel
        System.arraycopy(inputRegisters, registerIndex, registers, 0, channel.getCount());
        // now we have a register array which contains the value of the channel
        ChannelRecordContainer container = searchContainer(channel.getChannelAddress(), containers);
        long receiveTime = System.currentTimeMillis();
        Value value = ModbusDriverUtil.getRegistersValue(registers, channel.getDatatype(), channel.getSwap());
        if (logger.isTraceEnabled()) {
            logger.trace("response value channel " + channel.getChannelAddress() + ": " + value.toString());
        }
        container.setRecord(new Record(value, receiveTime));
    }
}
Also used : ChannelRecordContainer(org.openmuc.framework.driver.spi.ChannelRecordContainer) InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister) Value(org.openmuc.framework.data.Value) BooleanValue(org.openmuc.framework.data.BooleanValue) Record(org.openmuc.framework.data.Record)

Example 3 with InputRegister

use of com.ghgande.j2mod.modbus.procimg.InputRegister in project energy by nielsbasjes.

the class ModBusDataReader method getRawRegisterBytes.

public byte[] getRawRegisterBytes(int base, int len) throws ModbusException {
    byte[] bytes = new byte[len * 2];
    int i = 0;
    int remaining = len;
    while (remaining > 0) {
        int readSize = Math.min(remaining, maxRegistersPerModbusRequest);
        remaining -= readSize;
        final InputRegister[] registers = read(base, readSize);
        for (InputRegister register : registers) {
            byte[] registerBytes = register.toBytes();
            bytes[i++] = registerBytes[0];
            bytes[i++] = registerBytes[1];
        }
    }
    return bytes;
}
Also used : InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister)

Example 4 with InputRegister

use of com.ghgande.j2mod.modbus.procimg.InputRegister in project energy by nielsbasjes.

the class ModBusDataReader method readASCII.

public String readASCII(int registerAddress, int len) throws ModbusException {
    if (!isConnected) {
        throw new ModbusIOException("Not connected");
    }
    final InputRegister[] registers = master.readMultipleRegisters(unitId, registerAddress, len);
    char[] chars = new char[len * 2];
    int offset = 0;
    for (InputRegister register : registers) {
        chars[offset] = (char) ((register.getValue() & 0x7f00) >> 8);
        if (chars[offset] == 0x00)
            break;
        offset++;
        chars[offset] = (char) (register.getValue() & 0x7f);
        if (chars[offset] == 0x00)
            break;
        offset++;
    }
    return new String(chars, 0, offset);
}
Also used : InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister) ModbusIOException(com.ghgande.j2mod.modbus.ModbusIOException)

Example 5 with InputRegister

use of com.ghgande.j2mod.modbus.procimg.InputRegister in project OpenMUC by isc-konstanz.

the class ModbusConnection method readChannelGroup.

private void readChannelGroup(ModbusChannelGroup channelGroup, List<ChannelRecordContainer> containers) throws ModbusException {
    switch(channelGroup.getFunctionCode()) {
        case FC_01_READ_COILS:
            BitVector coils = readCoils(channelGroup);
            channelGroup.setChannelValues(coils, containers);
            break;
        case FC_02_READ_DISCRETE_INPUTS:
            BitVector discretInput = readDiscreteInputs(channelGroup);
            channelGroup.setChannelValues(discretInput, containers);
            break;
        case FC_03_READ_HOLDING_REGISTERS:
            Register[] registers = readHoldingRegisters(channelGroup);
            channelGroup.setChannelValues(registers, containers);
            break;
        case FC_04_READ_INPUT_REGISTERS:
            InputRegister[] inputRegisters = readInputRegisters(channelGroup);
            channelGroup.setChannelValues(inputRegisters, containers);
            break;
        default:
            throw new RuntimeException("FunctionCode " + channelGroup.getFunctionCode() + " not supported yet");
    }
}
Also used : BitVector(com.ghgande.j2mod.modbus.util.BitVector) InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister) SimpleInputRegister(com.ghgande.j2mod.modbus.procimg.SimpleInputRegister) InputRegister(com.ghgande.j2mod.modbus.procimg.InputRegister) SimpleInputRegister(com.ghgande.j2mod.modbus.procimg.SimpleInputRegister) Register(com.ghgande.j2mod.modbus.procimg.Register)

Aggregations

InputRegister (com.ghgande.j2mod.modbus.procimg.InputRegister)5 ModbusIOException (com.ghgande.j2mod.modbus.ModbusIOException)1 ModbusTCPMaster (com.ghgande.j2mod.modbus.facade.ModbusTCPMaster)1 Register (com.ghgande.j2mod.modbus.procimg.Register)1 SimpleInputRegister (com.ghgande.j2mod.modbus.procimg.SimpleInputRegister)1 BitVector (com.ghgande.j2mod.modbus.util.BitVector)1 BooleanValue (org.openmuc.framework.data.BooleanValue)1 Record (org.openmuc.framework.data.Record)1 Value (org.openmuc.framework.data.Value)1 ChannelRecordContainer (org.openmuc.framework.driver.spi.ChannelRecordContainer)1