free modbus详解

更新时间:2024-04-08 20:26:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

freemodbus之协议模块

2007-12-27 10:43:32 作者: 来源:freemodbus之Modbus协议配置 浏览次数:844 文字大

小:【大】【中】【小】

简介:Detailed Description详细描述 #include \This module defines the interface for the application. It contains the basic functions and types required to use the Modbus protocol stack. A ty ...

Detailed Description详细描述

#include \

This module defines the interface for the application. It contains the basic functions and types required to use the Modbus protocol stack. A typical application will want to call eMBInit() first. If the device is ready to answer network requests it must then call eMBEnable() to activate the protocol stack. In the main loop the function eMBPoll() must be called periodically. The time interval between pooling depends on the configured Modbus timeout. If an RTOS is available a separate task should be created and the task should always call the function eMBPoll().

这个模块为应用定义了接口。它包括了使用Modbus协议栈所必须的基本功能函数和类型。一个典型的应用程序将会首先调用eMBInit()函数。如果该设备需要回应网络请求,则其必须调用eMBEnable()函数来激活协议栈。在主循环中,函数eMBPoll()将会被周期性调用。两次调用的时间间隔决定于Modbus的超时时间设置。如果使用了RTOS,用户则必须创建一个独立的任务,并且该任务必须一直调用函数eMBPoll()。

// Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A

eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN ); // Enable the Modbus Protocol Stack. eMBEnable( ); for( ;; ) {

// Call the main polling loop of the Modbus protocol stack. eMBPoll( ); ... }

Defines定义

#define MB_TCP_PORT_USE_DEFAULT 0 Enumerations枚举类型

enum eMBMode { MB_RTU, MB_ASCII, MB_TCP } Modbus协议的模式:RTU ,ASCII 和TCP enum eMBRegisterMode { MB_REG_READ, MB_REG_WRITE } Modbus寄存器的模式:READ和WRITE enum eMBErrorCode { MB_ENOERR, MB_ENOREG, MB_EINVAL, MB_EPORTERR, MB_ENORES, MB_EIO, MB_EILLSTATE, MB_ETIMEDOUT } 错误码:没有错误,没有寄存器,无效, , ,IO错误,无效 ,超时 enum eMBParity { MB_PAR_NONE, MB_PAR_ODD, MB_PAR_EVEN } Modbus数据帧的校验类型:无校验,偶校验,奇校验 Functions函数

eMBErrorCode eMBInit (eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity) 协议初始化函数 输入参数: eMBMode eMode, Modubus协议模式 UCHAR ucSlaveAddress, 子模块地址 UCHAR ucPort, 端口 ULONG ulBaudRate, 波特率 eMBParity eParity 串行数据的奇偶校验 eMBErrorCode eMBTCPInit (USHORT usTCPPort) 如果使用了TCP协议,则用该函数进行初始化 eMBErrorCode eMBClose (void) 关闭Modbus协议栈 eMBErrorCode eMBEnable (void) 使能Modbus协议栈 eMBErrorCode eMBDisable (void) 禁止Modbus协议栈 eMBErrorCode eMBPoll (void) 周期性调用的协议处理函数 eMBErrorCode eMBSetSlaveID (UCHAR ucSlaveID, BOOL xIsRunning, UCHAR const *pucAdditional, USHORT usAdditionalLen) 设置子模块ID 输入参数: UCHAR ucSlaveID, 子模块ID BOOL xIsRunning, UCHAR const *pucAdditional, USHORT usAdditionalLen eMBErrorCode eMBRegisterCB (UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler) Modbus协议的寄存器回调函数 输入参数: UCHAR ucFunctionCode, 功能码 pxMBFunctionHandler pxHandler功能码对应的处理函数 Define Documentation文档 #define MB_TCP_PORT_USE_DEFAULT 0 Use the default Modbus TCP port (502). 使用缺省的ModbusTCP移植

Examples:

