Introduction to FreeModbus Library and Explanation of Modbus Variable Addresses

freeFree Technical Resource

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

Introduction to FreeModbus Library and Explanation of Modbus Variable Addresses缩略图

Introduction to FreeModbus Library

FreeModbus is a simple and easy-to-use Modbus RTU library that allows multiple independent Modbus RTU slaves to run simultaneously. The characteristic of this library is that it directly uses the 0xxxx-4xxxx register space, without the need for manual handling of register mapping and other tedious issues, and can limit the maximum number of registers used. It provides a timer interface and a serial port transceiver interface, which can be configured according to hardware requirements, very in line with the conventional idea of using Modbus communication.

Introduction to FreeModbus Library and Explanation of Modbus Variable Addresses插图

Instructions for Use

The following are the basic steps for using the FreeModbus library:

  1. Build a slave machine1.1 Import Library: Add the FreeModbus library to your project and import the header file # include "mdrtuslave. h". 1.2 Creating Modbus RTU Slave:
   #include "mdrtuslave.h"

   #define SLAVE_ID 5
   #define BUAD_RATE 9600
   static ModbusRTUSlaveHandler mdhandler;

   static mdVOID popchar(ModbusRTUSlaveHandler handler, mdU8 c)
   {
      // 此处添加串口发送代码
   }

   static void ModbusInit()
   {
      struct ModbusRTUSlaveRegisterInfo info;
      info.slaveId = SLAVE_ID;
      info.usartBaudRate = BUAD_RATE;
      info.mdRTUPopChar = popchar;
      mdCreateModbusRTUSlave(&mdhandler, info);
   }

   ModbusInit();

1.3 Calling Heartbeat Function:

   #define TIMER_UTIME  100  //定时器周期为100us

   static void timer_handler(void)  //100us定时器回调函数
   {
      mdhandler->portRTUTimerTick(mdhandler, TIMER_UTIME);
   }

1.4 Receiving Data from Serial Port:

   static void usart_rec_handler(char *buf, size_t len)   //串口接收中断函数
   {
      for (size_t i = 0; i < len; i++)
      {
         mdhandler->portRTUPushChar(mdhandler, buf[i]);
      }
   }
  1. Writing and Reading Slave Register2.1 Reading Input Coil:
   mdBit bit;
   mdU32 addr = 0;
   mdSTATUS ret;
   ret = mdhandler->registerPool->mdReadInputCoil(mdhandler->registerPool, addr, &bit);   //读取输入线圈,设备地址为10001
   if (ret == mdFALSE)
   {
      printf("读取失败n");
   }

2.2 Reading Hold Register:

   mdU16 data;
   mdU32 addr = 0;
   mdSTATUS ret;
   ret = mdhandler->registerPool->mdReadHoldRegister(mdhandler->registerPool, addr, &data);    //读取保持寄存器,地址为40001
   if (ret == mdFALSE)
   {
      printf("读取失败n");
   }

2.3 Writing Input Coil:

   mdBit bit = mdHigh;
   mdU32 addr = 0;
   mdSTATUS ret;
   ret = mdhandler->registerPool->mdWriteInputCoil(mdhandler->registerPool, addr, bit);   //写入输入线圈,地址为10001,高电平
   if (ret == mdFALSE)
   {
      printf("写入失败n");
   }

2.4 Write to Keep Register:

   mdU16 data = 0x1234;
   mdU32 addr = 0;
   mdSTATUS ret;
   ret = mdhandler->registerPool->mdWriteHoldRegister(mdhandler->registerPool, addr, data);  //写入保持寄存器,地址为40001,大小为0x1234
   if (ret == mdFALSE)
   {
      printf("写入失败n");
   }

