How to convert a Hex-value to float in modbus-protocol using C# - modbus

i am using the modbus-protocoll to retrieve an analog-value from a module.
On the webpanel i can see that the value is 09FD in Hex and 0.780 in float.
The function returns only the 09FD into C# and must be manuall converted to the float-value.
For this there is a convert-function in the modbus-dll:
public static float GetSingle(
ushort highOrderValue,
ushort lowOrderValue
)
But what part of "09FD" must be set to the two ushorts?
I dont figure out how to pass it to retrieve the double-value.
Thanks for help

The values returned in the register are integers, scaled in some way to permit representation of the fractional floating point values. You will need to review the documentation of your module to determine how to scale (and possibly offset) the result to convert it into actual engineering units.
If you don't have documentation, then you could retrieve multiple values from the web panel, plot them and fit a line through them to estimate the parameters used to scale the output value.
The GetSingle() function you reference applies if the values are represented in true floating point form (and passed in two adjacent integers) by the device. Your example with only a single integer suggests this is not the case.

Related

PureScript - Convert a number to an integer?

PureScript contains a method in the Integer library fromNumber.
Here is an example of how it might be used:
myInteger = fromMaybe 0 (fromNumber myNumber)
However the docs provide this puzzling explanation:
Creates an Int from a Number value. The number must already be an integer and fall within the valid range of values for the Int type otherwise Nothing is returned.
Basically, your number must already be an Integer to convert it to an integer.
Assuming your number is not already an integer, a reasonable use case, how would you convert it to a number?
If it's not already an integer, there is no one true way of converting it to an integer. You could round to the nearest integer, round up, round down, do banker's rounding, or some sort of crazy conversion scheme of your own.
The Data.Int module offers several functions for different conversion strategies, such as floor, ceil, and round.

Getting values from energy meter to Schneider PLC

I am trying to read the values from an energy meter, and convert them to REAL (32bit float).
In this case I am reading phase 1 voltage.
Each value is read across two registers.
So I have received to WORDS of values 17268 (MSW) and 2456 (LSW) converted them into a DWORD, and then to a REAL value after multiplying by 0.1, but I am not getting the answer I'm expecting.
I should be getting 245.0375 volts.
However I am getting 1.13E+08
Please see snip of structured text with live values.
snip
The problem is that DWORD_TO_REAL is trying to do a type conversion; that is, make the value of a DWORD match the format of a REAL. In your case, the contents of MSW and LSW are simply a IEEE754 value split in half and just need to be forced into the corresponding bits of a REAL variable. With TwinCAT (Beckhoff) I would do a direct memory copy:
MEMCPY(ADR(realValue)+2, ADR(MSW), 2);
MEMCPY(ADR(realValue), ADR(LSW), 2);
I would assume Schneider has a similar command.

convert from double to int in anylogic

I am trying to convert a double value to int and use it in the number of agents per arrival. I have a distribution but want it to round to the nearest integer. I looked up the anylogic math functions but the only one that makes sense is rint but that still returns a double.
you can do this:
(int) rint(yourVariable)
Another way is to use (int)floor(myDouble)) and (int)ceil(myDouble)) .
With these, you can decide if you want to round up or down (if the default rounding rules aren't to your liking)

How to convert uint8 to int in Matlab?

I have converted the image from RGB to grayscale and have the image-matrix in uint8. I'm trying to make some image processing with convolution which means that I want to use the values as int when I'm summing up everything. So, how can I convert from uint8 to int in Matlab?
Matlab support multiple integer formats. The main difference is the required space in memory and if the sign (+ or -) is used.
For example, uint8 means that the integer is unsigned and that it uses 8 bit to store the value. The number of used bits determines the maximal value. The uint8 can store a number between 0 and 2^8-1.
You can find a whole list of all supported integer here
If you would like to convert your uint8 into another format you can just write the desired format as a function and pass the value as parameter:
I2 = uint16(I);
In matlab you can use double type.
for example type cast with:
I2=double(I);
It is easy to understand that you can use
int8(I)
int16(I)
too.

Understanding the datatype Double

I am trying to write a program in C to get the percent of even numbers in an array. I am thinking of writing it using int datatype. But some one mentioned to me using double will be easier. I don't understand that. Can anyone guide me with it?
What does double datatype return?
Can the return statement be given as return (double)? What will that give?
Can double convert a real number to a percent? Eg: 0.5 to 50.0
The int datatype is, as the name would suggest, integers (or whole numbers). So you cannot represent a decimal like 0.5. A double is decimal number. So you can hold numbers like 0.5. Common practice is to store your percentage as a simple decimal number like 0.5 (using the double type). Then when you need to display nicely as 50.0%, just multiply by 100 before displaying.
Here's a useful link on C's basic datatypes: http://www.tutorialspoint.com/ansi_c/c_basic_datatypes.htm