Author Topic: Trouble Reading FP Values  (Read 1266 times)

Mark P.

  • Newbie
  • Posts: 6
    • View Profile
Trouble Reading FP Values
« on: January 27, 2019, 06:52:28 AM »

I am trying to Modbus Read some FP values from FX processor.
I am reading them from another FX processor.
Reading DM values works fine between the two.

Maybe the number system is off in the manual or these need something special ?

tIf looks like if I want to read FP[1]  the modbus values are held in 5000 and 5001

No matter what value I put in FP[1] greater then Zero I always get

in 5000 modbus location =16256 and  5001 =  Zero ?

Basic read code

C# = READMODBUS (13,2,5000)  'READ Value from Aux 2

D# = READMODBUS (13,2,5001)  'READ Value from Aux 2

This code works fine Reading DM values work fine with numbers like 1000, 1001 etc.
I have tried server variables to put the data in, no noticeable change

Am I looking in the wrong place for FP values ?


  • Expert Member
  • Posts: 464
  • Old PLC Coder
    • View Profile
Re:Trouble Reading FP Values
« Reply #1 on: January 27, 2019, 05:13:20 PM »

Your Modbus register address of 5000 may be correct.  If you are aceessing FP[1] via the Wientek HMI the correct answer will be 5001.

You have a 50:50 chance of getting them correct.

It depends on how the READMODBUS deals with the register address.  I don't use the READMODBUS and WRITEMODBUS functions/statements as they are too limiting for my usage, so I can't answer this question with 100% certainty.  But, I can give you enough info so you can figure it out.

OK, lets get to the problems with your code:

C# = READMODBUS (13,2,5000)  'READ Value from Aux 2

D# = READMODBUS (13,2,5001)  'READ Value from Aux 2

Problem #1 is that ReadModbus() returns a 16-bit integer and not a floating point value. so you can't assign it to a floating point variable such as C# or D# and expect to make much sense of it

Problem #2 is that you did save the 16-bit values returned by the calls to ReadMobus() so that you could build a 32-bit value that represents the correct bit pattern of the target FP[1] value.

Problem #3 is that you need to "cast" this 32-bit integer value so that you can store it a 32-bit float.

This is closer to what you need to do get read a 32-bit float:

' Read 2 sequential registers from target PLC and
' save as integers

c = READMODBUS (13,2,5000)     ' most signficant word
d = READMODBUS (13,2,5001)     ' least signficant word

' move msw pattern read from Modbus into msw of the variable "e"
e = c
e = e * &h00010000

' now add in the lsw bit pattern read from Modbus
e = e + (d & &h0000ffff)

' assign the 32-bit signed integer, "e", bit by bit to the C#.
' bits2float() acts as a cast. If bits2float were not used then
' TBASIC will attempt to create a floating point representation of the
' value of "e" and this is not what you want!
C# = bits2float(e)

OK. I am going to give you a way to figure out if 5000,5001 are the right register addresses:

'InitFP - CF to intialize FP[1..32] with known values
' The pattern was picked to be easy to "see".
' FP[1] -> &H01020304
' FP[2] -> &H05060708
' FP[3] -> &H090A0B0C
'  .
'  .
'  .
' FP[32] -> &H7D7E7F80
n = 1      ' initial byte value pattern
for i = 1 to 32
  a = n              ' msb of 32-bit pattern
  a = a * 256     ' shift left 8 bits
  a = a + n + 1
  a = a * 256     ' shift left 8 bits
  a = a + n + 2
  a = a * 256     ' shift left 8 bits
  a = a + n + 3   ' lsb of 32-bit pattern

  FP = bits2float(a)    ' convert to float and store

  n = n + 4       ' next byte value pattern

Initialize the target FP[] array using the test pattern generator.  Now run your ReadModbus test code under the online simulator and look at the values that you are getting back from Modbus for the MSW and LSW and you will be able to figure out where they are coming from. You will be able to adjust the Modbus register values if needed.

In the view window of the simulator, when looking at float variables you can either view them as IEEE (which is the hexadecimal 32-bit value) or "DEC" (which is the floating point value represented as a base 10 decimal value like "-3.141597"). Pick IEEE and look at the hex values.

Best regards,

Gary D*ckinson
« Last Edit: January 28, 2019, 10:58:46 AM by garysdickinson »