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

查看:39
本文介绍了将 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 just can't seem to get my head around how to do it in structured text.

任何帮助将不胜感激

推荐答案

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

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 个 pointer 变量和一个 word array 来直接检索信息.

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天全站免登陆