MCF5235TCP/demo.c, STR71XTCP/demo.c, and WIN32TCP/demo.cpp.

Enumeration Type Documentation枚举类型定义 enum eMBErrorCode Errorcodes used by all function in the protocol stack.

在协议栈中所有函数使用的错误码 Enumeration values: 枚举值

MB_ENOERR MB_ENOREG MB_EINVAL MB_ENORES MB_EIO

no error. 没有错误

illegal register address. 无效的寄存器地址 illegal argument. 无效的参数 insufficient resources. 资源不足 I/O error. I/O错误 误

MB_ETIMEDOUT timeout error occurred. 超时错误

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.

MB_EPORTERR porting layer error. 移植层错误

MB_EILLSTATE protocol stack in illegal state. 协议栈状态错

enum eMBMode Modbus serial transmission modes (RTU/ASCII). Modubs串行传输模式

Modbus serial supports two transmission modes. Either ASCII or RTU. RTU is faster but has more hardware requirements and requires a network with a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)

Modbus串行传输支持两种模式,ASCII或者是RTU。RTU模式较快但是具有较高的硬件要求并且要求网络延迟要低。ASCII模式较慢,但是在低速连接中更加可靠(如moderms)。 Enumeration values:

MB_RTU RTU transmission mode. RTU传输模式 MB_ASCII ASCII transmission mode. ASCII传输模式 MB_TCP TCP mode. TCP模式

enum eMBParity Parity used for characters in serial mode. 串行模式中字符的极性

The parity which should be applied to the characters sent over the serial link. Please note that this values are actually passed to the porting layer and therefore not all pari

ty modes might be available.

串行字符传输时应用的校验格式。需要注意,这个值其实是传给移植层,并且不是所有的奇偶模式都能有效。 Enumeration values:

MB_PAR_NONE No parity. 无奇偶校验 MB_PAR_ODD Odd parity. 偶校验 MB_PAR_EVEN Even parity. 奇校验

enum eMBRegisterMode If register should be written or read. 寄存器是读还是写

This value is passed to the callback functions which support either reading or writing register values. Writing means that the application registers should be updated and reading means that the modbus protocol stack needs to know the current register values.

值将传给支持读或者写寄存器的回调函数。写意味着应用寄存器更新,读表示Modbus协议栈需要知道当前寄存器的数值。

See also:

eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and eMBRegInputCB( ).

Enumeration values:

MB_REG_READ Read register values and pass to protocol stack. 读寄存器

数值并且传给协议栈

MB_REG_WRITE Update register values. 更新寄存器数值

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.

Function Documentation eMBErrorCode eMBClose( void ) Release resources used by the protocol stack.

释放协议栈使用的资源。

This function disables the Modbus protocol stack and release all hardware resources. It must only be called when the protocol stack is disabled.

该函数禁止Modbus协议栈并且释放所有的硬件资源。只有当协议栈被禁止了,才能调用这个函数。 Note: 注意

Note all ports implement this function. A port which wants to get an callback must define the macro MB_PORT_HAS_CLOSE to 1. 注意所有的移植都要实现这个函数。需要获取回调

Returns:

If the resources where released it return eMBErrorCode::MB_ENOERR. If the protocol stack is not in the disabled state it returns eMBErrorCode::MB_EILLSTATE.

如果资源释放了,该函数返回eMBErrorCode::MB_ENOERR。如果协议栈不是处于禁止状态,它返回eMBErrorCode::MB_EILLSTATE。

Examples:

LINUX/demo.c, MCF5235TCP/demo.c, STR71XTCP/demo.c, WIN32/demo.cpp, and WIN32TCP/demo.cpp.

eMBErrorCode eMBDisable ( void ) Disable the Modbus protocol stack. 禁止Modbus协议栈。

This function disables processing of Modbus frames. 这个函数禁止处理Modbus帧。 Returns: 返回

If the protocol stack has been disabled it returns eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns eMBErrorCode::MB_EILLSTATE.

