将32位实数转换为2x 16位字节 [英] Converting 32-Bit Real to 2x 16-Bit Bytes

查看:453
本文介绍了将32位实数转换为2x 16位字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过CAN通信(IFM)发送32位实数,但是CAN通信只接受16位值。

I'm trying to send a 32-Bit Real across a CAN communications (IFM) but the CAN comms only accepts a 16-Bit value.

如果我尝试发送的值超过255,它将重置为0,并继续该模式。因此,我需要将32位的Real值拆分为两个16位的值,然后在通讯的另一端重新组装。

If the value I'm trying to send goes above 255, it resets back to 0 and continues in that pattern. I therefore need to split the 32-Bit Real value in to two 16-Bit values and then reassemble on the other side of the comms.

我似乎不明白

任何帮助将不胜感激

推荐答案

我知道我参加聚会有点晚了,但想将此添加为解决方案。

I know I am a little late to the party but wanted to add this as a solution.

VAR
  rRealVar  :   REAL;
  awWordArray   :   ARRAY[0..1] OF WORD;
  pTemp     :   POINTER TO REAL;
  pTemp2        :   POINTER TO REAL;
END_VAR

// Get a pointer to the REAL variable
pTemp := ADR(rRealVar);

// Get a pointer to the ARRAY base
pTemp2 := ADR(awWordArray);

// Assign the value of the REAL variable into the ARRAY base
pTemp2^ := pTemp^;

(* Index 0 := Bits 15-0
   Index 1 := Bits 31-16

这类似于Felix Keil的答案,但它使用了2个指针变量和单词数组直接检索信息。

This is similar to Felix Keil's answer but it makes use of 2 pointer variables and a word array to retrieve the information directly.

这篇关于将32位实数转换为2x 16位字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