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