Skip to content

Commit

Permalink
fixed NPE on ModBus sendFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
ai-republic committed Oct 22, 2024
1 parent 4c93a60 commit d6d40b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,29 +111,29 @@ public ByteBuffer receiveFrame() throws IOException {
@Override
public void sendFrame(final ByteBuffer frame) throws IOException {
final int functionCode = frame.getInt();
// Modbus register addresses are 1-based, so subtract 1 for the j2mod library
final int startAddress = frame.getInt() - 1;
final int numRegisters = frame.getInt();
final int unitId = frame.getInt();

switch (functionCode) {
case Modbus.READ_INPUT_REGISTERS:
readInputRegisters(frame);
readInputRegisters(startAddress, numRegisters, unitId);
break;
case Modbus.READ_HOLDING_REGISTERS:
readHoldingRegisters(frame);
readHoldingRegisters(startAddress, numRegisters, unitId);
break;
case Modbus.WRITE_SINGLE_REGISTER:
writeSingleRegister(frame);
writeSingleRegister(startAddress, numRegisters, unitId, frame);
break;
case Modbus.WRITE_MULTIPLE_REGISTERS:
writeMultipleRegisters(frame);
writeMultipleRegisters(startAddress, numRegisters, unitId, frame);
break;
}
}


private void readInputRegisters(final ByteBuffer frame) throws IOException {
// Modbus register addresses are 1-based, so subtract 1 for the j2mod library
final int startAddress = frame.getInt() - 1;
final int numRegisters = frame.getInt();
final int unitId = frame.getInt();
private void readInputRegisters(final int startAddress, final int numRegisters, final int unitId) throws IOException {
ModbusSerialTransaction transaction = null;
ReadInputRegistersResponse response = null;
ReadInputRegistersRequest request = null;
Expand All @@ -157,11 +157,8 @@ private void readInputRegisters(final ByteBuffer frame) throws IOException {
}


private void readHoldingRegisters(final ByteBuffer frame) throws IOException {
private void readHoldingRegisters(final int startAddress, final int numRegisters, final int unitId) throws IOException {
// Modbus register addresses are 1-based, so subtract 1 for the j2mod library
final int startAddress = frame.getInt() - 1;
final int numRegisters = frame.getInt();
final int unitId = frame.getInt();
ModbusSerialTransaction transaction = null;
ReadMultipleRegistersRequest request = null;
ReadMultipleRegistersResponse response = null;
Expand All @@ -187,11 +184,7 @@ private void readHoldingRegisters(final ByteBuffer frame) throws IOException {
}


private void writeMultipleRegisters(final ByteBuffer frame) throws IOException {
// Modbus register addresses are 1-based, so subtract 1 for the j2mod library
final int startAddress = frame.getInt() - 1;
final int numRegisters = frame.getInt();
final int unitId = frame.getInt();
private void writeMultipleRegisters(final int startAddress, final int numRegisters, final int unitId, final ByteBuffer frame) throws IOException {
final Register[] registers = new Register[numRegisters];

for (int i = 0; i < numRegisters; i++) {
Expand Down Expand Up @@ -221,12 +214,7 @@ private void writeMultipleRegisters(final ByteBuffer frame) throws IOException {
}


private void writeSingleRegister(final ByteBuffer frame) throws IOException {
// Modbus register addresses are 1-based, so subtract 1 for the j2mod library
final int startAddress = frame.getInt() - 1;
final int numRegisters = frame.getInt();
final int unitId = frame.getInt();

private void writeSingleRegister(final int startAddress, final int numRegisters, final int unitId, final ByteBuffer frame) throws IOException {
ModbusSerialTransaction transaction = null;
WriteSingleRegisterResponse response = null;
WriteSingleRegisterRequest request = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package com.airepublic.bmstoinverter.protocol.modbus;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.stream.Stream;

import com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse;
Expand Down Expand Up @@ -51,12 +52,14 @@ public int getFunctionCode() {
}

public static ByteBuffer createRequestBuffer(final RegisterCode register, final int startAddress, final int numRegisters, final int unitId) {
final ByteBuffer buffer = ByteBuffer.allocate(16);
final ByteBuffer buffer = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(register.getFunctionCode());
buffer.putInt(startAddress);
buffer.putInt(numRegisters);
buffer.putInt(unitId);

buffer.rewind();

return buffer;
}

Expand Down

0 comments on commit d6d40b6

Please sign in to comment.