Examples:

LINUX/demo.c, MCF5235TCP/demo.c, STR71XTCP/demo.c, WIN32/demo.cpp, and WIN32TCP/demo.cpp.

eMBErrorCode eMBEnable ( void )

Enable the Modbus protocol stack. 使能Modbus协议栈

This function enables processing of Modbus frames. Enabling the protocol stack is onl

y possible if it is in the disabled state. 本函数使能Modbus帧的处理。 Returns:

If the protocol stack is now in the state enabled it returns eMBErrorCode::MB_ENOERR. If it was not in the disabled state it return eMBErrorCode::MB_EILLSTATE.

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 eMBInit

(eMBMo de

eMode,

UCHA ucSlaveAddress, R

UCHA ucPort, R

ULON ulBaudRate, G

eMBParity

eParity

)

Initialize the Modbus protocol stack. 初始化Modbus协议栈

This functions initializes the ASCII or RTU module and calls the init functions of the porting layer to prepare the hardware. Please note that the receiver is still disabled and no Modbus frames are processed until eMBEnable( ) has been called.

该函数初始化为ASCII或者RTU模式,并且调用移植层的初始化函数来准备硬件。需要注意的是,接受依然是禁止的,并且Modbus数据帧都不会得到处理,除非eMBEnable( )被调用。

Parameters:

eMode If ASCII or RTU mode should be used. Modbus模式

ucSlaveThe slave address. Only frames sent to this address or to the broadAddrescast address are processed. 子模块地址。只有发向本模块地址或者广播s

地址的数据帧能得到处理。

m dependent and some ports simply choose to ignore it. 使用的端口。该值是硬件相关的,许多移植都会选择忽略该参数。

ucPort The port to use. E.g. 1 for COM1 on windows. This value is platfor

ulBaudThe baudrate. E.g. 19200. Supported baudrates depend on the portinRate g layer. 波特率,所支持的波特率决定于移植层。 eParity Parity used for serial transmission.串行传输使用的校验。

Returns:

If no error occurs the function returns eMBErrorCode::MB_ENOERR. The protocol is then in the disabled state and ready for activation by calling eMBEnable( ). Otherwise one of the following error codes is returned:

? eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid sl

ave addresses are in the range 1 - 247.

? eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. 如果没有错误,这个函数返回eMBErrorCode::MB_ENOERR。协议栈将处于禁止状态,可以通过调用eMBEnable( )来激活。否则,将返回下列的一个错误: ? eMBErrorCode::MB_EINVAL 如果子模块地址无效。 ? eMBErrorCode::MB_EPORTERR 如果移植层返回一个错误。

Examples:

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

eMBErrorCode eMBPoll ( void ) The main pooling loop of the Modbus protocol stack. Modbus协议栈的主轮询函数。

This function must be called periodically. The timer interval required is given by the application dependent Modbus slave timeout. Internally the function calls xMBPortEventGet() and waits for an event from the receiver or transmitter state machines.

该函数必须被周期性调用。两次调用的时间间隔决定于Modbus的超时时间设置。在函

数内部调用了xMBPortEventGet()函数来等待接受或者发送状态机发出的事件。

Returns:

If the protocol stack is not in the enabled state the function returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns eMBErrorCode::MB_ENOERR. 如果协议栈没有被使能,这个函数返回eMBErrorCode::MB_EILLSTATE.,否则返回eMBErrorCode::MB_ENOERR.

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 eM(UCHAR BRegisterCB ucFunctionCode, pxMBFunctiopxHandler nHandler ) Registers a callback handler for a given function code. 为给定的功能码注册一个回调处理函数。

This function registers a new callback handler for a given function code. The callback handler supplied is responsible for interpreting the Modbus PDU and the creation of an appropriate response. In case of an error it should return one of the possible Modbus exceptions which results in a Modbus exception frame sent by the protocol stack. 这个函数为一个给定的功能代码注册一个新的回调函数。提供的回调函数负责解释Modbus PDU并且创建一个合适的应答。当错误发生时,这个函数会返回一个Modbus异常,该异常是由协议栈发送的Modbus异常。 Parameters: 参数