2.5 Other Register Operation APIs (Recommended Method):

   /*不推荐*/
    mdSTATUS (*mdReadBit)(RegisterPoolHandle handler,mdU32 addr,mdBit *bit);
    mdSTATUS (*mdWriteBit)(RegisterPoolHandle handler,mdU32 addr,mdBit bit);
    mdSTATUS (*mdReadBits)(RegisterPoolHandle handler,mdU32 addr,mdU32 len,mdBit *bits);
    mdSTATUS (*mdWriteBits)(RegisterPoolHandle handler,mdU32 addr,mdU32 len,mdBit *bits);
    mdSTATUS (*mdReadU16)(RegisterPoolHandle handler,mdU32 addr,mdU16 *data);
    mdSTATUS (*mdWriteU16)(RegisterPoolHandle handler,mdU32 addr,mdU16 data);
    mdSTATUS (*mdReadU16s)(RegisterPoolHandle handler,mdU32 addr,mdU32 len,mdU16 *data);
    mdSTATUS (*mdWriteU16s)(RegisterPoolHandle handler,mdU32 addr,mdU32 len,mdU16 *data);

   /*推荐*/
    mdSTATUS (*mdReadCoil)(RegisterPoolHandle handler, mdU32 addr, mdBit* bit);  //设备地址1~10000,addr=0 -> 设备1
    mdSTATUS (*mdReadCoils)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdBit* bits);
    mdSTATUS (*mdWriteCoil)(RegisterPoolHandle handler, mdU32 addr, mdBit bit);
    mdSTATUS (*mdWriteCoils)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdBit* bits);
    mdSTATUS (*mdReadInputCoil)(RegisterPoolHandle handler, mdU32 addr, mdBit* bit);   //设备地址10001~20000,addr=0 -> 设备10001
    mdSTATUS (*mdReadInputCoils)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdBit* bits);
    mdSTATUS (*mdWriteInputCoil)(RegisterPoolHandle handler, mdU32 addr, mdBit bit);
    mdSTATUS (*mdWriteInputCoils)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdBit* bits);
    mdSTATUS (*mdReadInputRegister)(RegisterPoolHandle handler, mdU32 addr, mdU16* data);//设备地址30001~40000,addr=0 -> 设备30001
    mdSTATUS (*mdReadInputRegisters)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdU16* data);
    mdSTATUS (*mdWriteInputRegister)(RegisterPoolHandle handler, mdU32 addr, mdU16 data);
    mdSTATUS (*mdWriteInputRegisters)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdU16* data);
    mdSTATUS (*mdReadHoldRegister)(RegisterPoolHandle handler, mdU32 addr, mdU16* data);//设备地址40001~50000,addr=0 -> 设备40001
    mdSTATUS (*mdReadHoldRegisters)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdU16* data);
    mdSTATUS (*mdWriteHoldRegister)(RegisterPoolHandle handler, mdU32 addr, mdU16 data);
    mdSTATUS (*mdWriteHoldRegisters)(RegisterPoolHandle handler, mdU32 addr, mdU32 len, mdU16* data);

There are other register operation APIs, such as mdReadCoil, mdReadInputCoil, mdReadInputRegister, etc., which are more in line with Modbus variable address specifications and can more easily read and write register values.

Modbus Variable Address

The Modbus variable address represents the position and type of different registers in Modbus communication. These addresses are part of the Modbus protocol and are used to specify registers to be read or written. The following are common Modbus variable addresses and their meanings:

  • 0xxxx: This is the coil register address, corresponding to Function Code 01 (read coil) and Function Code 05 (write single coil). They are used to control switch devices and are readable and writable.
  • 1xxxx: This is the address of the input coil register, corresponding to Function Code 02 (read input coil). They are used to represent input signals and are usually read-only.
  • 3xxxx: This is the input register address, corresponding to Function Code 04 (read input register). Each register represents a 16 bit unsigned integer, usually read-only.
  • 4xxxx: This is the address of the hold register, corresponding to Function Code 03 (read hold register), Function Code 06 (write single hold register), and Function Code 16 (write multiple hold registers). They are used to store device parameters, status, and other information, and are readable and writable.

These address types and function codes are the core components of Modbus communication, through which you can read and write different registers of the device, achieving monitoring and control of the device.

In summary, the FreeModbus library provides a convenient way to create Modbus RTU slaves and perform register read and write operations, while Modbus variable addresses define the types and functions of different registers, helping

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

《 “Introduction to FreeModbus Library and Explanation of Modbus Variable Addresses” 》 有 2 条Comments

Leave a Reply

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