需將浮點轉換後再傳送…
例 : -12.5 轉成16進制後 將 0x00 0x00 0x48 0xC1 寫入 MODBUS VI.
資料來源 : 參閱下列連結
連結 1. 为什么浮点变量值-12.5的十进制为:0xc1480000
連結 2. @ IEEE 规範
連結 3. @ 浮點數
一個浮點數 a由 兩個數m 和e 來表示:
a = m × b^e 。 m(即尾數), e是指數。
S : represents the sign bit where 1 is negative and 0 is positive.
E : is the exponent with an offset of 127.
M : is the 24-bit mantissa (stored in 23 bits).
Zero is a special value denoted with an Exponent field of 0 and a Mantissa of 0.
Using the above format,
the floating-point number -12.5 is stored as a hexadecimal value of 0xC1480000.
In memory, this value appears as follows:
Address+0 Address+1 Address+2 Address+3
Contents 0x00 0x00 0x48 0xC1
S 代表符號位,1是負,0是正
E 偏移127的冪,二進制階碼=(EEEEEEEE)-127。
M 24位的尾數保存在23位中,只存儲23位,最高位固定爲1。
從這個例子可以得到下面的信息:
符號位是1 表示一個負數
冪是二進制10000010或十進制130,130減去127是3,就是實際的冪。
尾數是後面的二進制數10010000000000000000000
在尾數的左邊有一個省略的小數點和1,
這個1在浮點數的保存中經常省略 ,加上一個1和小數點到尾數的開頭,得到尾數值如下:
1.10010000000000000000000
接着,根據指數調整尾數.
一個負的指數向左移動小數點.
一個正的指數向右移動小數點.因爲指數是3,尾數調整如下:
1100.10000000000000000000
結果是一個二進制浮點數,小數點左邊的二進制數代表所處位置的2的冪
例如:1100 表示 (12^3)+(12^2)+(02^1)+(02^0) = 12。
小數點的右邊也代表所處位置的2的冪,只是冪是負的。
例如:.100…表示(12^(-1))+(02^(-2))+(0*2^(-2))…=0.5。
這些值的和是12.5。
因爲設置的符號位表示這數是負的,
因此十六進制值0xC1480000表示 -12.5。
浮點數 16進制轉換.vi (187.7 KB)