单片机C语言 串口传输 结构体

串口传输 结构体  就是 把结构体 转换为 数组 ,然后传输 数组中的 char。


即 struct  - - - - - - ->> char [ ] ;

然后 char[ ] - - - -- - - - -->> struct .


给出一下简单实例代码 :

#include
#include

/*--------------------------start file---------------------------------*/
typedef struct
{
	int id;
	char name[10];
	int score;
}student;


int struct_array(char* des, student *src)
{
	int num;
	char *temp = (char *)  src ;
	for(num=0;numid);
	printf("name is : %s\n",name->name);
	printf("score is : %d \n",name->score);

}


void show_array(char *str ,int len )
{
	printf("len is : %d \n" ,len);

	while(len--)
	{
		putchar('|');
		putchar(*str++);
	}
	printf("|\n");
}

	

int main()
{
	char name[10] ="laomao";

	student laomao = { 123 , {"laomao"} , 99 } ; 
	char save[100] ;
	int len ;


	student * answer;



	len = struct_array(save, &laomao) ;
	show_array(save , len);

	array_struct(answer , save) ;
	show_student(answer);
	show_student(&laomao);
	



	return 0;
}

/*---------------------------end file----------------------------------*/

运行结果:

-------------------------------------------------------

len is : 20
|{| | | |l|a|o|m|a|o| | | | | | |c| | | |
id is : 123
name is : laomao
score is : 99
id is : 123
name is : laomao
score is : 99

---------------------------------------------------------------


你可能感兴趣的:(单片机C语言 串口传输 结构体)