ucFunctThe Modbus function code for which this handler should be registers.ionCod Valid function codes are in the range 1 to 127. e ler

注册的功能码,有效的功能码范围是1到127

received. If NULL a previously registered function handler for this function code is removed.

功能码对应的回调函数。如果为空,一个以前定义功能码回调函数将被去掉。

Returns: 返回

eMBErrorCode::MB_ENOERR if the handler has been installed. If no more resources are available it returns eMBErrorCode::MB_ENORES. In this case the values in mbconfig.h should be adjusted. If the argument was not valid it returns eMBErrorCode::MB_EINVAL.

eMBErrorCode::MB_ENOERR 如果回调函数正常安装。如果资源不足,将返回 eMBErrorCode::MB_ENORES. 在这种情况下,在 mbconfig.h 中定义的数值必须进行修改。如果参数无效,函数将返回 eMBErrorCode::MB_EINVAL.

eMBErrorCode eM(UCHAR pxHandThe function handler which should be called in case such a frame is

BSetSlaveID BOOL ucSlaveID, xIsRunning, UCHAR c pucAdditional, onst * USHORT usAdditionalLen ) Configure the slave id of the device.

设置设备的ID

This function should be called when the Modbus function Report Slave ID is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).

当使用Modbus的Report Slave ID 功能时,这个函数将会被调用(通过在mbconfig.h 中定义MB_FUNC_OTHER_REP_SLAVEID_ENABLED)。 Parameters:

ucSlaveID Values is returned in the Slave ID byte of the Report Slave ID res

ponse.

xIsRunning If TRUE the Run Indicator Status byte is set to 0xFF. otherwise t

he Run Indicator Status is 0x00.

pucAdditionValues which should be returned in the Additional bytes of the Real usAdditionalLen

Returns:

If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise it returns eMBErrorCode::MB_ENOERR.

port Slave ID response.

Length of the buffer pucAdditonal.

Examples:

AVR/demo.c, LINUX/demo.c, MCF5235/demo.c, and WIN32/demo.cpp.

eMBErrorCode eMBTCPInit ( USHORT usTCPPort ) Initialize the Modbus protocol stack for Modbus TCP. Modbus TCP的协议初始化

This function initializes the Modbus TCP Module. Please note that frame processing is still disabled until eMBEnable( ) is called.

该函数初始化Modbus TCP 模块。注意,帧处理在eMBEnable()函数调用之前是禁止掉的。 Parameters: 参数

usTCPPort The TCP port to listen on. 监听的TCP端口

Returns: 返回

If the protocol stack has been initialized correctly the function returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error codes is returned:

? eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid sl

ave addresses are in the range 1 - 247.

? eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. 如果协议栈初始化正确,函数返回MB_ENOERR。否则,函数会返回如下的错误码:

s

eMoIf eMBRegisterMode::MB_REG_WRITE the application register values shde ould be updated from the values in the buffer. For example this would b

e 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 pucRegBuffe

r.

如果该参数为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 M

odbus response is sent.

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

? eMBErrorCode::MB_ENOREG If the application can not supply values for r

egisters 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 curre

ntly 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.

eMBErrorCod(UCR * US HOusAddress, RT USusNRegs e eMBRegInpu HApucRegBuffer, tCB HORT ) 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给出,最后一个寄存器的地址是Parameters: 参数

pucRegBuA buffer where the callback function should write the current valueffer

of the modbus registers to.

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

usAddresThe starting address of the register. Input registers are in the rangs

e 1 - 65535.

寄存器的起始地址,输入寄存器的地址范围是1!65535。

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 fo

r 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 curre

ntly 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应答。

usAddress + usNRegs – 1。

freemodbus之协议配置

