The libmodbus library is a header file used for Modbus unit testing

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

The libmodbus library is a header file used for Modbus unit testing缩略图

A header file is a text file that contains declarations or definitions, typically used to introduce external code into a program. In C and C++, header files typically contain information such as declarations of functions, variables, constants, type definitions, and macro definitions. The main function of header files is to provide interfaces and declarations, allowing source code files to access functions, variables, and constants in other modules or libraries, while also facilitating code modularization and organization.

This code is a header file for Modbus unit testing, which contains multiple constants and configurations for easy use during the testing process. Let me explain:

/* 版权信息 */
#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,用于触发错误处理

// Modbus地址和数量定义
const uint16_t UT_BITS_ADDRESS = 0x130;  // 线圈地址
const uint16_t UT_BITS_NB = 0x25;        // 线圈数量
const uint8_t UT_BITS_TAB[] = { 0xCD, 0x6B, 0xB2, 0x0E, 0x1B };  // 预设的线圈状态

const uint16_t UT_INPUT_BITS_ADDRESS = 0x1C4;  // 输入线圈地址
const uint16_t UT_INPUT_BITS_NB = 0x16;        // 输入线圈数量
const uint8_t UT_INPUT_BITS_TAB[] = { 0xAC, 0xDB, 0x35 };  // 输入状态

const uint16_t UT_REGISTERS_ADDRESS = 0x160;  // 寄存器地址
const uint16_t UT_REGISTERS_NB = 0x3;         // 寄存器数量
const uint16_t UT_REGISTERS_NB_MAX = 0x20;    // 最大寄存器数量
const uint16_t UT_REGISTERS_TAB[] = { 0x022B, 0x0001, 0x0064 };  // 寄存器值

// 特殊地址,用于单元测试中触发异常和特殊行为
const uint16_t UT_REGISTERS_ADDRESS_SPECIAL = 0x170;               // 特殊寄存器地址
const uint16_t UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE = 0x171;  // 无效TID或从设备地址
const uint16_t UT_REGISTERS_ADDRESS_SLEEP_500_MS = 0x172;          // 500毫秒睡眠地址
const uint16_t UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS = 0x173;       // 5毫秒字节睡眠地址

// 特殊的寄存器数量,用于错误响应测试
const uint16_t UT_REGISTERS_NB_SPECIAL = 0x2;  // 特殊寄存器数量

const uint16_t UT_INPUT_REGISTERS_ADDRESS = 0x108;  // 输入寄存器地址
const uint16_t UT_INPUT_REGISTERS_NB = 0x1;         // 输入寄存器数量
const uint16_t UT_INPUT_REGISTERS_TAB[] = { 0x000A };  // 输入寄存器值

const float UT_REAL = 123456.00;  // 浮点数测试值

// 大小端转换数组,用于浮点数在Modbus协议中的存取测试
const uint8_t UT_IREAL_ABCD_SET[] = {0x47, 0xF1, 0x20, 0x00};   // ABCD格式设置
const uint16_t UT_IREAL_ABCD_GET[] = {0x47F1, 0x2000};          // ABCD格式获取
const uint8_t UT_IREAL_DCBA_SET[] = {0x00, 0x20, 0xF1, 0x47};   // DCBA格式设置
const uint16_t UT_IREAL_DCBA_GET[] = {0x0020, 0xF147};          // DCBA格式获取
const uint8_t UT_IREAL_BADC_SET[] = {0xF1, 0x47, 0x00, 0x20};   // BADC格式设置
const uint16_t UT_IREAL_BADC_GET[] = {0xF147, 0x0020};          // BADC格式获取
const uint8_t UT_IREAL_CDAB_SET[] = {0x20, 0x00, 0x47, 0xF1};   // CDAB格式设置
const uint16_t UT_IREAL_CDAB_GET[] = {0x2000, 0x47F1};          // CDAB格式获取

#endif /* _UNIT_TEST_H_ */

This header file mainly contains the following content:

  • Constant definition:
  • SERVER_IDINVALID_SERVER_IDDefined normal and illegal server IDs separately.
  • Modbus address and quantity definition:
  • Defined coil address, coil quantity, coil status dataset, input coil address, input coil quantity, input status dataset, register address, register quantity, maximum register quantity, register value, etc.
  • Special Address Definition:
  • Register addresses used in unit testing to trigger exceptions and special behaviors, such as invalid TID or slave device addresses, sleep addresses, etc.
  • Definition of Special Register Quantity:
  • Special Register Quantity Used for Error Response Testing.
  • Floating Point Test Value Definition:
  • defines a floating-point test value.
  • Size conversion array definition:
  • Different format array definitions for floating-point access testing in Modbus protocol. These definitions make unit testing related to the Modbus protocol more convenient and systematic.

These definitions make unit testing related to the Modbus protocol more convenient and systematic.

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.