C Programming Specification, demo sample code.

Source: Internet
Author: User

/****************************************************************copyright (c) 2014,tianyuan*all rights reserved.* * FileName: standard.h* file ID: Programming Specification Demo Sample code * * Current Version number: V1.0*WUYQ * finish date: 20140709** change record 1://change history. Contains the date of change, version number, changes and changes, etc. * Change date version number change person change content *---------------------------------------------------- ----------------------------------------------* 20140709 V1.0 wuyq Create ******************* /#ifndef _standard_h__#define _standard_h__/* redefine some basic data types */typedef Char s8;typedef unsigned char u8;typedef signed short s16;typedef unsigned short u16;type def int s32;typedef unsigned int u32;typedef float f32;typedef signed Long long s64;ty Pedef unsigned long long u64;typedef enum {FALSE =0, TRUE =!  FALSE} bool;         typedef BOOL BOOL;/* Message header */typedef struct {u16 u16msgtype;       /* Message type */U16 u16msglength; /* Long valid message dataDegree */U8 u8transtype;/* transmission Channel 0: Network 1: Serial/U8 u8reserved[3];/* alignment */}stru_msg_head;/* time information */typedef struct {u1    6 u16year;/* Year * * U16 u16month;/* Month */U8 u8day;/* Day */U8 u8hour;/* */U8 u8minute;/* points */ U8 u8second;/* sec */}stru_time_info;/* mcm->main Query System Information */struct stru_machine_info_req {stru_msg_head struMsgHe             ader;/* message Header */};/* MAIN->MCM feedback System Information */struct STRU_MACHINE_INFO_RSP {stru_msg_head strumsgheader;/* message header */F32             f32temp;/* Current Sample Temperature */F32 f32vol;/* Current Sample voltage */F32 F32CPUFREQ;/*CPU frequency */u32    u32freemem;/* remaining memory */u32 u32freedisk;/* remaining flash space */};/* main returns to MCM software upgrade results */struct STRU_SOFTWARE_UPDATE_RSP {    Stru_msg_head Strumsgheader; U8 b8successful;/* Whether the upgrade succeeds */U8 u8reserved[3];/* word alignment, retains the */};//function head can be used such as the following style/******************* * Description of function: * Input parameters: * OUTPUT Parameters: * Return Value: * Other instructions: * Change record 1://change history, includingChange date, version number, change person and change content * Change Date version number change the content of the person *---------------------------------------------------- ----------------------------------------------* 20140709 V1.0 wuyq Create ***************** /#endif


/***********************************************************************copyright (c) 2014,TIANYUAN*ALL rights reserved.** file name: unittest.c* file ID: None * Content Summary: Protocol and Unit Test demo sample code * Other Instructions: none * Current version number: v1.0* Author: wuyq* finish date: 20140709** change record 1://Change Calendar History, including date of change, version number, change of person and content of changes * Change Date version number change the content of the person *------------------------------------------------ --------------------------------------------------* 20140709 V1.0 wuyq Create *************** /#include <stdio.h> #include <string.h>// Redefine data type typedef unsigned char uint8;typedef unsigned short int uint16;typedef unsigned int uint32;typedef s igned int INT32;    Message header structure typedef struct{UINT16 iReserve1;    UINT16 iReserve2;    UINT16 IReserve3; UINT16 IReserve4;} msghead_t;                Message structure (including message header and message body) typedef struct{MSGHEAD_T Msghead;      Message header UINT32 Iopertype;  Operation type, the operation type can only be 1 or 2 UINT8     SZUSERNUMBER[30];      User number UINT8 szopertime[20];                Operation time, the format is: YYYYMMDD UINT32 iReserve1;           Reserved Field 1 UINT8 szreserve2[50]; Reserved field 2}userreqmsg_t; function declaration INT32 procuserreqmsg (userreqmsg_t *ptuserreqmsg); INT32 main (); /*********************************************************************** function Description: main function * Input parameters: None * Output parameters: None * return value: 0-run Complete * Other instructions: no * change Date version number change the content of the person *------------------------------------------------------------------ --------------------------------* 20140507 V1.0 zzx Create **********************************    /int32 Main () {UINT8 iretval = 0;        UINT32 iopertype = 0;      Operation type UINT8 szusernumber[30] = {0};    User number UINT8 szopertime[10] = {0};     Operation time in the format: YYYYMMDD userreqmsg_t tuserreqmsg = {0};    Request message//Assign a value to the header of the message tUserReqMsg.MsgHead.iReserve1 = 1; Tuserreqmsg.msgHead.ireserve2 = 2;    TUserReqMsg.MsgHead.iReserve3 = 3;     TUserReqMsg.MsgHead.iReserve4 = 4;    Read into the value of the Detail Message field printf ("Operation type: \ n");    scanf ("%d", &iopertype);    printf ("User number: \ n");    scanf ("%s", Szusernumber);    printf ("Operation time: \ n");     scanf ("%s", szopertime);    Assign a value to the Detail message field (the reserved field is not assignable) Tuserreqmsg.iopertype = Iopertype; strncpy (Tuserreqmsg.szusernumber, Szusernumber, strlen (Szusernumber));//Get number, replace strncpy strcpy with strncpy (     Tuserreqmsg.szopertime, Szopertime, strlen (szopertime));  Get time, replace strcpy with strncpy//exception inference for the field of the message body iRetVal = procuserreqmsg (&tuserreqmsg);  Note: When passing the reference, add the & if (iRetVal = = 0)//function to run correctly {//Print the Message field contents printf ("The user request message is:iopertype=%d, szusernumber=%s, szopertime=%s.\n ", Tuserreqmsg.iopertype, Tuserreqmsg.szusernumber,        Tuserreqmsg.szopertime);    return 0;  } else//print exception message {printf ("Some content of the user request message is wrong, check!\n");      return-1; }}/*********************************************************************** function Description: Exception inference for the field of the message body * Input parameters: ptuserreqmsg-user Request message * OUTPUT parameters: None * return value: 0-Success Other-failure * Other Instructions: no * change Date version number change the content of the person *------------------             --------------------------------------------------------------------------------* 20140507 V1.0 ZZX Create ***********************************************************************/int32 ProcUserReqMsg (UserReqMsg_T *p     tuserreqmsg) {INT32 iretvalue = 0; Exception inference for input parameters if (ptuserreqmsg = = NULL) {printf ("procuserreqmsg (...): input parameter (PTUSERREQMSG) is null         . \ n ");    return-1; }//exception inference for the Message body field if ((Ptuserreqmsg->iopertype! = 1) && (ptuserreqmsg->iopertype! = 2))//operation type can only is 1 or 2, the other is data exception {printf ("procuserreqmsg (...): The Iopertype is wrong, iopertype=%d.\n", Ptuserreqmsg->iopertyp        e);    Return-2; } if (Strlen (ptuserreqmsg->szuseRnumber)! = 8)//user number is abnormal, length 8 bits is correct {printf ("procuserreqmsg (...): The Szusernumber is wrong.\n");    return-3; } if (strlen (ptuserreqmsg->szopertime)! = 8)//Operation time exception, length 8 bits is correct {printf ("procuserreqmsg (...): the Szoper               Time is wrong.\n ");    return-4; } return 0;}

C Programming Specification, demo sample code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.