2007-12-27 10:42:48 作者: 来源:http://blog.ednchina.com/bpesun/17709/category.aspx 浏览次数:517 文字大小:【大】【中】【小】

简介:Modbus Configuration Modbus配置 Detailed Description 详细描述 Most modules in the protocol stack are completly optional and can be excluded. This is specially important if target resources a ...

Modbus Configuration

Modbus配置

Detailed Description

详细描述

Most modules in the protocol stack are completly optional and can be excluded. This is specially important if target resources are very small and program memory space should be saved.

All of these settings are available in the file mbconfig.h

协议栈中大多数模块是可选的并且可以被包含。这对于目标资源有限的应用非常重要,在这种应用中,需要节省程序存储空间。 所有的这些设置都在mbconfig.h中进行。

Defines

定义 #define MB_ASCII_ENABLED ( 1 ) #define MB_RTU_ENABLED ( 1 ) #define MB_TCP_ENABLED ( 0 ) #define MB_ASCII_TIMEOUT_SEC ( 1 ) #define MB_FUNC_HANDLERS_MAX ( 16 ) #define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 ) #define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( 1 ) #define MB_FUNC_READ_INPUT_ENABLED ( 1 ) #define MB_FUNC_READ_HOLDING_ENABLED ( 1 ) #define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 ) #define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 ) #define MB_FUNC_READ_COILS_ENABLED ( 1 ) #define MB_FUNC_WRITE_COIL_ENABLED ( 1 ) #define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 ) #define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 ) #define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 ) Define Documentation #define MB_ASCII_ENABLED ( 1 ) If Modbus ASCII support is enabled.

Modbus ASCII 协议支持

#define MB_ASCII_TIMEOUT_SEC ( 1 ) The character timeout value for Modbus ASCII. Modbus ASCII协议中字符超时值

The character timeout value is not fixed for Modbus ASCII and is therefore a configur

ation option. It should be set to the maximum expected delay time of the network. Modbus ASCII协议中的字符超时时间不是一个固定值,因此是一个可以配置的值。这个值可设置为网络中的最大的延迟时间。

#define MB_FUNC_HANDLERS_MAX ( 16 ) Maximum number of Modbus functions codes the protocol stack should support. Modbus协议栈支持的功能码的最多值

The maximum number of supported Modbus functions must be greater than the sum

of all enabled functions in this file and custom function handlers. If set to small adding more functions will fail.

这个最大值要大于所有使能的功能函数和用户功能处理函数的总和。如果值设小了,如果增加功能处理就会失败。

#define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 ) Number of bytes which should be allocated for the Report Slave ID command. 为Report Slave ID命令分配的字节数目

This number limits the maximum size of the additional segment in the report slave id ly used if MB_FUNC_OTHER_REP_SLAVEID_ENABLED is set to 1.

这个数值限制了在执行report slave ID时候附加信息的长度。参考eMBSetSlaveID( )获取信息来确定需要如何设置这个值。

#define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( 1 )

function. See eMBSetSlaveID( ) for more information on how to set this value. It is on

If the Report Slave ID function should be enabled.

Report Slave ID 功能使能

#define MB_FUNC_READ_COILS_ENABLED ( 1 ) If the Read Coils function should be enabled.

读线圈状态功能使能

#define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 ) If the Read Discrete Inputs function should be enabled.

读离散输入功能使能

#define MB_FUNC_READ_HOLDING_ENABLED ( 1 ) If the Read Holding Registers function should be enabled.

读保持寄存器功能使能

#define MB_FUNC_READ_INPUT_ENABLED ( 1 ) If the Read Input Registers function should be enabled.

读输入寄存器功能使能

#define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 ) If the Read/Write Multiple Registers function should be enabled.

读写多个寄存器功能使能

#define MB_FUNC_WRITE_COIL_ENABLED ( 1 ) If the Write Coils function should be enabled.

写线圈状态功能使能

#define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 ) If the Write Single Register function should be enabled.

