Because it is just for testing, it is all written in C. After the interface is completed, it will be encapsulated with classes.
Because my E language is very poor, the names of variables and functions are Chinese-style. Please understand. But I have annotated it in detail.
I would like to use this
article
to commemorate my crappy self-taught
programming
career
......................
#include
"
stdio.h
"
#include
<
windows.h
>
///
... //Data packet sequence number
DWORD sjbcd; //Data packet length
};
//Server-》Client packet
struct MSG_STOC {
DWORD cmd; //Command identifier
DWORD sjbcd; //The length of the entire MSG_STOC data packet buffer
BYTE shuju[1000]; //Additional data buffer
};
//Client-》Server packet
struct MSG_CTOS {
DWORD cmd; //Command identifier
DWORD sjbcd; //The length of the entire MSG_CTOS data packet buffer
BYTE shuju[1800]; //Additional data buffer
};
//Union
MSG_DATA {
struct MSG_STOC msg_stoc;
struct MSG_CTOS msg_ctos;
};
//Complete data packet
struct SJB {
struct MSG_TOU tou; //Data packet header
union MSG_DATA data; //Data buffer
};
///
...
cmd,DWORD sxid,struct SJGD*sjgd,struct SJB*sjb)
{
sjb->tou.lxid=lxid; //Fill the data packet type
sjb->tou.sxid=sxid; //Fill the data packet sequence number
if(lxid==CTOS)
{
sjb->data.msg_ctos.cmd=cmd; //Fill the command identifier
MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd,sjgd->sjcd); //Fill the additional data
sjb->data.msg_ctos.sjbcd=sjgd->sjcd; //Fill the length of the entire additional data buffer
}
if(lxid==STOC)
{
sjb->data.msg_stoc.cmd=cmd; //Fill command identifier
MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd,sjgd->sjcd); //Fill additional data
sjb->data.msg_stoc.sjbcd=sjgd->sjcd; //Fill the length of the entire additional data buffer
}
sjb->tou.sjbcd=20+sjgd->sjcd; //Fill data packet length
if((lxid!=CTOS)&&(lxid!=STOC)) return 0;
return 1;
}[page] ///
...
///
...
//The function is to send a standard data packet in binary form to the data pipe buffer
int Fabao(struct SJB*sjb,struct SJGD*sjgd)
{
MoveMemory(sjgd->sjgd,&sjb->tou.lxid,4); //Fill in data packet type
MoveMemory(sjgd->sjgd+4,&sjb->tou.sxid,4); //Fill in data packet sequence number
if(sjb->tou.lxid==CTOS)
{
MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4); //Fill the length of the data packet
MoveMemory(sjgd->sjgd+12,&sjb->data.msg_ctos.cmd,4);//Fill the command identifier
MoveMemory(sjgd->sjgd+16,&sjb->data.msg_ctos.sjbcd,4);//Fill the length of the entire additional data buffer
MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_ctos.sjbcd); //Fill additional data
sjgd->sjcd=sjb->tou.sjbcd; //Update the data pipeline length data
return 1;
}
if(sjb->tou.lxid==STOC)
{
MoveMemory(sjgd->sjgd+8,&sjb->tou.sjbcd,4);//Fill the length of the data packet
MoveMemory(sjgd->sjgd+12,&sjb->data.msg_stoc.cmd,4);//Fill the command identifier
MoveMemory(sjgd->sjgd+16,&sjb->data.msg_stoc.sjbcd,4);//Fill the length of the entire additional data buffer
MoveMemory(sjgd->sjgd+20,sjb->data.msg_ctos.shuju,sjb->data.msg_stoc.sjbcd); //Fill additional data
sjgd->sjcd=sjb->tou.sjbcd; //Update the data pipeline length data
return 1;
}
return 0;
}
///
...
if((c>='0')&&(c<='9'))
{
return (int)(c-'0');
}
return 0;
}
DWORD hex_dw(BYTE*hex) //Read a 4-bit integer from memory
{
DWORD D=0;
char x[2];
sprintf(x,"%.2X",hex[0]);
D+=hex_int(x[1]);
D+=hex_int(x[0])*16;
sprintf(x,"%.2X",hex[1]);
D+=hex_int(x[1])*16*16;
D+=hex_int(x[0])*16*16*16;
sprintf(x,"%.2X",hex[2]);
D+=hex_int(x[1])*16*16*16*16;
D+=hex_int(x[0])*16*16*16*16*16;
sprintf(x,"%.2X",hex[3]);
D+=hex_int(x[1])*16*16*16*16*16*16;
D+=hex_int(x[0])*16*16*16*16*16*16*16;
return D;
////
////////////////////////////////////////////////////////////////////////////////////
///
...The second parameter is the unpacked data storage structure
//The function is to unpack the data in the data pipeline into a standard remote control data packet to control the program flow
int Jiebao (struct SJGD*sjgd, struct SJB*sjb)
{
sjb->tou.lxid=hex_dw(sjgd->sjgd); //Unpacked data packet type
sjb->tou.sxid=hex_dw(sjgd->sjgd+4); //Decapsulate data packet sequence number
sjb->tou.sjbcd=hex_dw(sjgd->sjgd+8); //Decapsulate data packet length
if(sjb->tou.lxid==CTOS)
{
sjb->data.msg_ctos.cmd=hex_dw(sjgd->sjgd+12); //Decapsulate command identifier
sjb->data.msg_ctos.sjbcd=hex_dw(sjgd->sjgd+16); //Decapsulate the length of the entire additional data buffer
MoveMemory(sjb->data.msg_ctos.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //Decapsulate additional data
return 1;
}
if(sjb->tou.lxid==STOC)
{
sjb->data.msg_stoc.cmd=hex_dw(sjgd->sjgd+12); //Unseal command identifier
sjb->data.msg_stoc.sjbcd=hex_dw(sjgd->sjgd+16); //Unseal the length of the entire additional data buffer
MoveMemory(sjb->data.msg_stoc.shuju,sjgd->sjgd+20,sjb->data.msg_ctos.sjbcd); //Unseal additional data
return 1;
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
This is the server-side send and receive thread function http://www.51hei.com/mcu/2611.html
Previous article:Screen snow program implemented by linked list
Next article:The lossless compression ratio of LZW compression is as high as 4:1.
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Request X-CUBE-MEMS1 firmware package
- TI CCS compiler download update
- Simple and efficient zero-point acquisition circuit
- How to choose the type and model of diode in circuit design
- Design of small signal precision rectifier circuit
- 【DWIN Serial Port Screen】Practical Application
- [National Technology N32G457 Review] RT-Thread drives SSD1306
- F28335 ADC module trigger EPWM setting error
- ADI Live Review on April 21: What to listen to in traffic jams? New generation of in-car audio systems and software-defined cars
- IIC communication problem between MSP430 and SHT11