C Programming specification, sample code.

Source: Internet
Author: User

C Programming specification, sample code.

/*************************************** * ************************ Copyright (c) 2014, TianYuan * All rights reserved. ** file name: standard. h * File ID: Sample Code for programming specifications ** current version: V1.0 * Author: wuyq * completion date: 20140709 ** modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ************* ***************************** * **********************/# Ifndef _ STANDARD_H __# define _ STANDARD_H _/* redefinition some basic data types */typedef char s8; typedef unsigned char u8; typedef signed short s16; typedef unsigned short 2010; typedef int s32; typedef unsigned int u32; typedef float f32; typedef signed long s64; typedef unsigned long u64; typedef enum {FALSE = 0, TRUE =! FALSE} bool; typedef bool BOOL;/* message header */typedef struct {2010u16msgtype;/* Message Type */service.u16msglength;/* valid message data length */u8 u8TransType; /* transmission channel 0: Network 1: Serial Port */u8 u8Reserved [3];/* alignment */} STRU_MSG_HEAD;/* time information */typedef struct {2010u16year; /* year */2010u16month;/* month */u8 u8Day;/* day */u8 u8Hour;/* hour */u8 u8Minute;/* minute */u8 u8Second; /* seconds */} STRU_TIME_INFO;/* MCM-> Main query system information */struct stru_machine_info_req {STRU_MSG_HEAD struMsgHeader;/* message header */}; /* Main-> MCM feedback system information */struct stru_machine_info_rsp {STRU_MSG_HEAD struMsgHeader;/* message header */f32 f32Temp;/* Current sampling temperature */f32 f32Vol; /* Current Sampling voltage */f32 f32CpuFreq;/* Cpu frequency */u32 u32FreeMem;/* remaining memory */u32 u32FreeDisk;/* remaining FLASH space */}; /* Main returns the software upgrade result to MCM */struct stru_software_update_rsp {STRU_MSG_HEAD struMsgHeader; u8 b8Successful;/* indicates whether the upgrade is successful */u8 u8Reserved [3, retain */}; // use the following style for the function Header /***************************** **************************************** ** function description: * input parameter: * output parameter: * return value: * Other Description: * modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ************* **************************************** * *****************/# endif


 
/*************************************** * ******************************* Copyright (c) 2014, TianYuan * All rights reserved. ** file name: UnitTest. c * File ID: none * Content summary: Protocol and unit test sample code * Other Instructions: none * Current version: V1.0 * Author: wuyq * completion date: 20140709 ** modify record 1: // modify history, including the modification date, version number, modifier, and modification content. * modify date version number modifier content * required * 20140709 V1.0 wuyq creation ************* **************************************** * ****************/# include
 
  
# Include
  
   
// Redefine the Data Type typedef unsigned char UINT8; typedef unsigned short int UINT16; typedef unsigned int UINT32; typedef signed 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, in the format of y Yyymmdd 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 parameter: none * output parameter: none * return value: 0-execution completed * Other Description: No * modification date version number modifier modified * expires * 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 of yyyymmdd UserReqMsg_T tUserReqMsg = {0}; // request message // assign a value to the Message Header tUserReqMsg. msgHead. iReserve1 = 1; tUserReqMsg. msgHead. iReserve2 = 2; tUserReqMsg. msgHead. iReserve3 = 3; tUserReqM Sg. msgHead. iReserve4 = 4; // read the value of the Message Field printf ("Operation Type: \ n"); scanf ("% d", & iOperType); printf ("user number: \ n "); scanf (" % s ", szUserNumber); printf (" Operation Time: \ n "); scanf (" % s ", szOperTime ); // assign values to specific message fields (retain fields without assigning values) tUserReqMsg. iOperType = iOperType; strncpy (tUserReqMsg. szUserNumber, szUserNumber, strlen (szUserNumber); // obtain the number. Use strncpy instead of strcpy strncpy (tUserReqMsg. szOperTime, szOperTime, strlen (szOperTime ));// Obtain the time. Use strncpy to replace strcpy. // the message body field is incorrectly judged. iRetVal = ProcUserReqMsg (& tUserReqMsg); // note: when passing parameters, add & if (iRetVal = 0) // The function execution is correct {// print The message field content printf ("The user request message is: iOperType = % d, szUserNumber = % s, szOperTime = % s. \ n ", tUserReqMsg. iOperType, tUserReqMsg. szUserNumber, tUserReqMsg. szOperTime); return 0;} else // print the exception message {printf ("Some content of the user request message is wrong, please ch Eck! \ N "); return-1 ;}} /*************************************** * ******************************** function description: exception judgment on the fields of the message body * input parameter: ptUserReqMsg-user request message * output parameter: none * return value: 0-success other-failure * Other description: no * modified date version number modifier modified content * found * 20140507 V1.0 zzx create ************************ **************************************** * *****/INT32 ProcUserReq Msg (UserReqMsg_T * ptUserReqMsg) {INT32 iRetValue = 0; // exception judgment on input parameters if (ptUserReqMsg = NULL) {printf ("ProcUserReqMsg (...): input parameter (ptUserReqMsg) is NULL. \ n "); return-1 ;}// exception judgment on the message body field if (ptUserReqMsg-> iOperType! = 1) & (ptUserReqMsg-> iOperType! = 2) // The operation type can only be 1 or 2, and the other is Data Exception {printf ("ProcUserReqMsg (...): the iOperType is wrong, iOperType = % d. \ n ", ptUserReqMsg-> iOperType); return-2;} if (strlen (ptUserReqMsg-> szUserNumber )! = 8) // The user number is incorrect. The length is 8 characters. {printf ("ProcUserReqMsg (...): the szUserNumber is wrong. \ n "); return-3;} if (strlen (ptUserReqMsg-> szOperTime )! = 8) // The operation time is abnormal. The length is 8 bits. {printf ("ProcUserReqMsg (...): the szOperTime is wrong. \ n "); return-4;} return 0 ;}
  
 

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.