Serial port transmission structure is to convert the structure into an array, and then transfer the char in the array.
That is struct——->> char [] ;
Then char[]———>> struct.
Give a simple example code:
#include#include #include struct data{ int id; int score; char name[10];}; #define SIZE sizeof(struct data)void struct_array(char* des, struct data *src){ int num; char *temp = (char *)src; for(num=0; num id=1; shm->score=98; strcpy(shm->name, "admin"); char buf[SIZE]={" " }; struct data *str = (struct data *)malloc(SIZE); struct_array(buf, shm); show_array(buf, SIZE); array_struct(str, buf); printf("%d %d % s ", str->id, str->score, str->name); free(shm); free( str); return 0;}
Run result:
#include#include < string.h>#include struct data{ int id; int score; char name[10];}; #define SIZE sizeof(struct data)void struct_array(char* des, struct data *src){ int num; char *temp = (char *)src; for(num=0; num id=1; shm->score=98; strcpy(shm->name, "admin"); char buf[SIZE]={" "} ; struct data *str = (struct data *)malloc(SIZE); struct_array(buf, shm); show_array(buf, SIZE); array_struct(str, buf); printf("%d %d %s ", str->id, str->score, str->name); free(shm); free( str); return 0;}