Skip to content

Commit 4dd7df2

Browse files
committed
Finish testing with alternative setup
1 parent 3bc040b commit 4dd7df2

File tree

7 files changed

+66
-40
lines changed

7 files changed

+66
-40
lines changed

modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private void calculateDelta(@NotNull final ModBusData modBusData, @NotNull final
209209
final @NotNull ModbusClient modbusClient) {
210210
final AddressRange addressRange = modbusToMqttMapping.getAddressRange();
211211

212-
return doRead(addressRange.startIdx, addressRange.unitId, modbusToMqttMapping.getDataType(), addressRange.readType, modbusClient)
212+
return doRead(addressRange.startIdx, addressRange.unitId, addressRange.flipRegisters, modbusToMqttMapping.getDataType(), addressRange.readType, modbusClient)
213213
.thenApply(dataPoint -> {
214214
final ModBusData data = new ModBusData(modbusToMqttMapping);
215215
data.addDataPoint(dataPoint);
@@ -220,6 +220,7 @@ private void calculateDelta(@NotNull final ModBusData modBusData, @NotNull final
220220
protected static CompletableFuture<DataPoint> doRead(
221221
final int startIdx,
222222
final int unitId,
223+
final boolean flipRegisters,
223224
final @NotNull ModbusDataType dataType,
224225
final @NotNull ModbusAdu readType,
225226
final @NotNull ModbusClient modbusClient) {
@@ -228,13 +229,15 @@ protected static CompletableFuture<DataPoint> doRead(
228229
.readHoldingRegisters(
229230
startIdx,
230231
dataType,
231-
unitId);
232+
unitId,
233+
flipRegisters);
232234
} else if (INPUT_REGISTERS.equals(readType)) {
233235
return modbusClient
234236
.readInputRegisters(
235237
startIdx,
236238
dataType,
237-
unitId);
239+
unitId,
240+
flipRegisters);
238241
} else if (COILS.equals(readType)) {
239242
return modbusClient
240243
.readCoils(

modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public ModbusProtocolAdapterFactory(final boolean writingEnabled) {
105105
context.getIncludeTimestamp(),
106106
context.getIncludeTagNames(),
107107
context.getLegacyProperties(),
108-
new AddressRange(context.getAddressRange().startIdx, ModbusAdu.HOLDING_REGISTERS, 0),
108+
new AddressRange(context.getAddressRange().startIdx, ModbusAdu.HOLDING_REGISTERS, 0, false),
109109
ModbusDataType.INT_32))
110110
.collect(Collectors.toList());
111111

modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/config/AddressRange.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,20 @@ public class AddressRange {
4242
required = true)
4343
public final int unitId;
4444

45+
@JsonProperty(value = "flipRegisters", defaultValue = "false")
46+
@ModuleConfigField(title = "Indicates if registers should be evaluated in reverse order",
47+
description = "Registers and their contents are normally written/read as big endian, some implementations decided to write the content as big endian but to order the actual registers as little endian.",
48+
defaultValue = "false")
49+
public final boolean flipRegisters;
50+
4551
public AddressRange(
4652
@JsonProperty(value = "startIdx", required = true) final int startIdx,
4753
@JsonProperty(value = "readType", required = true) final ModbusAdu readType,
48-
@JsonProperty(value = "unitId", required = true) final int unitId) {
54+
@JsonProperty(value = "unitId", required = true) final int unitId,
55+
@JsonProperty(value = "flipRegisters", defaultValue = "false") final boolean flipRegisters) {
4956
this.startIdx = startIdx;
5057
this.readType = readType;
5158
this.unitId = unitId;
59+
this.flipRegisters = flipRegisters;
5260
}
5361
}

modules/hivemq-edge-module-modbus/src/main/java/com/hivemq/edge/adapters/modbus/impl/ModbusClient.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public CompletableFuture<Void> connect() {
7979
.thenApply(response -> {
8080
try {
8181
final ByteBuf buf = response.getCoilStatus();
82-
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + 1),
83-
convert(buf, ModbusDataType.BOOL, 1));
82+
return dataPointFactory.create("registers-" + startIdx,
83+
convert(buf, ModbusDataType.BOOL, 1, false));
8484
} finally {
8585
ReferenceCountUtil.release(response);
8686
}
@@ -97,8 +97,8 @@ public CompletableFuture<Void> connect() {
9797
.thenApply(response -> {
9898
try {
9999
final ByteBuf buf = response.getInputStatus();
100-
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + 1),
101-
convert(buf, ModbusDataType.BOOL, 1));
100+
return dataPointFactory.create("registers-" + startIdx,
101+
convert(buf, ModbusDataType.BOOL, 1, false));
102102
} finally {
103103
ReferenceCountUtil.release(response);
104104
}
@@ -108,16 +108,16 @@ public CompletableFuture<Void> connect() {
108108
/**
109109
* Holding registers are 16bit.
110110
*/
111-
public @NotNull CompletableFuture<DataPoint> readHoldingRegisters(final int startIdx, final @NotNull ModbusDataType dataType, final int unitId) {
111+
public @NotNull CompletableFuture<DataPoint> readHoldingRegisters(final int startIdx, final @NotNull ModbusDataType dataType, final int unitId, final boolean flipRegisters) {
112112

113113
return modbusClient
114114
.<ReadHoldingRegistersResponse>sendRequest(new ReadHoldingRegistersRequest(startIdx, Math.min(dataType.nrOfRegistersToRead,
115115
DEFAULT_MAX_INPUT_REGISTERS)), unitId)
116116
.thenApply(response -> {
117117
try {
118118
final ByteBuf buf = response.getRegisters();
119-
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + dataType.nrOfRegistersToRead),
120-
convert(buf, dataType, dataType.nrOfRegistersToRead));
119+
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + dataType.nrOfRegistersToRead - 1),
120+
convert(buf, dataType, dataType.nrOfRegistersToRead, flipRegisters));
121121
} finally {
122122
ReferenceCountUtil.release(response);
123123
}
@@ -127,15 +127,15 @@ public CompletableFuture<Void> connect() {
127127
/**
128128
* Inout registers are 16bit.
129129
*/
130-
public @NotNull CompletableFuture<DataPoint> readInputRegisters(final int startIdx, final @NotNull ModbusDataType dataType, final int unitId) {
130+
public @NotNull CompletableFuture<DataPoint> readInputRegisters(final int startIdx, final @NotNull ModbusDataType dataType, final int unitId, final boolean flipRegisters) {
131131
return modbusClient
132132
.<ReadInputRegistersResponse>sendRequest(new ReadInputRegistersRequest(startIdx, Math.min(dataType.nrOfRegistersToRead,
133133
DEFAULT_MAX_INPUT_REGISTERS)), unitId)
134134
.thenApply(response -> {
135135
try {
136136
final ByteBuf buf = response.getRegisters();
137-
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + dataType.nrOfRegistersToRead),
138-
convert(buf, dataType, dataType.nrOfRegistersToRead));
137+
return dataPointFactory.create("registers-" + startIdx + "-" + (startIdx + dataType.nrOfRegistersToRead - 1),
138+
convert(buf, dataType, dataType.nrOfRegistersToRead, flipRegisters));
139139
} finally {
140140
ReferenceCountUtil.release(response);
141141
}
@@ -149,8 +149,7 @@ public CompletableFuture<Void> disconnect() {
149149
}
150150

151151
private @NotNull Object convert(
152-
final @NotNull ByteBuf buffi, final @NotNull ModbusDataType dataType, final int count) {
153-
boolean registersLittleEndian = true;
152+
final @NotNull ByteBuf buffi, final @NotNull ModbusDataType dataType, final int count, final boolean flipRegisters) {
154153
switch (dataType) {
155154
case BOOL:
156155
return buffi.readBoolean();
@@ -159,46 +158,60 @@ public CompletableFuture<Void> disconnect() {
159158
case UINT_16:
160159
return Short.toUnsignedInt(buffi.readShort());
161160
case INT_32:
162-
if(registersLittleEndian) {
163-
byte[] bytes = ByteBuffer.allocate(4).putInt(-2147483647).array();
161+
if(flipRegisters) {
164162
byte b1 = buffi.readByte();
165163
byte b2 = buffi.readByte();
166164
byte b3 = buffi.readByte();
167165
byte b4 = buffi.readByte();
168-
System.out.println("SINT " + String.format("%02x", b1) + " " + String.format("%02x", b2) + " " + String.format("%02x", b3) + " " + String.format("%02x", b4));
169-
System.out.println("SINT " + String.format("%02x", bytes[2]) + " " + String.format("%02x", bytes[3]) + " " + String.format("%02x", bytes[0]) + " " + String.format("%02x", bytes[1]));
170-
ByteBuffer bb = ByteBuffer.wrap(new byte[] {b3 ,b4, b1, b2});
171-
return bb.getInt();
166+
return Unpooled.wrappedBuffer(new byte[] {b4 ,b3, b2, b1}).readInt();
172167
} else {
173168
return buffi.readInt();
174169
}
175170
case UINT_32:
176-
if(registersLittleEndian) {
171+
if(flipRegisters) {
177172
byte b1 = buffi.readByte();
178173
byte b2 = buffi.readByte();
179174
byte b3 = buffi.readByte();
180175
byte b4 = buffi.readByte();
181-
System.out.println("UINT " + String.format("%02x", b1) + " " + String.format("%02x", b2) + " " + String.format("%02x", b3) + " " + String.format("%02x", b4));
182-
ByteBuffer bb = ByteBuffer.wrap(new byte[] {b4 ,b3, b2, b1});
183-
return bb.getInt();
176+
return Unpooled.wrappedBuffer(new byte[] {b3 ,b4, b1, b2}).readUnsignedInt();
184177
} else {
185178
return buffi.readUnsignedInt();
186179
}
187180
case INT_64:
188-
if(registersLittleEndian) {
189-
return buffi.readUnsignedShort() + (buffi.readUnsignedShort() << 16);
181+
if(flipRegisters) {
182+
byte b1 = buffi.readByte();
183+
byte b2 = buffi.readByte();
184+
byte b3 = buffi.readByte();
185+
byte b4 = buffi.readByte();
186+
byte b5 = buffi.readByte();
187+
byte b6 = buffi.readByte();
188+
byte b7 = buffi.readByte();
189+
byte b8 = buffi.readByte();
190+
return Unpooled.wrappedBuffer(new byte[] {b7 ,b8, b5 ,b6, b3 ,b4, b1, b2}).readUnsignedInt();
190191
} else {
191192
return buffi.readLong();
192193
}
193194
case FLOAT_32:
194-
if(registersLittleEndian) {
195-
return buffi.readUnsignedShort() + (buffi.readUnsignedShort() << 16);
195+
if(flipRegisters) {
196+
byte b1 = buffi.readByte();
197+
byte b2 = buffi.readByte();
198+
byte b3 = buffi.readByte();
199+
byte b4 = buffi.readByte();
200+
return Unpooled.wrappedBuffer(new byte[] {b3 ,b4, b1, b2}).readFloat();
196201
} else {
197202
return buffi.readFloat();
198203
}
199204
case FLOAT_64:
200-
if(registersLittleEndian) {
201-
return buffi.readUnsignedShort() + (buffi.readUnsignedShort() << 16);
205+
if(flipRegisters) {
206+
byte b1 = buffi.readByte();
207+
byte b2 = buffi.readByte();
208+
byte b3 = buffi.readByte();
209+
byte b4 = buffi.readByte();
210+
byte b5 = buffi.readByte();
211+
byte b6 = buffi.readByte();
212+
byte b7 = buffi.readByte();
213+
byte b8 = buffi.readByte();
214+
return Unpooled.wrappedBuffer(new byte[] {b7 ,b8, b5 ,b6, b3 ,b4, b1, b2}).readUnsignedInt();
202215
} else {
203216
return buffi.readDouble();
204217
}

modules/hivemq-edge-module-modbus/src/test/java/com/hivemq/edge/adapters/modbus/ModbusMainTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
public class ModbusMainTest {
1010
public static void main(String[] args) throws Exception {
1111

12-
ModbusAdapterConfig modbusAdapterConfig = new ModbusAdapterConfig("1", 502, "172.16.10.12", 5000, null) ;
12+
String host = args[0];
13+
int port = Integer.parseInt(args[1]);
14+
15+
ModbusAdapterConfig modbusAdapterConfig = new ModbusAdapterConfig("1", port, host, 5000, null) ;
1316
ModbusClient modbusClient = new ModbusClient(modbusAdapterConfig, DataPointImpl::new);
1417

1518
modbusClient.connect().get();
1619

17-
//41335
18-
DataPoint dataPoint = modbusClient.readInputRegisters(30021, ModbusDataType.INT_32, 12).get();
20+
DataPoint dataPoint = modbusClient.readHoldingRegisters(100, ModbusDataType.INT_32, 255, false).get();
1921

2022
System.out.println(dataPoint.getTagName() + " " + dataPoint.getTagValue());
2123
}

modules/hivemq-edge-module-modbus/src/test/java/com/hivemq/edge/adapters/modbus/ModbusProtocolAdapterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected static ModBusData createSampleData() {
6868
true,
6969
false,
7070
List.of(),
71-
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0),
71+
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0, false),
7272
ModbusDataType.INT_16);
7373
final ModBusData data = new ModBusData(pollingContext);
7474
IntStream.range(0, 10).forEach(i -> data.addDataPoint(new DataPointImpl("register-" + i, i)));

modules/hivemq-edge-module-modbus/src/test/java/com/hivemq/edge/adapters/modbus/config/ModbusAdapterConfigTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public void unconvertConfigObject_full_valid() throws Exception {
216216
false,
217217
true,
218218
List.of(new MqttUserProperty("my-name", "my-value")),
219-
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0),
219+
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0, false),
220220
ModbusDataType.UINT_16);
221221

222222
final ModbusAdapterConfig modbusAdapterConfig = new ModbusAdapterConfig("my-modbus-adapter",
@@ -264,7 +264,7 @@ public void unconvertConfigObject_defaults() throws ProtocolAdapterException {
264264
null,
265265
null,
266266
null,
267-
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0),
267+
new AddressRange(1, ModbusAdu.HOLDING_REGISTERS, 0, false),
268268
null);
269269

270270
final ModbusToMqttMapping pollingContext2 = new ModbusToMqttMapping("my/destination/2",
@@ -273,7 +273,7 @@ public void unconvertConfigObject_defaults() throws ProtocolAdapterException {
273273
null,
274274
null,
275275
null,
276-
new AddressRange(10, ModbusAdu.HOLDING_REGISTERS, 0),
276+
new AddressRange(10, ModbusAdu.HOLDING_REGISTERS, 0, false),
277277
null);
278278

279279

0 commit comments

Comments
 (0)