0

I am creating a java Desktop Application. I want to write the data into the register of a device. As per my project document, size of the register is 16-bit long. I am using EasyModbusJava jar to write data into the register. Till now I have written some integer data on the device's register. Now I want to write the ascii of 32 characters at 16 consecutive register(2 Character per Register). But issue is that the available methods for writing on the registers takes int as an argument. If am passing the short(int) array of ascii values then it needs to be typecast which means that it will no longer be acquiring the size of the short data type.

There are two methods are available to write into the registers address.

For Writing at Single Register

public void WriteMultipleRegisters(int startingAddress, int[] values){...}

For Writing at Multiple Register

public void WriteMultipleRegisters(int startingAddress, int[] values){...}

Suggest some way to resolve my issue.

Below is the link of the jar file documentation which I am using in my project. Docs of Jar File.

1 Answers1

0

I think the easiest thing to do is use a ByteBuffer to manage this byte manipulation. Something like,

char[] arr = "Hello, World".toCharArray();
ByteBuffer bb = ByteBuffer.allocate(arr.length);
for (char ch : arr) {
    bb.put((byte) ch);
}
bb.rewind();
// You may need a call to ByteBuffer.order(ByteOrder) here.
for (int i = 0; i < arr.length / 2; i++) {
    int v = bb.getShort(); // Reads two bytes and converts to 16-bit short integer
    System.out.println(v);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249