C# Modbus协议中读取浮点数的操作方法
程序员文章站
2022-04-14 20:21:03
输入参数P1,P2代表PLC中浮点数储存的两个寄存器获取的数据 ......
输入参数p1,p2代表plc中浮点数储存的两个寄存器获取的数据
public static float getfloat(ushort p1, ushort p2) { int intsign, intsignrest, intexponent, intexponentrest; float faresult, fadigit; intsign = p1 / 32768; intsignrest = p1 % 32768; intexponent = intsignrest / 128; intexponentrest = intsignrest % 128; fadigit = (float)(intexponentrest * 65536 + p2) / 8388608; faresult = (float)math.pow(-1, intsign) * (float)math.pow(2, intexponent - 127) * (fadigit + 1); return faresult; }