我试图将包含int,char和char数组的结构的成员复制到字节数组中以发送到串行线.到目前为止我有
struct msg_on_send { char descriptor_msg[5]; int address; char space; char cmdmsg[5]; char CR; char LF; }; void switch_output_on() { int member; struct msg_on_send SendMsg_on[sizeof member] = { }; unsigned char buffer [ sizeof SendMsg_on[0] ]; showbytes(buffer, serialize(buffer, SendMsg_on)); } /*************************************************************************** * Function: ArrayBuild * * Purpose: Uses memcopy to transfer the struct members sequentially * * into an array of char * * Arguments: * * Returns: size_t i = a count of the number of bytes in the array * ***************************************************************************/ size_t ArrayBuild(unsigned char *dst, const struct msg_on_send *object) { size_t i = 0; memcpy(&dst[i], &object->descriptor_msg, sizeof object->descriptor_msg); i += sizeof object->descriptor_msg; memcpy(&dst[i], &object->address, sizeof object->address); i += sizeof object->address; memcpy(&dst[i], &object->space, sizeof object->space); i += sizeof object->space; memcpy(&dst[i], &object->cmdmsg, sizeof object->cmdmsg); i += sizeof object->cmdmsg; memcpy(&dst[i], &object->CR, sizeof object->CR); i += sizeof object->CR; memcpy(&dst[i], &object->LF, sizeof object->LF); i += sizeof object->LF; return i; } /*********************************************************************** * Function: USARTWrite * * Purpose: Writes the array data to the USART data register * * Arguments: void *object = struct member * * size_t size = size of array remaining * * Returns: None * ***********************************************************************/ void USARTWrite(const void *object, size_t size) { const unsigned char *byte; for ( byte = object; size--; ++byte ) { printf("%02X", *byte); } putchar('\n'); }
当我获得此代码时,我不完全理解它是如何工作的.我可以看到memcpy接受结构的每个元素并使其成为由'i'变量索引的串行流,但我不知道USARTWrite函数如何将其分组为字符串,或者如何加载数组我的结构初始化.
对不起,这篇文章有点长,但我刚刚开始编程,并试图了解这个概念.
谢谢戴夫
编辑:
哇,很快就有很多好的答案 - 谢谢你们.
slaz:这对我来说似乎是合乎逻辑的,我还没有真正考虑过这种方法,因为我还没有真正掌握指针,但我开始看到它们是C的重要组成部分,所以我真的会有看看.
这行代码将数据发送到我的UART,我将用包含消息内容的数组替换?看起来我错过了一个合乎逻辑的步骤,我有一个变量告诉我我的结构在哪里开始,它有多大,但没有数组要发送
USART_SendData(USART1, message_on_contents[array_count]);
哈珀谢尔比:谢谢你的描述,让我的想法更加清晰.
RGDS
戴夫
您不必将结构实际复制到字节数组中.你可以选择这样做:
struct msg_on_send myMessage; // code to set myMessage to whatever values... // get a byte pointer that points to the beginning of the struct uint8_t *bytePtr = (uint8_t*)&myMessage; // pass that into the write function, and it will write the amount of bytes passed in USARTWrite(bytePtr, sizeof(myMessage));
指针的力量!:)
对不起,直到现在我才看到你的评论.下面的代码在Linux上编译就好了,所以我希望它适合你.
printf()以十六进制打印,每个字节将获得2个字符.
#includestruct msg_on_send { char descriptor_msg[5]; int address; char space; char cmdmsg[5]; char CR; char LF; }; void USARTWrite(const void *object, size_t size) { const unsigned char *byte; for ( byte = object; size--; ++byte ) { printf("%02X", *byte); } putchar('\n'); } int main (int argc, char**argv) { struct msg_on_send myMsg; unsigned char* ptr= (unsigned char*)&myMsg; USARTWrite(ptr, sizeof(myMsg)); return 0; }
我希望这有帮助.
〜
〜