Author Topic: Read two consecutive 16 bit register - Modbus  (Read 3667 times)

Flemmingaa

  • Newbie
  • *
  • Posts: 7
  • Fl-aa
    • View Profile
Read two consecutive 16 bit register - Modbus
« on: February 04, 2016, 09:10:21 AM »
Hi
From a device I like to read a parameter. Each parameter is held in two consecutive 16 bit register.

The data used, is in 32 bit IEEE 754 floating point format.

If I send the Requests:
01 04 00 00 00 02 71 F8
Adress:1
Function code:04
Starting address:0000
Quantity of registers:0002

I get:
01 04 04 43 65 7F 88 ED 49
Values: 43 65 7F 88
1. register "43 65" is the same as 17253 (Dec) and 0100001101100101 (bin)
2. register "7F 88" is the same as 32648 (Dec) and 0111111110001000 (bin)
and together
"43 65 7F 88" is 01000011011001010111111110001000

If I run this through "http://www.h-schmidt.net/FloatConverter/IEEE754.html"
I get 229.49817
It super and correct

Is there a way to read this through "ModbusRTUCom"
I think I've tried many things, but get some strange numbers
Greetings

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Read two consecutive 16 bit register - Modbus
« Reply #1 on: February 04, 2016, 09:20:07 AM »
Normally you would use an address like F40001 to read a floating point, but function code 4 is for the 3xxxx address range which AdvancedHMI does not support the F3xxxx in the current version.

You would need to read 2 consectutive 16 bit registers, then process them into a 32 bit floating point. Something like this:

Dim bytes(3) as byte

bytes(0)=cbyte(cint(value(0)) and 65280)
bytes(1)=cbyte(cint(value(0)) and 255)
bytes(2)=cbyte(cint(value(1)) and 65280)
bytes(3)=cbyte(cint(value(1)) and 255)

Flemmingaa

  • Newbie
  • *
  • Posts: 7
  • Fl-aa
    • View Profile
Re: Read two consecutive 16 bit register - Modbus
« Reply #2 on: February 04, 2016, 11:14:08 AM »
I Archie

Dim value() As String = ModbusRTUCom.Read("3001", 2)
gives:
value(0) = 17253 (Dec) (It super and correct) that can I convert to bin
value(0) = -14240 (Dec) (a negative number) - how do I "read" that ?  what means "a negative number"

Can I get the "value" directly in hex ?
Greetings

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Read two consecutive 16 bit register - Modbus
« Reply #3 on: February 04, 2016, 01:03:14 PM »
        Dim values() As String = ModbusTCPCom1.Read("30001", 2)

        Dim bytes(3) As Byte
        BitConverter.GetBytes(Convert.ToInt16(values(0))).CopyTo(bytes, 0)
        BitConverter.GetBytes(Convert.ToInt16(values(1))).CopyTo(bytes, 2)

        Dim FloatValue As Single = BitConverter.ToSingle(bytes, 0)