Convertions

219 views
Skip to first unread message

ahf...@gmail.com

unread,
Mar 26, 2007, 1:51:43 PM3/26/07
to NModbus
Hallo!

I´m extreme newbie to C# and Modbus.

So... I´m reading the following registers with "ReadHoldingRegisters"
Method (NModbus)

%R10 and %R11 and inserting in an array:

Registers[0] (16 bits - Ushort) <- %R10
Registers[1] (16 bits - Ushort) <- %R11

I Want to convert this to a 32 bits value like Float or Long integer.
Should be interesting to convert a 32 bit value in
a two 16 bits value too.

Someone have a methods to this convertions? Should be nice this in the
driver maybe...

Best regards!

sja...@gmail.com

unread,
Mar 26, 2007, 8:12:47 PM3/26/07
to NModbus
These conversions can be done many ways... here is a simple way to
convert two UInt16s to a UInt32

UInt16 lowOrderValue;
UInt16 highOrderValue;

UInt32 convertedValue = (UInt32)(highOrderValue << 16 |
lowOrderValue);

I will consider adding some methods to the Modbus.Util namespace for
such conversions to the next version.

Scott

Sean Kellogg

unread,
Mar 26, 2007, 9:14:25 PM3/26/07
to NModbus...@googlegroups.com
ahfauth,

I have used the BitConverter class for this sort of thing before. It
is a very general class and there could be more simple ways than this.
You would probably have to convert your Int16 registers to Byte
arrays with the GetBytes method, concatenate, and then transform back
to Int32 using the ToInt32 method.

Good luck
Sean

ahf...@gmail.com

unread,
Mar 27, 2007, 11:58:48 AM3/27/07
to NModbus
Okay!

I'll try now...

Scott, nice work!

Thank you guys!

André

On 26 mar, 22:14, "Sean Kellogg" <pstin...@gmail.com> wrote:
> ahfauth,
>
> I have used the BitConverter class for this sort of thing before. It
> is a very general class and there could be more simple ways than this.
> You would probably have to convert your Int16 registers to Byte
> arrays with the GetBytes method, concatenate, and then transform back
> to Int32 using the ToInt32 method.
>
> Good luck
> Sean
>

> On 3/26/07, ahfa...@gmail.com <ahfa...@gmail.com> wrote:
>
>
>
>
>
> > Hallo!
>
> > I´m extreme newbie to C# and Modbus.
>
> > So... I´m reading the following registers with "ReadHoldingRegisters"
> > Method (NModbus)
>
> > %R10 and %R11 and inserting in an array:
>
> > Registers[0] (16 bits - Ushort) <- %R10
> > Registers[1] (16 bits - Ushort) <- %R11
>
> > I Want to convert this to a 32 bits value like Float or Long integer.
> > Should be interesting to convert a 32 bit value in
> > a two 16 bits value too.
>
> > Someone have a methods to this convertions? Should be nice this in the
> > driver maybe...
>

> > Best regards!- Ocultar texto entre aspas -
>
> - Mostrar texto entre aspas -

si

unread,
Mar 28, 2007, 5:40:43 AM3/28/07
to NModbus
This is what I use for floats, swapping the 2 words may depend on your
target device.

There is probably a much easier way of doing it, but this seems to
work OK.

Should work for Int32's with a few minor changes.

private static Single ModbusWordArrayToFloat(UInt16[] data)
{
if (data.Length != 2)
throw new ArgumentException("2 words of data required
for a float");

byte[] bData = new byte[4];

byte[] w1 = BitConverter.GetBytes(data[0]);
byte[] w2 = BitConverter.GetBytes(data[1]);

//reverse words
Array.Copy(w2, 0, bData, 0, 2);
Array.Copy(w1, 0, bData, 2, 2);

return BitConverter.ToSingle(bData, 0);
}

private static UInt16[] FloatToModbusWordArray(float value)
{
UInt16[] data = new UInt16[2];

byte[] bytes = BitConverter.GetBytes(value);

byte[] w1 = new byte[2];
byte[] w2 = new byte[2];

Array.Copy(bytes, 0, w1, 0, 2);
Array.Copy(bytes, 2, w2, 0, 2);

//reverse words
data[0] = BitConverter.ToUInt16(w2, 0);
data[1] = BitConverter.ToUInt16(w1, 0);

return data;
}

> > - Mostrar texto entre aspas -- Hide quoted text -
>
> - Show quoted text -

ahf...@gmail.com

unread,
Apr 9, 2007, 3:04:43 PM4/9/07
to NModbus
Simon, nice piece of code. This works fine!

For a Int32's, just need to invert some bytes... ;)

Like this:

private static Int32 ModbusWordArrayToLong(UInt16[] data)


{
if (data.Length != 2)
throw new ArgumentException("2 words of data required

for a Long");

byte[] bData = new byte[4];

byte[] w1 = BitConverter.GetBytes(data[0]);
byte[] w2 = BitConverter.GetBytes(data[1]);

//words reservadas
Array.Copy(w1, 0, bData, 0, 2);
Array.Copy(w2, 0, bData, 2, 2);

return BitConverter.ToInt32(bData, 0);
}


private static UInt16[] LongToModbusWordArray(Int32 value)


{
UInt16[] data = new UInt16[2];

byte[] bytes = BitConverter.GetBytes(value);

byte[] w1 = new byte[2];
byte[] w2 = new byte[2];

Array.Copy(bytes, 0, w1, 0, 2);
Array.Copy(bytes, 2, w2, 0, 2);

//reverse words
data[0] = BitConverter.ToUInt16(w1, 0);
data[1] = BitConverter.ToUInt16(w2, 0);

return data;
}

Thanks!

> > - Show quoted text -- Ocultar texto entre aspas -

Reply all
Reply to author
Forward
0 new messages