MCU C language serial port transmission structure

Publisher:huanranLatest update time:2019-10-16 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Serial port transmission structure is to convert the structure into an array and then transmit the char in the array.


That is, struct - - - - - - ->> char [ ] ;


Then char[ ] - - - -- - - - -->> struct .


Here is a simple example code:


#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;num *(des+num)= temp[num];

return num;

 

}

 

 

void array_struct(student * des ,char * src)

{

 

char* temp = (char *) des;

int num;

for(num=0;num < sizeof(student);num++)

*(temp++)= src[num];

}

 

 

void show_student(student * name)

{

printf("id is : %dn",name->id);

printf("name is : %sn",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----------------------------------*/

operation result:


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


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


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

Keywords:MCU Reference address:MCU C language serial port transmission structure

Previous article:STC8 MCU + 8-pin OLED driver to display ADC value source program
Next article:Calculation of transmission time at different baud rates

Recommended ReadingLatest update time:2024-11-23 11:20

51 MCU state machine debounce code
#include   enum State {Init, Press, Free}; //Define the enumeration type of the key state enum State Key_State; //Key state   sbit Key1 = P2^4; sbit Key2 = P2^5; sbit Key3 = P2^6; sbit Key4 = P2^7;     void Delay_ms(unsigned int x) { int i,j; for(i=0; i 110; i++) for(j=0; j ; } //======================================
[Microcontroller]
Summary of commonly used interfaces of MCU ARM
Commonly used interfaces include spi, I2c, uart, etc. They all have their own protocol regulations. Let's talk about the connection and difference between them: 1 I2C bus      There are two main situations involving I2C programming: ARM chips with dedicated controllers and MCU chips without controllers. For ARM chip
[Microcontroller]
Summary of commonly used interfaces of MCU ARM
FR81S family 32-bit microcontroller builds body control platform
As global warming continues, the automotive industry needs to produce more environmentally friendly and fuel-efficient cars. Demand for luxury cars with improved safety and comfort is increasing in developed countries, while there is also a strong demand for compact cars in rapidly developing countries such as China,
[Microcontroller]
FR81S family 32-bit microcontroller builds body control platform
Design of intelligent bus overload monitoring system based on single chip microcomputer
Overloaded vehicles are prone to accidents, and once a bus has an accident, it is very easy to induce a major accident with mass deaths and injuries. It has caused great losses and harm to the country and the masses. At present, the country has issued a series of policies and regulations to stop the overloading of b
[Microcontroller]
51 MCU H-bridge circuit controls motor forward and reverse rotation and PWM speed regulation
I built an H-bridge circuit to control the forward and reverse rotation and PWM speed regulation of the motor. The program is available on the Internet. You can use it by changing the pins. The circuit and source program are as follows:      Function: P1.1 button to stop, P1.2 to turn left, P1.3 to turn right, P1.
[Microcontroller]
51 MCU H-bridge circuit controls motor forward and reverse rotation and PWM speed regulation
Freescale 9S12 Series MCU Application Notes (ECT Module) 4
Experiment 4: Timer overflow rollover function The so-called timer overflow flip is to flip the output leg PTx level of a channel when TCNT overflows. This function is very practical and can realize PWM function. First of all, we still need to introduce a related register TTOV. TTOV Register (Timer Toggle On Over
[Microcontroller]
Freescale 9S12 Series MCU Application Notes (ECT Module) 4
Design of PWM speed regulation of DC motor based on single chip microcomputer
Motor pwm speed control program /****************************************************       ========= DC Motor Control + Speed ​​Display =========== ********************************************************/ #include reg52.h //header file #define uchar unsigned char //macro definition #define uint unsigned int
[Microcontroller]
Design of PWM speed regulation of DC motor based on single chip microcomputer
RW61X: Wi-Fi 6 tri-band device in a secure i.MX RT MCU
Currently, there are at least 100 connected devices using different wireless standards in smart homes and modern buildings. NXP RW61x is a highly integrated, secure, tri-band wireless MCU that provides a powerful solution for this need through simple and efficient wireless connectivity. RW61x
[Embedded]
RW61X: Wi-Fi 6 tri-band device in a secure i.MX RT MCU
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号