Design of fixed-length communication command (with start and end character frame)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/mzldxf/article/details/102512029


Measurement and control system in the industrial field, using a communication protocol often custom ASCII MODBUS-RTU protocol or the like. This paper describes a communication protocol ASCII custom design instruction.

Fixed-length instruction

Fixed-length and non-fixed-length instructions in the instruction command communication for a small number of instructions of the protocol, we can simply use fixed-length instructions.

Configuration instructions

Here Insert Picture Description
FIG. 1 [1]: Sample communication instruction
1, instruction generally includes the following sections: start character, the address of the receiving terminal, the instruction number, the data needs to be passed, terminator. In addition to the data portion, other parts are good provisions can not be changed. Instruction may be determined by the start character and stop character.

Composition data

Normal data constituting two ways, is used in the above decimal value of the data itself, i.e.,% d. More often actually is an ASCII manner shown in Figure 2:
Here Insert Picture Description
Figure 2: ASCII code data transfer instruction
in accordance with a predetermined data agreed well be a high-bit, the data may be 6 high. When calculating attention to subtract 0x30 recalculated.

Code verification

Sometimes in order to make the communication more reliable calibration will use, in the instruction need to add a check code, for example, cumulative and verification:
Here Insert Picture Description
Figure 3: accumulation and verification
example 6 was calculated from the data and the address, whichever is lower then 7 bit and as a cumulative sum check code, written in commands. After the calculation in the program and command 0x22 comparison, whether identical, the data transmission is considered correct.

The sample code

/*************************************************************************
* 满洲里国峰电子科技
* www.guofengdianzi.com
* 微信: GuoFengDianZi
***************************************************************************/
#define START 0X01
#define ADDRESS 0X60
#define COMMAND 0X0D
#define END 0X0E
#define COMMAND_LENGTH 10
unsigned char Buffer[BUFFER_LENGTH];

//读取串口缓存区内数据的个数
RxBuffLength_N=UART1_GetCharsInRxBuf();
//串口缓存区内数据的个数等于或超过了指令的长度,
if(RxBuffLength_N>=COMMAND_LENGTH){
	//将数据从缓冲区中导出					
	TransportUartData(&Buffer[0], RxBuffLength_N);
	//判断是否为该命令,是,则并计算数据的和,不是该命令返回DL101RETURN_ERROR报错
	ReturnValue=Read_and_Cal_ADValue(&Buffer[0], RxBuffLength_N);
	if(ReturnValue==DL101RETURN_ERROR)
		ReturnValue=0;//返回值为0说明有错误,放弃本次计算
	else
		printf("sum=%d\r\n",ReturnValue);//输出计算结果
}		

Author: V Bear (specialty: RF chip design, radar systems Hobbies: Embedded welcome to cooperation and exchange projects.)
Micro letter: GuoFengDianZi

Reference:
[. 1]: www.guofengdianzi.com

Guess you like

Origin blog.csdn.net/mzldxf/article/details/102512029