How to combine three consecutive Modbus registers to get an integer value?

Ram

I have to read a device's value using Modbus protocol. It has a value that is 6 bytes long represented by three consecutive Holding Registers. This is graphical representation of both signed and unsigned types.

enter image description here

enter image description here

I can read three registers using a Modbus library in Java. Each register is read as a short value which is 2 bytes. Now I need to know how to combine the three values to form an integer value in both signed and unsigned formats?

Marker

Please keep in mind, I haven't written java in a LONG time so my java is VERY rusty. Because there are no unsigned values in Java it is a little harder. Try something like this:

class ModbusConvert
{
    public static long toUnsigned(int register0, int register1, int register2)
    {
        //NOTE: Assumes long is 64-bit, int is 32-bit

        // Convert each register to a long in the appropriate bit position and OR them together
        long v2 = (((long)register2) & 0x0FFFF);
        long v1 = (((long)register1) & 0x0FFFF) << 16;
        long v0 = (((long)register0) & 0x0FFFF) << 32;
        return v0 | v1 | v2;
    }

    public static long toSigned(int register0, int register1, int register2)
    {
        //NOTE: Assumes long is 64-bit, int is 32-bit

        // Convert each register to a long in the appropriate bit position and OR them together
        if ((register0 & 0x8000) != 0) register0 |= 0xFFFF0000; // Sign extend

        long v2 = (((long)register2) & 0x0FFFF);
        long v1 = (((long)register1) & 0x0FFFF) << 16;
        long v0 = ((long)register0) << 32;
        return v0 | v1 | v2;
    }

    public static void main(String args[])
    {
        // Example Register Values (value = 0xFEDC76543210)
        int register0 = 0xFEDC;
        int register1 = 0x7654;
        int register2 = 0x3210;

        long unsigned = toUnsigned(register0, register1, register2);
        long signed = toSigned(register0, register1, register2);

        System.out.println(String.format("UNSIGNED: 0x%016X", unsigned));
        System.out.println(String.format("SIGNED  : 0x%016X", signed));
    }
}

The output is:
UNSIGNED: 0x0000FEDC76543210
SIGNED  : 0xFFFFFEDC76543210

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How get integer value from a enum in Rails?

分類Dev

How to get an integer value from Entry in Tkinter?

分類Dev

How to get the Integer value of a column in .withColumn function? [Spark - Scala]

分類Dev

How can I get textbox value into array and convert into integer in java

分類Dev

How to get RecyclerView's child from integer value?

分類Dev

Polymer How to get integer value of paper-dropdown menu

分類Dev

Get the Minimum value from Consecutive dataframe values

分類Dev

How can I combine three tables(MySQL)?

分類Dev

How can I combine consecutive keys of an array depending on their values?

分類Dev

How to optimize three consecutive str.replace() calls?

分類Dev

How to get all consecutive subsets of a vector?

分類Dev

two same rows but one column with distinct value, how to combine them and get rid of another line

分類Dev

How to check if value is logically integer?

分類Dev

Combine multiple consecutive words in python

分類Dev

How to combine three Signals using reactive cocoa in swift?

分類Dev

How do I combine these three php feeds into one single feed

分類Dev

PHP - How to combine three or more classes that each depend on each other?

分類Dev

Ruby get maximum integer size value

分類Dev

Stanford Parser: Get Integer value for CARD?

分類Dev

How to add a column with the median value of 5 consecutive rows in r?

分類Dev

How to find the number of bytes of an integer value stored in a integer variable

分類Dev

How can I illustatrate three parallel, non-consecutive, but dependent processes with BPMN?

分類Dev

How to get the count of repeated key values of array object of consecutive elements?

分類Dev

How do I get space between consecutive RelativeLayouts inside a LinearLayout?

分類Dev

Check if three integer are unique

分類Dev

How to declare a variable based on value of another Integer?

分類Dev

R: Combine consecutive rows based on the previous row

分類Dev

How to get integer LocationId into ViewModel from ListItem?

分類Dev

Do VRAM/Registers get saved when hibernating?

Related 関連記事

  1. 1

    How get integer value from a enum in Rails?

  2. 2

    How to get an integer value from Entry in Tkinter?

  3. 3

    How to get the Integer value of a column in .withColumn function? [Spark - Scala]

  4. 4

    How can I get textbox value into array and convert into integer in java

  5. 5

    How to get RecyclerView's child from integer value?

  6. 6

    Polymer How to get integer value of paper-dropdown menu

  7. 7

    Get the Minimum value from Consecutive dataframe values

  8. 8

    How can I combine three tables(MySQL)?

  9. 9

    How can I combine consecutive keys of an array depending on their values?

  10. 10

    How to optimize three consecutive str.replace() calls?

  11. 11

    How to get all consecutive subsets of a vector?

  12. 12

    two same rows but one column with distinct value, how to combine them and get rid of another line

  13. 13

    How to check if value is logically integer?

  14. 14

    Combine multiple consecutive words in python

  15. 15

    How to combine three Signals using reactive cocoa in swift?

  16. 16

    How do I combine these three php feeds into one single feed

  17. 17

    PHP - How to combine three or more classes that each depend on each other?

  18. 18

    Ruby get maximum integer size value

  19. 19

    Stanford Parser: Get Integer value for CARD?

  20. 20

    How to add a column with the median value of 5 consecutive rows in r?

  21. 21

    How to find the number of bytes of an integer value stored in a integer variable

  22. 22

    How can I illustatrate three parallel, non-consecutive, but dependent processes with BPMN?

  23. 23

    How to get the count of repeated key values of array object of consecutive elements?

  24. 24

    How do I get space between consecutive RelativeLayouts inside a LinearLayout?

  25. 25

    Check if three integer are unique

  26. 26

    How to declare a variable based on value of another Integer?

  27. 27

    R: Combine consecutive rows based on the previous row

  28. 28

    How to get integer LocationId into ViewModel from ListItem?

  29. 29

    Do VRAM/Registers get saved when hibernating?

ホットタグ

アーカイブ