Skip to content

Commit

Permalink
Modbus: add datatype int32 + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micw committed Oct 2, 2024
1 parent 5a56750 commit 8f16d04
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/de/wyraz/homedatabroker/source/ModBusIPSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public Number decode(byte[] data, int offset) {
data[offset++] & 0xFF) << 16 >> 16;
}
},
int32(4) {
@Override
public Number decode(byte[] data, int offset) {
return
(data[offset++] & 0xFF) << 24 |
(data[offset++] & 0xFF) << 16 |
(data[offset++] & 0xFF) << 8 |
data[offset++] & 0xFF;
}
},
uint16(2) {
@Override
public Number decode(byte[] data, int offset) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package de.wyraz.homedatabroker.source;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;

import de.wyraz.homedatabroker.source.ModBusIPSource.ModBusRegFormat;

public class ModBusRegFormatTest {

@Test
public void testDecodeInt16() {
assertThat(ModBusRegFormat.int16.decode(new byte[] {(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(-1);
assertThat(ModBusRegFormat.int16.decode(new byte[] {(byte)0x80,(byte)0x00}, 0))
.isEqualTo(-32768);
assertThat(ModBusRegFormat.int16.decode(new byte[] {(byte)0x7F,(byte)0xFF}, 0))
.isEqualTo(32767);
assertThat(ModBusRegFormat.int16.decode(new byte[] {(byte)0x00,(byte)0x01}, 0))
.isEqualTo(1);
}
@Test
public void testDecodeUint16() {
assertThat(ModBusRegFormat.uint16.decode(new byte[] {(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(65535);
assertThat(ModBusRegFormat.uint16.decode(new byte[] {(byte)0x80,(byte)0x00}, 0))
.isEqualTo(32768);
assertThat(ModBusRegFormat.uint16.decode(new byte[] {(byte)0x7F,(byte)0xFF}, 0))
.isEqualTo(32767);
assertThat(ModBusRegFormat.uint16.decode(new byte[] {(byte)0x00,(byte)0x01}, 0))
.isEqualTo(1);
}

@Test
public void testDecodeInt32() {
assertThat(ModBusRegFormat.int32.decode(new byte[] {(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(-1);
assertThat(ModBusRegFormat.int32.decode(new byte[] {(byte)0x80,(byte)0x00,(byte)0x00,(byte)0x00}, 0))
.isEqualTo(-2147483648);
assertThat(ModBusRegFormat.int32.decode(new byte[] {(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(2147483647);
assertThat(ModBusRegFormat.int32.decode(new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01}, 0))
.isEqualTo(1);
}

@Test
public void testDecodeUint32() {
assertThat(ModBusRegFormat.uint32.decode(new byte[] {(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(4294967295L);
assertThat(ModBusRegFormat.uint32.decode(new byte[] {(byte)0x80,(byte)0x00,(byte)0x00,(byte)0x00}, 0))
.isEqualTo(2147483648L);
assertThat(ModBusRegFormat.uint32.decode(new byte[] {(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF}, 0))
.isEqualTo(2147483647L);
assertThat(ModBusRegFormat.uint32.decode(new byte[] {(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x01}, 0))
.isEqualTo(1L);
}

}

0 comments on commit 8f16d04

Please sign in to comment.