写单个寄存器功能使能

#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 ) If the Write Multiple Coils function should be enabled.

写多高线圈功能使能

#define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 ) If the Write Multiple registers function should be enabled.

写多高寄存器功能使能

#define MB_RTU_ENABLED ( 1 ) If Modbus RTU support is enabled.

Modbus RTU支持使能

#define MB_TCP_ENABLED ( 0 ) If Modbus TCP support is enabled.

Modbus TCP支持使能

Freemodbus之工具函数 2007-12-27 10:33:07 作者: 来源:http://blog.ednchina.com/bpesun/17709/category.aspx 浏览次数:433 文字大小:【大】【中】【小】 简介:Utilities 工具 Detailed Description 详细描述 This module contains some utility functions which can be used by the application. It includes some special functions for working with bitfie ... Utilities 工具 Detailed Description 详细描述 This module contains some utility functions which can be used by the application. It includes some special functions for working with bitfields backed by a character array buffer. 这个模块包括应用中使用的一些功能函数。这些功能函数包括一些特殊的通过字符数组缓冲方式对位域进行处理的功能

Functions

void xMBUtilSetBits (UCHAR *ucByteBuf, USHORT usBitOffset, UCHAR ucNBits, UCHAR ucValues) UCHAR xMBUtilGetBits (UCHAR *ucByteBuf, USHORT usBitOffset, UCHAR ucNBits) Function Documentation 函数文档 UCHAR xMBUtilGet(UCHAR Bits * ucByteBuf, USHOR usBitOffset, T UCHAR ucNBits ) Function to read bits in a byte buffer. 在字节缓冲中读取位

This function is used to extract up bit values from an array. Up to eight bit values ca

n be extracted in one step.

这个函数从一个字节中来获取一个位的值。一步最多可以获取8个位的值。 Parameters: 参数

ucByteBuf A buffer where the bit values are stored.

存储位的缓冲字节

usBitOffseThe starting address of the bits to set. The first bit has the offsett

0.

位的起始地址,第一个位的地址偏移量是0

ucNBits Number of bits to modify. The value must always be smaller than

8.

需要修改的位的数量。该值要小于8。

UCHAR ucBits[2] = {0, 0}; UCHAR ucResult;

// Extract the bits 3 - 10.

ucResult = xMBUtilGetBits( ucBits, 3, 8 );

void xMBUtilSet(UCHAR * ucByteBuf, Bits USHORT usBitOffset, UCHAR ucNBits, UCHAR ucValues ) Function to set bits in a byte buffer. 设置一个字节缓冲的位

This function allows the efficient use of an array to implement bitfields. The array used for storing the bits must always be a multiple of two bytes. Up to eight bits can be set or cleared in one operation.

这个函数可以实现有效处理位域的功能。存储位域的数值必须是两个字节。一次操作最多可处理8位。 Parameters:

ucByA buffer where the bit values are stored. Must be a multiple of 2 byteteBus. No length checking is performed and if usBitOffset / 8 is greater thaf

n the size of the buffer memory contents is overwritten.

位存储的缓冲区。必须是2个字节。 usBitOffset

The starting address of the bits to set. The first bit has the offset 0. 位设置的起始地址,第一个位的偏移为0。

ucNNumber of bits to modify. The value must always be smaller than 8. Bits 需要修改的位的数量。该值必须小于8。

ucVaThew new values for the bits. The value for the first bit starting at usBlues itOffset is the LSB of the value ucValues

位的新值。在usBitOffset中的第一位的值是ucValues的最低有效位。

ucBits[2] = {0, 0};

// Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1) xMBUtilSetBits( ucBits, 4, 1, 1 );

// Set bit 7 to 1 and bit 8 to 0. xMBUtilSetBits( ucBits, 7, 2, 0x01 );

// Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A; xMBUtilSetBits( ucBits, 8, 8, 0x5A);

本文来源:https://www.bwwdw.com/article/fkgr.html

Top