原创 Modbus协议的寄存器操作

2007-9-17 11:37 7301 9 9 分类: MCU/ 嵌入式


Modbus Registers<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


Modbus的寄存器


Detailed Description


详细描述


 


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 If no error occurred. In this case a normal Modbus response is sent.


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


·   eMBErrorCode::MB_ENOREG If no such discrete inputs exists. In this case a ILLEGAL DATA ADDRESS exception frame 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 eMBRegHoldingCB



UCHAR * 


pucRegBuffer,


 


 


USHORT 


usAddress,


 


 


USHORT 


usNRegs,


 


 


eMBRegisterMode 


eMode


 



 



 



 


Callback function used if a Holding Register value is read or written by the protocol stack. The starting register address is given by usAddress and the last register is given by usAddress + usNRegs - 1.


如果协议栈需要对保持寄存器数值进行读写,将用到这个回调函数。通过usAddress给出起始寄存器地址,最后一个寄存器地址通过usAddress + usNRegs – 1得到。


Parameters: 参数



 


pucRegBuffer 


If the application registers values should be updated the buffer points to the new registers values. If the protocol stack needs to now the current values the callback function should write them into this buffer.


如果需要更新用户寄存器数值,这个缓冲区必须指向新的寄存器数值。如果协议栈想知道当前的数值,回调函数必须将当前值写入这个缓冲区。


 


usAddress 


The starting address of the register.


寄存器起始地址


 


usNRegs 


Number of registers to read or write.


读写寄存器的数量


 


eMode 


If eMBRegisterMode::MB_REG_WRITE the application register values should be updated from the values in the buffer. For example this would be the case when the Modbus master has issued an WRITE SINGLE REGISTER command. If the value eMBRegisterMode::MB_REG_READ the application should copy the current values into 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 can not supply values for registers within this range. In this case a ILLEGAL DATA ADDRESS exception frame 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 eMBRegInputCB



UCHAR * 


pucRegBuffer,


 


 


USHORT 


usAddress,


 


 


USHORT 


usNRegs


 



 



 



 


Callback function used if the value of a Input Register is required by the protocol stack. The starting register address is given by usAddress and the last register is given by usAddress + usNRegs - 1.


如果协议栈需要对输入寄存器进行操作,需要用到这个回调函数。起始寄存器的地址由usAddress给出,最后一个寄存器的地址是usAddress + usNRegs – 1


Parameters: 参数



 


pucRegBuffer 


A buffer where the callback function should write the current value of the modbus registers to.


回调函数将Modbus寄存器的当前值写入的缓冲区


 


usAddress 


The starting address of the register. Input registers are in the range 1 - 65535.


寄存器的起始地址,输入寄存器的地址范围是165535


 


usNRegs 


Number of registers the callback function must supply.


寄存器数量


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 can not supply values for registers within this range. In this case a ILLEGAL DATA ADDRESS exception frame 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.

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条