rossmann-engineering/EasyModbusTCP.NET

What is the best way to get string from register??

dj910624 opened this issue · 4 comments

When I read register from Modbus server, I always convert each register element.
Source code is follow:

data = modbusclient.ReadInputRegisters(12800, 123);
returnSize = data[2];
for (int u = 0; u < data.Length; u++)
{
       data[u] = (ushort)IPAddress.NetworkToHostOrder(Convert.ToInt16(data[u]));
}

if (returnSize == 244)
       string read_value = ModbusClient.ConvertRegistersToString(data, 3, returnSize - 4);
else
       string read_value = ModbusClient.ConvertRegistersToString(data, 3, returnSize);

It is really best way to convert register array to string ??
Thanks.

Sorry, but I don't follow you here. You are reading 123 registers. Why returnsize can be 244?

Oh, 244 is the length of packet contents, When I request to 12800 address to server, they always return contents length with contents.
For example, If I request 12800 address and 123 quantity, returned packet bytes length is 246 (123 * 2) and real contents length is 244(246 - header: 2 bytes). This number is always included in return_data[2].

There are two static Methods for that purpose:

public static string ConvertRegistersToString(int[] registers, int offset, int stringLength)
public static int[] ConvertStringToRegisters(string stringToConvert)