🌐 This page is not yet available in English. Showing the Chinese version. Back to Chinese page.
头文件(Header file)是一种包含声明或定义的Text文件,通常用于在程序中引入外部代码。在C和C++中,头文件通常包含了函数、变量、常量的声明、Type定义以及宏定义等信息。头文件的主要作用是提供接口和声明,使得源代码文件可以访问到其他模块或库中的函数、变量和常量,同时也有利于代码的模块化和组织。
这段代码是一个用于Modbus单元测试的头文件,其中包含了多个常量和配置,方便在测试过程中使用。让我来解释一下:
/* 版权信息 */
#ifndef _UNIT_TEST_H_
#define _UNIT_TEST_H_
/* 由configure.ac定义的常量 */
#define HAVE_INTTYPES_H @HAVE_INTTYPES_H@
#define HAVE_STDINT_H @HAVE_STDINT_H@
// 如果定义了HAVE_INTTYPES_H,包含inttypes.h库
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
// 如果定义了HAVE_STDINT_H,根据编译器决定包含的stdint.h库
#ifdef HAVE_STDINT_H
# ifndef _MSC_VER
# include <stdint.h>
# else
# include "stdint.h"
# endif
#endif
#define SERVER_ID 17 // 正常的服务器ID
#define INVALID_SERVER_ID 18 // 非法的服务器ID,用于触发Error处理
// Modbusaddress和quantity定义
const uint16_t UT_BITS_ADDRESS = 0x130; // Coil address
const uint16_t UT_BITS_NB = 0x25; // 线圈quantity
const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B }; // 预设的Coil status
const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4; // 输入Coil address
const uint16_t UT_INPUT_BITS_NB = 0x16; // 输入线圈quantity
const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 }; // input state
const uint16_t UT_REGISTERS_ADDRESS = 0x160; // register address
const uint16_t UT_REGISTERS_NB = 0x3; // number of registers
const uint16_t UT_REGISTERS_NB_MAX = 0x20; // 最大number of registers
const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 }; // register value
// 特殊address,用于单元测试中触发异常和特殊行为
const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x170; // 特殊register address
const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171; // InvalidTID或从Device address
const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172; // 500毫秒睡眠address
const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173; // 5毫秒byte睡眠address
// 特殊的number of registers,用于ErrorResponse测试
const uint16_t UT_REGISTERS_NB_SPECIAL = 0x2; // 特殊number of registers
const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x108; // Enter register address
const uint16_t UT_INPUT_REGISTERS_NB = 0x1; // 输入number of registers
const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A }; // 输入register value
const float UT_REAL = 123456.00; // 浮点数测试值
// 大小端转换数组,用于浮点数在Modbus协议中的存取测试
const uint8_t UT_IREAL_ABCD_SET[] = {0x47, 0xF1, 0x20, 0x00}; // ABCDformat设置
const uint16_t UT_IREAL_ABCD_GET[] = {0x47F1, 0x2000}; // ABCDformat获取
const uint8_t UT_IREAL_DCBA_SET[] = {0x00, 0x20, 0xF1, 0x47}; // DCBAformat设置
const uint16_t UT_IREAL_DCBA_GET[] = {0x0020, 0xF147}; // DCBAformat获取
const uint8_t UT_IREAL_BADC_SET[] = {0xF1, 0x47, 0x00, 0x20}; // BADCformat设置
const uint16_t UT_IREAL_BADC_GET[] = {0xF147, 0x0020}; // BADCformat获取
const uint8_t UT_IREAL_CDAB_SET[] = {0x20, 0x00, 0x47, 0xF1}; // CDABformat设置
const uint16_t UT_IREAL_CDAB_GET[] = {0x2000, 0x47F1}; // CDABformat获取
#endif /* _UNIT_TEST_H_ */这个头文件主要包含了以下内容:
- 常量定义:
SERVER_ID和INVALID_SERVER_ID分别定义了正常和非法的服务器ID。- Modbusaddress和quantity定义:
- 定义了Coil address、线圈quantity、Coil statusdata集、输入Coil address、输入线圈quantity、input statedata集、register address、number of registers、最大number of registers、register value等。
- 特殊address定义:
- 用于单元测试中触发异常和特殊行为的register address,如InvalidTID或从Device address、睡眠address等。
- 特殊的number of registers定义:
- 用于ErrorResponse测试的特殊number of registers。
- 浮点数测试值定义:
- 定义了一个浮点数测试值。
- 大小端转换数组定义:
- 用于浮点数在Modbus协议中的存取测试的不同format数组定义。
这些定义使得在进行Modbus协议Related的单元测试时更加方便和system化。
发表回复