Skip to content

Commit

Permalink
add support for Modbus BIG_ENDIAN_WORD_SWAP and LITTLE_ENDIAN_WORD_SW…
Browse files Browse the repository at this point in the history
…AP (#1632)

modbus support BIG_ENDIAN_WORD_SWAP
modbus support LITTLE_ENDIAN_WORD_SWAP
  • Loading branch information
StrawberryBlue committed Jun 14, 2024
1 parent 05a3ca2 commit a864274
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
package org.apache.plc4x.java.spi.generation;

public enum ByteOrder {
//A B C D
BIG_ENDIAN,
LITTLE_ENDIAN
//D C B A
LITTLE_ENDIAN,
//B A D C
BIG_ENDIAN_WORD_SWAP,
//C D A B
LITTLE_ENDIAN_WORD_SWAP

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.jinahya.bit.io.ArrayByteInput;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.spi.generation.io.MyDefaultBitInput;
import org.apache.plc4x.java.spi.utils.ByteOrderUtils;

import java.io.IOException;
import java.math.BigDecimal;
Expand Down Expand Up @@ -250,11 +251,16 @@ public int readUnsignedInt(String logicalName, int bitLength, WithReaderArgs...
}
return value;
case "default":
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
final int longValue = bi.readInt(true, bitLength);
return Integer.reverseBytes(longValue) >>> (32 - bitLength);
switch (byteOrder){
case LITTLE_ENDIAN:
return ByteOrderUtils.LittleEndian(bi.readInt(true, bitLength));
case BIG_ENDIAN_WORD_SWAP:
return ByteOrderUtils.BigEndianWordSwap(bi.readInt(true, bitLength));
case LITTLE_ENDIAN_WORD_SWAP:
return ByteOrderUtils.LittleEndianWordSwap(bi.readInt(true, bitLength));
default:
return ByteOrderUtils.BigEndian(bi.readInt(true, bitLength));
}
return bi.readInt(true, bitLength);
default:
throw new ParseException("unsupported encoding '" + encoding + "'");
}
Expand Down Expand Up @@ -303,11 +309,17 @@ public long readUnsignedLong(String logicalName, int bitLength, WithReaderArgs..
}
return value;
case "default":
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
final long longValue = bi.readLong(true, bitLength);
return Long.reverseBytes(longValue) >>> 32;
final long longValue = bi.readLong(true, bitLength);
switch (byteOrder){
case LITTLE_ENDIAN:
return ByteOrderUtils.LittleEndian(longValue);
case BIG_ENDIAN_WORD_SWAP:
return ByteOrderUtils.BigEndianWordSwap(longValue);
case LITTLE_ENDIAN_WORD_SWAP:
return ByteOrderUtils.LittleEndianWordSwap(longValue);
default:
return ByteOrderUtils.BigEndian(longValue);
}
return bi.readLong(true, bitLength);
default:
throw new ParseException("unsupported encoding '" + encoding + "'");
}
Expand Down Expand Up @@ -359,9 +371,20 @@ public BigInteger readUnsignedBigInteger(String logicalName, int bitLength, With
case "default":
// Read as signed value
long val = bi.readLong(false, bitLength);
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
val = Long.reverseBytes(val);
switch (byteOrder){
case LITTLE_ENDIAN:
val = ByteOrderUtils.LittleEndian(val);
break;
case BIG_ENDIAN_WORD_SWAP:
val = ByteOrderUtils.BigEndianWordSwap(val);
break;
case LITTLE_ENDIAN_WORD_SWAP:
val = ByteOrderUtils.LittleEndianWordSwap(val);
break;
default:
break;
}

if (val >= 0) {
return BigInteger.valueOf(val);
} else {
Expand Down Expand Up @@ -418,10 +441,16 @@ public int readInt(String logicalName, int bitLength, WithReaderArgs... readerAr
throw new ParseException("int can only contain max 32 bits");
}
try {
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
return Integer.reverseBytes(bi.readInt(false, bitLength));
switch (byteOrder){
case LITTLE_ENDIAN:
return ByteOrderUtils.LittleEndian(bi.readInt(false, bitLength));
case BIG_ENDIAN_WORD_SWAP:
return ByteOrderUtils.BigEndianWordSwap(bi.readInt(false, bitLength));
case LITTLE_ENDIAN_WORD_SWAP:
return ByteOrderUtils.LittleEndianWordSwap(bi.readInt(false, bitLength));
default:
return ByteOrderUtils.BigEndian(bi.readInt(false, bitLength));
}
return bi.readInt(false, bitLength);
} catch (IOException e) {
throw new ParseException("Error reading signed int", e);
}
Expand All @@ -436,10 +465,16 @@ public long readLong(String logicalName, int bitLength, WithReaderArgs... reader
throw new ParseException("long can only contain max 64 bits");
}
try {
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
return Long.reverseBytes(bi.readLong(false, bitLength));
switch (byteOrder){
case LITTLE_ENDIAN:
return ByteOrderUtils.LittleEndian(bi.readLong(false, bitLength));
case BIG_ENDIAN_WORD_SWAP:
return ByteOrderUtils.BigEndianWordSwap(bi.readLong(false, bitLength));
case LITTLE_ENDIAN_WORD_SWAP:
return ByteOrderUtils.LittleEndianWordSwap(bi.readLong(false, bitLength));
default:
return ByteOrderUtils.BigEndian(bi.readLong(false, bitLength));
}
return bi.readLong(false, bitLength);
} catch (IOException e) {
throw new ParseException("Error reading signed long", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.jinahya.bit.io.BufferByteOutput;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.plc4x.java.spi.generation.io.MyDefaultBitOutput;
import org.apache.plc4x.java.spi.utils.ByteOrderUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -227,8 +228,17 @@ public void writeUnsignedInt(String logicalName, int bitLength, int value, WithW
break;
}
case "default":
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
value = Integer.reverseBytes(value) >> (32 - bitLength);
switch (byteOrder){
case LITTLE_ENDIAN:
value = ByteOrderUtils.LittleEndian(value);
break;
case BIG_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.BigEndianWordSwap(value);
break;
case LITTLE_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.LittleEndianWordSwap(value);
break;
default:
}
bo.writeInt(true, bitLength, value);
break;
Expand Down Expand Up @@ -285,8 +295,17 @@ public void writeUnsignedLong(String logicalName, int bitLength, long value, Wit
break;
}
case "default":
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
value = Long.reverseBytes(value) >> 32;
switch (byteOrder){
case LITTLE_ENDIAN:
value = ByteOrderUtils.LittleEndian(value);
break;
case BIG_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.BigEndianWordSwap(value);
break;
case LITTLE_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.LittleEndianWordSwap(value);
break;
default:
}
bo.writeLong(true, bitLength, value);
break;
Expand Down Expand Up @@ -370,8 +389,17 @@ public void writeInt(String logicalName, int bitLength, int value, WithWriterArg
throw new SerializationException("int can only contain max 32 bits");
}
try {
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
value = Integer.reverseBytes(value);
switch (byteOrder){
case LITTLE_ENDIAN:
value = ByteOrderUtils.LittleEndian(value);
break;
case BIG_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.BigEndianWordSwap(value);
break;
case LITTLE_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.LittleEndianWordSwap(value);
break;
default:
}
bo.writeInt(false, bitLength, value);
} catch (Exception e) {
Expand All @@ -388,8 +416,17 @@ public void writeLong(String logicalName, int bitLength, long value, WithWriterA
throw new SerializationException("long can only contain max 64 bits");
}
try {
if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
value = Long.reverseBytes(value);
switch (byteOrder){
case LITTLE_ENDIAN:
value = ByteOrderUtils.LittleEndian(value);
break;
case BIG_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.BigEndianWordSwap(value);
break;
case LITTLE_ENDIAN_WORD_SWAP:
value = ByteOrderUtils.LittleEndianWordSwap(value);
break;
default:
}
bo.writeLong(false, bitLength, value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.apache.plc4x.java.spi.utils;

public class ByteOrderUtils {
/**
* Byte order is 'A B C D'
* @param num int number
* @return int number in big endian format
*/
public static int BigEndian(int num){
return num;
}
/**
* Byte order is 'D C B A'
* @param num int number
* @return int number in little endian format
*/
public static int LittleEndian(int num){
return Integer.reverseBytes(num);
}
/**
* Byte order is 'B A D C'
* @param num int number
* @return int number in big endian swap format
*/
public static int BigEndianWordSwap(int num){
return (num << 16)|(num >>> 16);
}
/**
* Byte order is 'C D A B'
* @param num int number
* @return int number in little endian swap format
*/
public static int LittleEndianWordSwap(int num){
return ((num&0xff00)>>>8)|
((num<<8)&0xff00)|
((num<<8)&0xff000000)|
((num &0xff000000)>>>8);
}



/**
* Byte order is 'A B C D E F G H'
* @param num long number
* @return long number in big endian format
*/
public static long BigEndian(long num){
return num;
}

/**
* Byte order is 'H G F E D C B A'
* @param num long number
* @return long number in little endian format
*/
public static long LittleEndian(long num){
return Long.reverseBytes(num);
}

/**
* Byte order is 'B A D C F E H G'
* @param num long number
* @return long number in big endian format
*/
public static long BigEndianWordSwap(long num){

return (num & 0x00ff00ff00ff00ffL) << 8 | (num >>> 8) & 0x00ff00ff00ff00ffL;
}

/**
* Byte order is 'G H E F C D A B'
* @param num long number
* @return long number in little endian format
*/
public static long LittleEndianWordSwap(long num){

return (num & 0xffff000000000000L) >>> 48 |
(num & 0x0000ffff00000000L) >>> 16 |
(num & 0x00000000ffff0000L) << 16 |
(num & 0x000000000000ffffL) << 48;
}


}

0 comments on commit a864274

Please sign in to comment.