Loading... ## 开发原因: 在接触PLC项目的时候,有许多项目都需要通过modbus对传感器数据进行解析,而浮点数又是许多传感器数据的标配,这时就需要对16位或者32位16进制浮点数进行转换解析。 为了方便自己以后的项目开发,所以封装了一个16进制浮点数转10进制小数的FB。 由于这些数据又存在高位在前和高位在后的情况,所以根据不同情况增加了一个输入,用于指示高位在前或者后面。 ## 变量定义如下: <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-cc29041170415cd6b72fc1f1a78de35693" aria-expanded="true"><div class="accordion-toggle"><span>变量定义如下 点击展开</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-cc29041170415cd6b72fc1f1a78de35693" class="collapse collapse-content"><p></p> ```iecst FUNCTION_BLOCK FB_BinaryFloat VAR_INPUT xEnb:BOOL; (*使能信号*) HxEnb: BOOL; (*高位在前标志位*) arTransmit:ARRAY[0..49]OF BYTE; (*需要转化的数组*) END_VAR VAR_OUTPUT xTransmit:BOOL; (*转换开始标志位*) rTransformation:REAL; (*转换结果*) END_VAR VAR arTransmit_MID:ARRAY[1..4] OF BYTE; (*浮点数二进制转换中间变量*) diSign:DINT; (*符号位*) diExponent:DINT; (*指数位*) diMantissa:DINT; (*尾数位*) END_VAR ``` <p></p></div></div></div> <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-a516739fde72ceb6f3b7856d3d2e511c49" aria-expanded="true"><div class="accordion-toggle"><span>运行内容如下 点击展开</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-a516739fde72ceb6f3b7856d3d2e511c49" class="collapse collapse-content"><p></p> ```iecst IF xEnb AND HxEnb THEN xTransmit:=TRUE; arTransmit_MID[1]:=arTransmit[5]; arTransmit_MID[2]:=arTransmit[6]; arTransmit_MID[3]:=arTransmit[3]; arTransmit_MID[4]:=arTransmit[4]; ELSIF xEnb AND NOT HxEnb THEN xTransmit:=TRUE; arTransmit_MID[1]:=arTransmit[3]; arTransmit_MID[2]:=arTransmit[4]; arTransmit_MID[3]:=arTransmit[5]; arTransmit_MID[4]:=arTransmit[6]; ELSE xTransmit:=FALSE; END_IF IF NOT xEnb THEN SysMemSet(ADR( rTransformation),0,SIZEOF( rTransformation)); SysMemSet(ADR( arTransmit_MID),0,SIZEOF( arTransmit_MID)); RETURN; END_IF diSign:=BOOL_TO_DINT(arTransmit_MID[3].7); diExponent:=(arTransmit_MID[3]- BOOL_TO_DINT(arTransmit_MID[3].7)*128)*2+BOOL_TO_DINT( arTransmit_MID[4].7); diMantissa:=( arTransmit_MID[4]-BOOL_TO_DINT( arTransmit_MID[4].7)*128)*256*256+ arTransmit_MID[1]*256+ arTransmit_MID[2]; IF diExponent<>0 AND diExponent<>255 THEN rTransformation:= (EXPT(-1, diSign))*(EXPT(2, diExponent-127))*(1+EXPT(2,-1)* BOOL_TO_DINT(diMantissa.22)+EXPT(2,-2)* BOOL_TO_DINT(diMantissa.21) +EXPT(2,-3)* BOOL_TO_DINT(diMantissa.20)+EXPT(2,-4)* BOOL_TO_DINT(diMantissa.19)+EXPT(2,-5)* BOOL_TO_DINT(diMantissa.18) +EXPT(2,-6)* BOOL_TO_DINT(diMantissa.17)+EXPT(2,-7)* BOOL_TO_DINT(diMantissa.16)+EXPT(2,-8)* BOOL_TO_DINT(diMantissa.15) +EXPT(2,-9)* BOOL_TO_DINT(diMantissa.14)+EXPT(2,-10)* BOOL_TO_DINT(diMantissa.13)+EXPT(2,-11)* BOOL_TO_DINT(diMantissa.12) +EXPT(2,-12)* BOOL_TO_DINT(diMantissa.11)+EXPT(2,-13)* BOOL_TO_DINT(diMantissa.10)+EXPT(2,-14)* BOOL_TO_DINT(diMantissa.9) +EXPT(2,-15)* BOOL_TO_DINT(diMantissa.8)+EXPT(2,-16)* BOOL_TO_DINT(diMantissa.7)+EXPT(2,-17)* BOOL_TO_DINT(diMantissa.6) +EXPT(2,-18)* BOOL_TO_DINT(diMantissa.5)+EXPT(2,-19)* BOOL_TO_DINT(diMantissa.4)+EXPT(2,-20)* BOOL_TO_DINT(diMantissa.3) +EXPT(2,-21)* BOOL_TO_DINT(diMantissa.2)+EXPT(2,-22)* BOOL_TO_DINT(diMantissa.1)+EXPT(2,-23)* BOOL_TO_DINT(diMantissa.0) ); END_IF ``` <p></p></div></div></div> <div class="tip inlineBlock success simple"> **如果我的内容对你有帮助,帮我点个赞吧!!** </div> 最后修改:2023 年 06 月 18 日 © 允许规范转载 赞 10 如果觉得我的文章对你有用,请随意赞赏
1 条评论
MD博主太棒了