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!
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
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
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 -
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 -
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 -