Linuxdept (Customer) asked a question.September 21, 2021 at 8:30 PMP2000 Modbus Values showing up crazyso my vb.net form is showing the modbus values in a abnormal way so my tag is like so any ideas ? Expand PostProductivity
Adisharr (Customer)Edited September 21, 2021 at 10:52 PMLooks like you're displaying a 32 bit float as two 16 bit integers. hex value for a 34.2 float is 0x4208cccd 0x4208 = 169040xcccd = 52,429 (52,429-65536 = -13,107) https://www.vbforums.com/showthread.php?259885-Convert-Integer-to-Float Expand Post
Garry (Customer)5 years agohttps://www.vbforums.com/showthread.php?535551-Converting-Hex-to-floating-point-and-backThe above post may also help you out.Regards,GarryExpand Post
Linuxdept (Customer)Edited September 22, 2021 at 12:45 PMi am using a Float 32 as my tag. i need something with decimals but in raw Modbus its coming out as the Following Expand Post
kewakl (Customer)Edited September 22, 2021 at 2:39 PMAs @Adisharr (Customer) posted above, you are displaying two 16-bit INTs, and need to display a 32-bit FLOATFloats 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 availableExpand Post
kewakl (Customer)Edited September 22, 2021 at 2:58 PMTo expand on @Adisharr (Customer) post above and correlate to the IEEE754 calculator site:Bold entries = As shown in the IEEE754 calculator 16904 = 0x420852429 = 0xCCCD16904*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.htmlExpand Post
kewakl (Customer)5 years agoStill no FORUM POST PREVIEW, so just take all the announcements that this idiot (me) just edited a post! Expand Post
Linuxdept (Customer)5 years agoWhat is just happing here..what am i doing wrong here. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' First way to convert string to integer ' Convert a string to an integer Dim text As String = AnalogValueDisplay1.Value Dim text1 As String = AnalogValueDisplay9.Value Dim stringToInteger As Integer = Integer.Parse(text) Dim ifloat As Single = Convert.ToSingle(text) Dim idouble As Double = Convert.ToDouble(text1) ' Dim stringToInteger As Integer = Convert.ToInt32(text) ' Label16.Text = stringToInteger ' Convert a string to a decimal Dim stringToDecimal As Decimal = Convert.ToDecimal(text) Label16.Text = ifloat Label20.Text = idouble End SubExpand Post
kewakl (Customer)Edited September 22, 2021 at 3:49 PMOne 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 bitsint low = ... ' the low 16 bitsint combined = (high << 16) | low;float num = Float.intBitsToFloat(combined) This may not be anywhere near proper VB code, more like pseudocode. edit: clarityExpand Post
Garry (Customer)5 years agohttps://social.msdn.microsoft.com/Forums/vstudio/en-US/dd856ea0-92d6-4ec8-bfb0-6e35fe7347af/how-can-i-convert-2-16-bit-shorts-into-a-32-bit-single-and-back-again?forum=vbgeneralHere is some VB code that will do this. Regards,GarryExpand Post
kewakl (Customer)5 years agoHopefully that can help OP get past this.It seems my VB practice is a bit dusty. I seem to remember using 'bitconverter.getbytes' or something close to this years ago to convert ints to bitfields ??? VB has changed a bit since I last used it for a project.Expand Post
Looks like you're displaying a 32 bit float as two 16 bit integers.
hex value for a 34.2 float is 0x4208cccd
0x4208 = 16904
0xcccd = 52,429 (52,429-65536 = -13,107)
https://www.vbforums.com/showthread.php?259885-Convert-Integer-to-Float
https://www.vbforums.com/showthread.php?535551-Converting-Hex-to-floating-point-and-back
The above post may also help you out.
Regards,
Garry
i am using a Float 32 as my tag. i need something with decimals but in raw Modbus its coming out as the Following

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.
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
Still no FORUM POST PREVIEW, so just take all the announcements that this idiot (me) just edited a post!
What is just happing here..what am i doing wrong here.
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
https://social.msdn.microsoft.com/Forums/vstudio/en-US/dd856ea0-92d6-4ec8-bfb0-6e35fe7347af/how-can-i-convert-2-16-bit-shorts-into-a-32-bit-single-and-back-again?forum=vbgeneral
Here is some VB code that will do this.
Regards,
Garry
Hopefully that can help OP get past this.
It seems my VB practice is a bit dusty.
I seem to remember using 'bitconverter.getbytes' or something close to this years ago to convert ints to bitfields ???
VB has changed a bit since I last used it for a project.