Note that there are some explanatory texts on larger screens.

plurals
  1. POtrying to copy struct members to byte array in c
    primarykey
    data
    text
    <p>I am attempting to copy the members of a struct containing a mixture of ints, char's and arrays of chars into a byte array to send to a serial line. So far I have</p> <pre><code>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(&amp;dst[i], &amp;object-&gt;descriptor_msg, sizeof object-&gt;descriptor_msg); i += sizeof object-&gt;descriptor_msg; memcpy(&amp;dst[i], &amp;object-&gt;address, sizeof object-&gt;address); i += sizeof object-&gt;address; memcpy(&amp;dst[i], &amp;object-&gt;space, sizeof object-&gt;space); i += sizeof object-&gt;space; memcpy(&amp;dst[i], &amp;object-&gt;cmdmsg, sizeof object-&gt;cmdmsg); i += sizeof object-&gt;cmdmsg; memcpy(&amp;dst[i], &amp;object-&gt;CR, sizeof object-&gt;CR); i += sizeof object-&gt;CR; memcpy(&amp;dst[i], &amp;object-&gt;LF, sizeof object-&gt;LF); i += sizeof object-&gt;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'); } </code></pre> <p>As I obtained this code, I don't fully understand how it works. I can see that the memcpy takes each element of the struct and makes it into a serial stream indexed by the 'i' variable, but I don't know how the USARTWrite function packetises this into a string, or how to load the array with my struct initialisation.</p> <p>Sorry this post is a bit long, but I'm just starting this programming lark, and trying to get my head around this concept.</p> <p>Thanks Dave</p> <p>EDIT:</p> <p>wow, many good answers quickly - thanks guys.</p> <p>slaz: That seems logical to me, I hadn't really thought about that approach as I haven't really got my head around pointers yet, but I am beginning to see that they are an essential part of C, so I duly will have a look.</p> <ul> <li><p>This line of code sends the data to my UART, what would I replace the array containing the message contents with? It seems like I am missing a logical step here where I have a variable telling me where my structure starts and how big it is, but no array to send</p> <pre><code>USART_SendData(USART1, message_on_contents[array_count]); </code></pre></li> </ul> <p>Harper Shelby: Thank you for that description, it makes it much clearer in my mind.</p> <p>rgds</p> <p>Dave</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload