fangzhchen / nmodbus

Automatically exported from code.google.com/p/nmodbus
0 stars 0 forks source link

nmodbus reading 32bit Holding registers #17

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Can nmodbus read a 32bit integer or float from a holding register? my 
microcontroller supports it on all registers and it would be to expensive 
a task to change the code on the microcontroller to write longs to 
separate registers allowing me to read an upper and lower ushort and 
combine them to be a 32bit variable.

is there a way to read 32bit direct from a holding register?

Regards Clinton Storer.

Original issue reported on code.google.com by clinto...@iprimus.com.au on 19 Jul 2007 at 6:05

GoogleCodeExporter commented 8 years ago
NModbus only supports reading 16 bit holding registers as there is no Modbus 
function
code for reading 32 bit holding registers. If your microcontroller supports 32 
bit
values, simply read it in two 16 bit chunks, then perform the a conversion to 
your 32
bit integer or float.

There are already conversion helper methods in Modbus.Util.ModbusUtil - 
GetSingle
And ToUInt32 which take two 16 bit values and return a float or integer 
respectively.
For more conversion methods check out Mono’s DataConvert class which replaces 
.NET’s
System.BitConvert  http://www.mono-project.com/Mono_DataConvert

Does this answer your question?

Scott

Original comment by sja...@gmail.com on 19 Jul 2007 at 3:18

GoogleCodeExporter commented 8 years ago
Hello Scott

How do I address the second 16 bit chunk? as there is 32bits in holding 
register 
7101 and 32 bits in 7102. so if I read holding register 7101 i get the first 16 
bits 
but I don’t know how to address the other 16 bit? is it possible?

my basic reading code is below that reads holding register 7101 into an ushort[]
can you show me how to read the second 16bit chunk?

using (TcpClient client = new TcpClient(sIPAddress, 502))
{
   Modbus.Device.ModbusTcpMaster master = Modbus.Device.ModbusTcpMaster.CreateTcp
(client);
   string sMenu = "71";
   string sStartParameter = "01";
   string sStartAddress = sMenu + sStartParameter;
   int iStartAddress = (Convert.ToInt32(sStartAddress) - 1);
   ushort usStartAddress = (ushort)iStartAddress;
   int iNumParameters = 1;
   ushort numInputs = (ushort)iNumParameters;

   ushort[] inputs = master.ReadHoldingRegisters(usStartAddress, numInputs);
}

Regards Clinton Storer.

Original comment by clinto...@iprimus.com.au on 19 Jul 2007 at 11:30

GoogleCodeExporter commented 8 years ago

Original comment by sja...@gmail.com on 2 Dec 2007 at 1:32