Reading float

tcf24m

Member
Join Date
Aug 2004
Location
Cambridge, Ontario
Posts
24
Hi,

I am using VB6 and a MicroLogix 1500. Communicating is fine but when I try to read a Float somethings not right. If I read a simple value like 10 it works but if I try to read a value like 5000 I get 4992 or if I try and read 1.2 I get 1.195313. The boss had a driver he bought from http://blftech.com/ a couple of years ago. When you do a read the values just end up in an array and I am just dumping what is in the array onto the screen.

Anybody with have some experience with this, do I have to convert what I read from to PLC into a format the computer understands, Seems weird that the values I am reading are so close to whats in the PLC.

Tom
 
Not sure what is happening on the 4992. Was that an integer or a float you where trying to read? I am not sure have many variables you are trying to read but you could multiply the floats by a 100 converting them to an integer and then divide then back by a 100 in the VB6.
I have never use bfltech before so I can offer nothing except calling them and seeing if they have VB6 examples. I have always used automated solutions as my activex. There are other ways and software to read from a plc. Search the forums, there have been a lot of posts on this topic.
 
So, I am stuck with the driver that I have.

I gave the example of 5000 just to show that it is not reading values out of float memory location properly. It won't read 5000.12, it was just an example.

What I am trying to figure out now is how the Micrologix stores floats, is is sign/exponent/matissa. I am guessing that is the problem and I need to convert what I have in my array into a single(no floats in VB).

The second attempt I was going to try and make is to copy my floats into integers in RSlogix read the integers then convert the integers into floats in VB....maybe my driver dosen't work so well with floats. I know it works fine with integers.

Tom
 
Further,

I would just multiply my float by 100 and read the integer but that is not enough resolution for me. I would use a long but the driver does not read longs, only integers, floats and bits. So I am stuck with a float(back to sign/exponent/matissa...I think.
 
I am not sure if this applies but here goes.
I assume you have to specify the number of floats that you want to read. For example if you wanted to read 5 floats you may have to do a read of 10. Also in your VB6 make sure you are converting it with whatever call you need. In my automated solutions they have calls such as GetWordFloat or GetWordLong.
It may also be that your float data you receive is in every other word (1 ,3 ,5, etc). Start off by trying to read just one float and go from there. It may help if you post a portion of your VB6 code and one of us might be able to comment.
 
Below is snippet of my code. The things you mentioned Mark I have thought about. When I read a float location the float in broken up into 2 16 bit integers. For me to display I reference every second position in the array(1,3). I only need to read two floats.
What you can see below is two attempts. My fist attempt which is commented out just assigned the my input array to .caption. My second attempt tried to take what was in my input array, move it into a byte array and then into a single. Both produce the same results.

Right now if I run that code and I have a float value of 1.2 in my PLC I get 1.195313 displayed on the screen.


"""""""Reads from PLC""""""""""

Private Sub Refresh_Float_Timer_Timer()

DoEvents 'Helps with my comms

'Make sure no other communication is happening

If RequesttoWriteAuto = False And ClearInProgress = False And refreshinprogress = False And WriteInProgress = False And refreshfloatinprogress = False And CommunicatePLC2.Control_IsBusy = False And firstpass = False

Then
'set up memory address and start communication
With CommunicatePLC2

.Memory_FileAddress = 10
.Memory_StartAddress = 0
.Memory_LengthInWords = 2
.Memory_Function = ReadWordMemory_df1
.Memory_Type = F_Float_df1
.Control_Update

End With

refreshfloatinprogress = True
Refresh_Float_Timer = False

DoEvents

End If

End Sub

"""""""After successful read, I display what is in my input array. The driver I have triggers a "Done" event after every successful read/write this code is inside my that event.

If refreshfloatinprogress = True Then

With CommunicatePLC2
CopyMemory floatbytearraay(0), .Memory_DataFloat(1), 2
CopyMemory floatbytearraay(2), .Memory_DataFloat(0), 2
CopyMemory floatVar, floatbytearraay(0), 4
'x_pos_var = .Memory_DataFloat(1)
x_pos.Caption = floatVar
'y_pos.Caption = .Memory_DataFloat(3)

End With

refreshfloatinprogress = False
Refresh_Timer_Auto.Enabled = True

End If
 
I figured a way around what ever the problem was. I am using CPW in my PLC to copy my float into 2 integers, reading the integers and then converting the integers into single in VB.

It works.

Thank you for your time.

Tom
 
Check out this thread
http://www.control.com/thread/1323333495#1323737849
and note that the errors you're seeing are close to the errors seen by Chrisman on this thread with errors in floating point via Modbus. Gerry's explanation seems totally reasonable.

That problem resulted from reading the half of the float of one value and half of the float from another value, using the inverted word order to get the reading to be "close" but not the exact value it should be.

The key is that the floating point has to be assembled and interpreted in the correct order.

I was astonished that the particular mis-combination of bytes within a float could produce errors so close to the actual values.
 

Similar Topics

Hey all, I have an AcuDC energy meter which provides data on voltage, current, and power over RS-485/Modbus RTU. It provides float values in two...
Replies
8
Views
2,050
Hi to all! Need some help! I'm reading CATERPILLAR engine data of a natural gas compressor into my MVI56E-MCM card. The card sits in a 14 slot...
Replies
7
Views
3,666
Hello I have a s7-1200 and I would like to read the tags present in this controller with my controllogix controller. The two controllers don't use...
Replies
8
Views
341
Hi all, i have 8 CJ2m plc units that show different numbers on the plc display and i am stuck on reading the info. my unit has an ip address of...
Replies
3
Views
150
Hi Everyone, I am not proficient in RSLogix 500 so I have a question regarding the evaluation of N7:0 data as an input. So as I understand in...
Replies
1
Views
130
Back
Top Bottom