我收到了需要在代码中实现的功能。该功能正在其自身的自测器上运行。 这是与struct相关联的(如果需要,我可以制作一个可执行的示例):
#define STEERINGCMD_MSG_BUFFERLENGTH 40
typedef struct GLOBAL_MSG
{
u1 starter;
u1 frameID;
u1 length;
u1 msgData[STEERINGCMD_MSG_BUFFERLENGTH];
u4 CRYPTO;
};
GLOBAL_MSG steeringCmdMsg;
struct SteeringData
{
int isL1;
int isL5;
unsigned long long CoPR;
unsigned long long CoPA;
unsigned long long CaPR;
unsigned long long CaPA;
};
void generateNextSteeringCmdMsg(SteeringData stData)
{
steeringCmdMsg.starter = 0xAA;
steeringCmdMsg.frameID = 68;
steeringCmdMsg.length = STEERINGCMD_MSG_BUFFERLENGTH;
if()
{
steeringCmdMsg.msgData[0] = 0x0F;
}
else if( )
{
steeringCmdMsg.msgData[0] = 0xF0;
}
steeringCmdMsg.msgData[1] = stData.CoPR & 0xFF;
steeringCmdMsg.msgData[2] = stData.CoPR >> 8 & 0xFF;
steeringCmdMsg.msgData[3] = stData.CoPR >> 16 & 0xFF;
steeringCmdMsg.msgData[4] = stData.CoPR >> 24 & 0xFF;
steeringCmdMsg.msgData[5] = stData.CoPR >> 32 & 0xFF;
steeringCmdMsg.msgData[6] = stData.CoPR >> 40 & 0xFF;
steeringCmdMsg.msgData[7] = stData.CoPR >> 48 & 0xFF;
steeringCmdMsg.msgData[8] = stData.CoPR >> 56 & 0xFF;
steeringCmdMsg.msgData[9] = stData.CoPA & 0xFF;
steeringCmdMsg.msgData[10] = stData.CoPA >> 8 & 0xFF;
steeringCmdMsg.msgData[11] = stData.CoPA >> 16 & 0xFF;
steeringCmdMsg.msgData[12] = stData.CoPA >> 24 & 0xFF;
steeringCmdMsg.msgData[13] = stData.CoPA >> 32 & 0xFF;
steeringCmdMsg.msgData[14] = stData.CoPA >> 40 & 0xFF;
steeringCmdMsg.msgData[15] = stData.CoPA >> 48 & 0xFF;
steeringCmdMsg.msgData[16] = stData.CoPA >> 56 & 0xFF;
steeringCmdMsg.msgData[17] = stData.CaPR & 0xFF;
steeringCmdMsg.msgData[18] = stData.CaPR >> 8 & 0xFF;
steeringCmdMsg.msgData[19] = stData.CaPR >> 16 & 0xFF;
steeringCmdMsg.msgData[20] = stData.CaPR >> 24 & 0xFF;
steeringCmdMsg.msgData[21] = stData.CaPR >> 32 & 0xFF;
steeringCmdMsg.msgData[22] = stData.CaPR >> 40 & 0xFF;
steeringCmdMsg.msgData[23] = stData.CaPR >> 48 & 0xFF;
steeringCmdMsg.msgData[24] = stData.CaPR >> 56 & 0xFF;
steeringCmdMsg.msgData[25] = stData.CaPA & 0xFF;
steeringCmdMsg.msgData[26] = stData.CaPA >> 8 & 0xFF;
steeringCmdMsg.msgData[27] = stData.CaPA >> 16 & 0xFF;
steeringCmdMsg.msgData[28] = stData.CaPA >> 24 & 0xFF;
steeringCmdMsg.msgData[29] = stData.CaPA >> 32 & 0xFF;
steeringCmdMsg.msgData[30] = stData.CaPA >> 40 & 0xFF;
steeringCmdMsg.msgData[31] = stData.CaPA >> 48 & 0xFF;
steeringCmdMsg.msgData[32] = stData.CaPA >> 56 & 0xFF;
char* actMsg =(char*) &steeringCmdMsg;
steeringCmdMsg.crc32 = calculateCRYPTOForBuf(actMsg, STEERINGCMD_MSG_BUFFERLENGTH - 4);
}
void getSteeringMsg(i1 msg[STEERINGCMD_MSG_BUFFERLENGTH])
{
char* actMsgData =(char*) &steeringCmdMsg.msgData;
msg[0] = steeringCmdMsg.starter;
msg[1] = steeringCmdMsg.frameID;
msg[2] = steeringCmdMsg.length;
memcpy( &msg[3], actMsgData, STEERINGCMD_MSG_BUFFERLENGTH - 7);
msg[39] = steeringCmdMsg.CRYPTO >>24;
msg[38] = steeringCmdMsg.CRYPTO >>16;
msg[37] = steeringCmdMsg.CRYPTO >>8;
msg[36] = steeringCmdMsg.CRYPTO;
}
//used like this in the main
int main {
i1 tmpMsg[STEERINGCMD_MSG_BUFFERLENGTH + 1];
memset(tmpMsg, 0, STEERINGCMD_MSG_BUFFERLENGTH + 1);
generateNextSteeringCmdMsg(steeringDataL1);
getSteeringMsg(tmpMsg);
write(tmpMsg, STEERINGCMD_MSG_BUFFERLENGTH);
return 0;
}
上下文: 我的设备需要以二进制形式接收我们所谓的“ steering msg”,其中包含基本标头和SteeringData中包含的所有参数结构。最后,将所有参数串联在msgData中,我计算出我们所谓的CRYPTO。并将其添加到邮件末尾。
问题是,在此代码中,stData.CoPR / CoPA ...是无符号的long long(大小= 8),而在我的代码中,这是一个double(struct.CoPR-不能修改,因为来自结构)。
所以我正在寻找修改此功能并用我的double结构替换此unsigned long的最佳方法。一种简单的强制转换,或另一种直接将我的double结构转换为char数组的方法。但是我不知道在哪里看,什么是最最安全最安全的方法。
随时要求更多解释,谢谢!