trying to copy struct members to byte array in c -


I have a struct into a byte array for ints, four and imitate members containing a mixture of characters Array Trying to send on a serial line so far I have

  struct msg_on_send {char discriptor_msg [5]; Int address; Four places; Four CMDMGG [5]; Four cr; Four lf; }; Zero switch_output_on () {int member; Struct msg_on_send SendMsg_on [sizeof member] = {}; Unsigned four buffer [sizeof SendMsg_on [0]]; Showbite (buffer, serialize (buffer, sendmail_on)); } / ************************************************************************************************** **************************************************************************************************** the *************************** * Function: ArrayBuild * * purpose: to sequentially * * struct members transfer into an array of four for uses Memcopy * arguments: * Return: Akar_ti i = array number of bytes * ************************* ************************************************************************************************ ************** Size / October (unsigned char * dst, const struct msg_on_send * object) {size_t i = 0; Mempie (and DST [i], and Object-> Descriptor_MG, Object of shape-> Descriptor_msg); I + = sizeof object-> Descriptor_mms; Memcpy (& amp; dst [i], & amp; Object-> Addresses, Objects of Object-> Addresses); I + = shape object-> Address; Memcpy (& amp; dst [i], & amp; Object-> Space, sizeof object-> Space); I + = sizeof object-> Space; Memc (& amp; DST [i], and Object-> CMDMGG, Size of Object-> CMDMSG); I + = sizeof object-> Cmdmsg; Mempie (and DST [i], and object-> CR, Object of shape-> CR); I + = Size Object-> CR; Mempi (and DST [i], and Object-> LF, Size of Object-> LF); I + = sizeof object-> LF; Return i; } / ************************************************************************************************** **************************************************************************************************** *********************** * Function: USARTWrite * purpose: void * object = struct members: USART data register * the logic writes the array data * * Akar_ti size = remaining array size * returns: none * *********************************** ******************************************************************************************** ****************************** / zero USARTWrite (const void * object, size_t size) {const unsigned char * bytes; {Printf ("% 02X", * byte) for (byte = object; size -; ++ byte); } Cursed ('\ n'); }  

As I have received this code, I do not fully understand how it works. I can see that it takes each element of memcpy struct and it is indexed by a serial stream 'i' variable, but I do not know how USARTWrite function to load the array into a string with the Packetises, or how My Structural Initialization.

Sorry, this post is a little late, but I'm just starting this programming lark, and I'm trying to get my head around this concept.

Thanks Dave

EDIT:

Wow, very good answer quickly - Thanks guys.

Slaj It'm sounds logical to me, I really had not thought of this approach has not been around far pointers to my head, but I began to see that They are an essential part of C, so I will keep a watch.

  • This line code sends data to my UART, which content will I change to an array? It is where I would have to tell me where to start my composition and how big it is, but a variable amount to send an array I've been missing a logical step

      USART_SendData (USART1, message_on_contents [array_count]);     P> Dave   

    Sorry I have not seen your comment yet. The code below is fine on Linux, so I hope this will work for you.
    printf () is printing in hex, you will get 2 letters for each byte.

      #include & lt; Stdio.h & gt; Struct msg_on_send {char discriptor_msg [5]; Int address; Four places; Four CMDMGG [5]; Four cr; Four lf; }; Zero USARTWrite (Constant Worth * Object, Size_T Shape) {conf unsigned char * byte; {Printf ("% 02X", * byte) for (byte = object; size -; ++ byte); } Cursed ('\ n'); } Int main (int argc, char ** argv) {struct msg_on_send myMsg; Unsigned four * ptr = (unsigned char *) and myMsg; USARTWrite (ptr, sized (myMsg)); Return 0; }  

    I hope this will be helpful.

    ~


Comments