电子大神的日记本,供应链专家的功夫茶盘,在这里记录、分享与共鸣。

登录以开始

Modbus协议的寄存器操作

==============================================================================

Modbus Registers

Modbus的寄存器

Detailed Description

详细描述

 #include "mb.h" 

The protocol stack does not internally allocate any memory for the registers. This makes the protocol stack very small and also usable on low end targets. In addition the values don't have to be in the memory and could for example be stored in a flash.
Whenever the protocol stack requires a value it calls one of the callback function with the register address and the number of registers to read as an argument. The application should then read the actual register values (for example the ADC voltage) and should store the result in the supplied buffer.
If the protocol stack wants to update a register value because a write register function was received a buffer with the new register values is passed to the callback function. The function should then use these values to update the application register values.

协议栈不在内部为寄存器分配空间。这就使得协议栈非常小并且适用于低端目标应用。并且Modbus寄存器的值不一定存储在内存中,而是可以存储在如flash之类的存储器中。当协议栈需要获取数值是,它通过调用回调函数来实现,该回调函数以寄存器地址和数量作为参数。应用程序将读实际的寄存器数值(如ADC电压),并且存储在特定的缓冲区中。

如果协议栈收到了一个写寄存器命令,将会执行寄存器更新操作,一个包含新寄存器数值的缓冲区会传给回调函数。这个回调函数将用这些值来更新应用寄存器数值。

Functions功能函数

eMBErrorCode

eMBRegInputCB (UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs)

输入寄存器回调函数

eMBErrorCode

eMBRegHoldingCB (UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode)

保持寄存器回调函数

eMBErrorCode

eMBRegCoilsCB (UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode)

线圈状态寄存器回调函数

eMBErrorCode

eMBRegDiscreteCB (UCHAR *pucRegBuffer, USHORT usAddress, USHORT usNDiscrete)

离散寄存器回调函数


Function Documentation功能函数文档

eMBErrorCode eMBRegCoilsCB

**( **

**UCHAR * **

pucRegBuffer**,**

** **

** **

**USHORT **

usAddress**,**

** **

** **

**USHORT **

usNCoils**,**

** **

** **

**eMBRegisterMode **

eMode

** **

**) **

** **

Callback function used if a Coil Register value is read or written by the protocol stack. If you are going to use this function you might use the functions xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.

如果协议栈需要对线圈状态寄存器进行读写,则需要调用回调函数。如果用户用到这个功能,用户可能用到xMBUtilSetBits( ) 和 xMBUtilGetBits( ) 来处理位域。

Parameters: 参数

pucRegBuffer

The bits are packed in bytes where the first coil starting at address usAddress is stored in the LSB of the first byte in the buffer pucRegBuffer. If the buffer should be written by the callback function unused coil values (I.e. if not a multiple of eight coils is used) should be set to zero.

位组成一个字节,起始寄存器对应的位处于该字节pucRegBuffer的最低位LSB。如果回调函数要写这个缓冲区,没有用到的线圈(例如不是8个一组的线圈状态)对应的位的数值必须设置位0。

usAddress

The first coil number.

第一个线圈地址

usNCoils

Number of coil values requested.

请求的线圈个数

eMode

If eMBRegisterMode::MB_REG_WRITE the application values should be updated from the values supplied in the buffer pucRegBuffer. If eMBRegisterMode::MB_REG_READ the application should store the current values in the buffer pucRegBuffer.

如果该参数为eMBRegisterMode::MB_REG_WRITE,用户的应用数值将从pucRegBuffer中得到更新。如果该参数为eMBRegisterMode::MB_REG_READ,用户需要将当前的应用数据存储在pucRegBuffer中。

Returns: 返回

The function must return one of the following error codes:

这个函数将返回如下的错误码:

·   eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal Modbus response is sent.

eMBErrorCode::MB_ENOERR如果没有错误发生。在这种情况下,发送一个正常的Modbus应答。

·   eMBErrorCode::MB_ENOREG If the application does not map an coils within the requested address range. In this case a ILLEGAL DATA ADDRESS is sent as a response.

eMBErrorCode::MB_ENOREG如果请求的地址范围中没有线圈状态寄存器,在这种情况下,发送一个ILLEGAL DATA ADDRESS应答。

·   eMBErrorCode::MB_ETIMEDOUT If the requested register block is currently not available and the application dependent response timeout would be violated. In this case a SLAVE DEVICE BUSY exception is sent as a response.

eMBErrorCode::MB_ETIMEDOUT如果请求的寄存器区当前无效,并且应用相关的应当超时。这种情况下,发送一个SLAVE DEVICE BUSY应答。

·   eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case a SLAVE DEVICE FAILURE exception is sent as a response.

eMBErrorCode::MB_EIO

如果一个其他错误发生。在这种情况下,发送一个SLAVE DEVICE FAILURE应答。

Examples: 例子

AT91SAM7X_ROWLEY/demo.c, AVR/demo.c, LINUX/demo.c, MCF5235/demo.c, MCF5235TCP/demo.c, MSP430/demo.c, STR71X/simple2.c, STR71XTCP/demo.c, WIN32/demo.cpp, and WIN32TCP/demo.cpp.

eMBErrorCode eMBRegDiscreteCB

**( **

**UCHAR * **

pucRegBuffer**,**

** **

** **

**USHORT **

usAddress**,**

** **

** **

**USHORT **

usNDiscrete

** **

**) **

** **

Callback function used if a Input Discrete Register value is read by the protocol stack.

If you are going to use his function you might use the functions xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.

Parameters:

pucRegBuffer

The buffer should be updated with the current coil values. The first discrete input starting at usAddress must be stored at the LSB of the first byte in the buffer. If the requested number is not a multiple of eight the remaining bits should be set to zero.

用当前的线圈数据更新这个寄存器,起始寄存器对应的位处于该字节pucRegBuffer的最低位LSB。如果回调函数要写这个缓冲区,没有用到的线圈(例如不是8个一组的线圈状态)对应的位的数值必须设置为0。

usAddress

The starting address of the first discrete input.

离散输入的起始地址

usNDiscrete

Number of discrete input values.

离散输入点数量

Returns:

The function must return one of the following error codes:

这个函数将返回如下的错误码:

·   eMBErrorCode::MB_ENOERR

博主
bpesun@163.com
bpesun's Blog
  skype bpesun