me.martiii.modbustcpjava.protocol.responses.ReadResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of modbus-tcp-java Show documentation
Show all versions of modbus-tcp-java Show documentation
Modbus-TCP protocol for java.
The newest version!
package me.martiii.modbustcpjava.protocol.responses;
import me.martiii.modbustcpjava.protocol.ModbusResponse;
public class ReadResponse{
private int numberBytes;
private byte[] valuesData;
public ReadResponse(ModbusResponse response) {
byte[] data = response.getData();
numberBytes = data[0] & 0xff;
valuesData = new byte[data.length - 1];
System.arraycopy(data, 1, valuesData, 0, data.length - 1);
}
public int getNumberRegisters() {
return numberBytes / 2;
}
public int[] getUnsignedRegisters() {
int[] registers = new int[getNumberRegisters()];
for (int i = 0; i < getNumberRegisters(); i++) {
registers[i] = ((valuesData[i * 2] & 0xff) << 8) | (valuesData[i * 2 + 1] & 0xff);
}
return registers;
}
public int[] getRegisters() {
int[] registers = new int[getNumberRegisters()];
for (int i = 0; i < getNumberRegisters(); i++) {
registers[i] = ((valuesData[i * 2]) << 8) | (valuesData[i * 2 + 1] & 0xff);
}
return registers;
}
public int getNumberCoils() {
return numberBytes * 8;
}
public boolean[] getCoils() {
boolean[] coils = new boolean[getNumberCoils()];
for (int i = 0; i < numberBytes; i++) {
for (int b = 0; b < 8; b++) {
coils[i * 8 + b] = (valuesData[i] & (byte) Math.pow(2, b)) != 0;
}
}
return coils;
}
}