• Linuxdept (Customer)

    i am using a Float 32 as my tag. i need something with decimals but in raw Modbus its coming out as the Following value1Modbus6

  • kewakl (Customer)

    As @Adisharr (Customer)​ posted above, you are displaying two 16-bit INTs, and need to display a 32-bit FLOAT

    Floats are bit-packed as shown in the image below. In VB.NET does System.Convert.ToSingle() do what you need. (The example link is for .Net 5.x)

    You should pack the two 16-bit INTs into a 32-bit INT, then make the System.Convert call.

     

    The bit patterns look correct as compared to this calculator results.

    Image is not available

    Expand Post
  • kewakl (Customer)

    To expand on @Adisharr (Customer)​ post above and correlate to the IEEE754 calculator site:

    Bold entries = As shown in the IEEE754 calculator

     

    16904 = 0x4208

    52429 = 0xCCCD

    16904*65535 = 1,107,820,544 = 0x4208 0000

     

    1,107,820,544+52429 = 1,107,872,973 = 0x4208 CCCD = 0b0100 0010 0000 1000 1100 1100 1100 1101

     

    0b0100 0010 0000 1000 1100 1100 1100 1101 is the IEE754 bit pattern for 34.200000762939453125, which is the nearest value that can be stored in a FLOAT.

     

    This bit pattern (32-bit value) would then be sent to System.Convert.ToSingle()

     

    [Edit Add link to IEEE754 calculator] https://www.h-schmidt.net/FloatConverter/IEEE754.html

    Expand Post
  • kewakl (Customer)

    Still no FORUM POST PREVIEW, so just take all the announcements that this idiot (me) just edited a post!

     

  • Linuxdept (Customer)

    What is just happing here..what am i doing wrong here.

    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2. ' First way to convert string to integer
    3. ' Convert a string to an integer
    4. Dim text As String = AnalogValueDisplay1.Value
    5. Dim text1 As String = AnalogValueDisplay9.Value
    6. Dim stringToInteger As Integer = Integer.Parse(text)
    7. Dim ifloat As Single = Convert.ToSingle(text)
    8. Dim idouble As Double = Convert.ToDouble(text1)
    9. ' Dim stringToInteger As Integer = Convert.ToInt32(text)
    10. ' Label16.Text = stringToInteger
    11. ' Convert a string to a decimal
    12. Dim stringToDecimal As Decimal = Convert.ToDecimal(text)
    13. Label16.Text = ifloat
    14. Label20.Text = idouble
    15.  
    16. End Sub

    convert

    Expand Post
  • kewakl (Customer)

    One thing that may be interfering is that negative value in 400401.

    I do not know if VB.Net has a native way to pack the two 16-bit INTs into a 32-bit INT.

     

    The math that I showed earlier was meant to describe the concept. The math does not handle correctly a negative LSW.

     

    I do not have VS/VB installed on this laptop so I cannot test the next idea.

    Can VB.NET do bit shift [LIKE] this?

    int high = ... ' the high 16 bits

    int low = ... ' the low 16 bits

    int combined = (high << 16) | low;

    float num = Float.intBitsToFloat(combined)

     

    This may not be anywhere near proper VB code, more like pseudocode.

     

    edit: clarity

    Expand Post