[英]Can we swap the endianness of a data buffer, which is a fixed sized array, before mapping it to its eventual data structure?
Scenario: A remote machine (big endian) sends a message to a local machine (little endian) over RS422. 场景:远程机器(大端)通过RS422向本地机器(小端)发送消息。
The local machine gets the message as a buffer, ie dataBuffer
which is an array of 4 16-bit ints. 本地机器将消息作为缓冲区获取,即
dataBuffer
,它是一个包含4个16位整数的数组。 This buffer data eventually be mapped to a MainType
data somewhere in the program but this is not our concern. 此缓冲区数据最终会映射到程序中的某个
MainType
数据,但这不是我们关注的问题。 We need a function that swaps the bytes (change the endianness) using the swapData()
method. 我们需要一个使用
swapData()
方法交换字节(更改字节顺序)的swapData()
。
Question: Given the fact that MainType
has exactly 4
data members each 16 bits
AND dataBuffer
is array of size 4 and each data is 16 bits, can we just swap the data in the buffer WITHOUT mapping it to MainType
data structure (as below)? 问题:鉴于
MainType
正好有4
数据成员,每个16 bits
AND dataBuffer
是大小为4的数组,每个数据是16位,我们可以只交换缓冲区中的数据而不将其映射到MainType
数据结构(如下所示)?
Constraints: 约束:
dataBuffer
needs to be global in the program, dataBuffer
需要在程序中是全局的, swapData()
function, swapData()
函数中处理, data
will be filled in some other method such as useData()
data
将用其他方法填充,例如useData()
Here is the code: 这是代码:
...
typedef unsigned short int USINT16;
typedef struct {
USINT16 a : 1;
USINT16 b : 1;
USINT16 c : 1;
USINT16 d : 1;
USINT16 e : 1;
USINT16 f : 1;
USINT16 g : 1;
USINT16 h : 2;
USINT16 i : 3;
USINT16 j : 4;
} OtherType; // 16 bits
typedef struct {
USINT16 X;
USINT16 Y;
USINT16 Z;
OtherType W;
} MainType;
...
unsigned short dataBuffer[4]; // available in global scope
...
void swapData() {
receiveData(&dataBuffer); // data buffer is filled
int i;
for (i = 0; i < 4; i++) {
dataBuffer[i] = __builtin_bswap16(dataBuffer);
}
// The data is little endian now ?
}
...
void useData() {
MainType data; // map the swapped buffer to data
// use the data etc.
....
}
If the remote machine behavior is frozen, you can investigate and determine what is the encoding of bit-fields on that platform and translate the buffer received on the local machine appropriately. 如果远程计算机行为被冻结,您可以调查并确定该平台上的位字段的编码是什么,并适当地转换在本地计算机上接收的缓冲区。
Byte swapping all 16 bit entries, including W
, is a good initial step, you might have to change the struct
definition for OtherType
to fit the order in which it is defined by the compiler for the remote machine. 字节交换所有16位条目(包括
W
)是一个很好的初始步骤,您可能必须更改OtherType
的struct
定义以适合远程计算机的编译器定义的顺序。 You can determine that by transmitting samples from the remote machine where only 1 field is set to all bits one and the others stay 0 and print the 16 bit value received. 您可以通过从远程计算机传输样本来确定,其中只有1个字段设置为所有位1,其他位置保持为0并打印接收到的16位值。
Byte-swapping W
is advisable because Wh
most likely falls on a byte boundary with 1 bit in each byte. 建议使用字节交换
W
,因为Wh
很可能落在字节边界上,每个字节有1位。 For its bits to be adjacent in the local machine, the bytes in W
should be swapped. 要使其在本地机器中相邻的位,应交换
W
的字节。 If the bit order on the remote machine is abcdefg h1 h0 i2 i1 i0 j3 j2 j1 j0
for the whole 16 bit word in W
, when stored in memory on the remote machine, it becomes <abcdefg h1>
<h0 i2 i1 i0 j3 j2 j1 j0>
and subsequently transmitted as bytes and loaded in a 16 bit register on the local machine, it would become h0 i2 i1 i0 j3 j2 j1 j0 abcdefg h1
if you dont swap the bytes, because the first byte is loaded in the low order bits of the register. 如果远程机器上的位顺序是对于
W
的整个16位字abcdefg h1 h0 i2 i1 i0 j3 j2 j1 j0
,当存储在远程机器的存储器中时,它变为<abcdefg h1>
<h0 i2 i1 i0 j3 j2 j1 j0>
随后作为字节发送并加载到本地机器上的16位寄存器中,如果不交换字节,它将变为h0 i2 i1 i0 j3 j2 j1 j0 abcdefg h1
,因为第一个字节以低位加载寄存器的位。 Byte swapping prevents this but you still may have a problem with the bit-field order in the local machine as your current definition might be encoded as j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba
if the bit-fields are likely allocated from the lowest to the highest bit positions. 字节交换可以防止这种情况,但是您仍然可能在本地计算机中遇到位字段顺序问题,
j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba
如果位字段可能从最低位分配,则当前定义可能被编码为j3 j2 j1 j0 i2 i1 i0 h1 h0 gfedcba
到最高位位置。
If you know some assembly language, generate the assembly for code that manipulate the bit-fields on both platforms and check if the fields are placed differently. 如果您知道某种汇编语言,请生成用于操作两个平台上的位字段的代码的程序集,并检查字段的放置方式是否不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.