我正在使用罗克韦尔自动化连接组件研讨会(CCW)。
我需要将REAL类型转换为通过RS-232 / ASCII发送的字节数组。如何实现这一目标?
答案 0 :(得分:1)
最简单的方法是使用COP指令。
将源元素中的二进制数据复制到目标 元件。源元素保持不变。
COP_1(Enable:=TRUE, Src:=3.14, SrcOffset:=0, Dest:=byte_array, DestOffset:=0, Length:=4, Swap:=FALSE);
缺点是在继续之前需要额外的代码来验证COP完成。
答案 1 :(得分:0)
创建一个用户定义的函数,比如说REAL_TO_754_INT(所有变量都是DINT,除了In_f32和norm_f32,它们都是REAL):
// Ref: http://beej.us/guide/bgnet/examples/pack2.c
IF In_f32 = 0.0 THEN
// get this special case out of the way
REAL_TO_754_INT := 0;
ELSE
// check sign and begin normalization
IF In_f32 < 0.0 THEN
sign := 1;
norm_f32 := -In_f32;
ELSE
sign := 0;
norm_f32 := In_f32;
END_IF;
// get the normalized form of f and track the exponent
shift := 0;
WHILE (norm_f32 >= 2.0) DO
norm_f32 := norm_f32 / 2.0;
shift := shift + 1;
END_WHILE;
WHILE (norm_f32 < 1.0) DO
norm_f32 := norm_f32 * 2.0;
shift := shift - 1;
END_WHILE;
norm_f32 := norm_f32 - 1.0;
// calculate the binary form (non-float) of the significand data
// 23 is the number of significand bits
significand := ANY_TO_DINT(norm_f32 * ANY_TO_REAL(SHL(1, 23)) + 0.5);
// get the biased exponent
// 7 is (number of exponent bits, 8)-1
expn := shift + (SHL(1,7) - 1); // shift + bias
// return the final answer
// 31 is (the total number of bits, 32)-1
REAL_TO_754_INT := OR_MASK(OR_MASK(SHL(sign, 31), SHL(expn, 23)), significand);
END_IF; // In_f32
然后重复屏蔽和移位以分成字节:
as_DINT := REAL_TO_754_INT(your_REAL);
message[1] := ANY_TO_BYTE(AND_MASK(as_DINT, 16#FF));
message[2] := ANY_TO_BYTE(AND_MASK(SHR(as_DINT,8), 16#FF));
message[3] := ANY_TO_BYTE(AND_MASK(SHR(as_DINT,16), 16#FF));
message[4] := ANY_TO_BYTE(AND_MASK(SHR(as_DINT,24), 16#FF));