freemodbus详解文档格式.docx
《freemodbus详解文档格式.docx》由会员分享,可在线阅读,更多相关《freemodbus详解文档格式.docx(50页珍藏版)》请在冰豆网上搜索。
Thismoduledefinestheinterfacefortheapplication.ItcontainsthebasicfunctionsandtypesrequiredtousetheModbusprotocolstack.AtypicalapplicationwillwanttocalleMBInit()first.IfthedeviceisreadytoanswernetworkrequestsitmustthencalleMBEnable()toactivatetheprotocolstack.InthemainloopthefunctioneMBPoll()mustbecalledperiodically.ThetimeintervalbetweenpoolingdependsontheconfiguredModbustimeout.IfanRTOSisavailableaseparatetaskshouldbecreatedandthetaskshouldalwayscallthefunctioneMBPoll().
这个模块为应用定义了接口。
它包括了使用Modbus协议栈所必须的基本功能函数和类型。
一个典型的应用程序将会首先调用eMBInit()函数。
如果该设备需要回应网络请求,则其必须调用eMBEnable()函数来激活协议栈。
在主循环中,函数eMBPoll()将会被周期性调用。
两次调用的时间间隔决定于Modbus的超时时间设置。
如果使用了RTOS,用户则必须创建一个独立的任务,并且该任务必须一直调用函数eMBPoll()。
//InitializeprotocolstackinRTUmodeforaslavewithaddress10=0x0A
eMBInit(MB_RTU,0x0A,38400,MB_PAR_EVEN);
//EnabletheModbusProtocolStack.
eMBEnable(
);
for(;
;
)
{
//CallthemainpollingloopoftheModbusprotocolstack.
eMBPoll(
...
}
Defines定义
#define
MB_TCP_PORT_USE_DEFAULT
Enumerations枚举类型
enum
eMBMode
{
MB_RTU,
MB_ASCII,
MB_TCP
}
Modbus协议的模式:
RTU
,ASCII
和TCP
eMBRegisterMode
MB_REG_READ,
MB_REG_WRITE
Modbus寄存器的模式:
READ和WRITE
eMBErrorCode
MB_ENOERR,
MB_ENOREG,
MB_EINVAL,
MB_EPORTERR,
MB_ENORES,
MB_EIO,
MB_EILLSTATE,
MB_ETIMEDOUT
错误码:
没有错误,没有寄存器,无效,
,
,IO错误,无效
,超时
eMBParity
MB_PAR_NONE,
MB_PAR_ODD,
MB_PAR_EVEN
Modbus数据帧的校验类型:
无校验,偶校验,奇校验
Functions函数
eMBInit
(eMBMode
eMode,UCHARucSlaveAddress,UCHARucPort,ULONGulBaudRate,
eParity)
协议初始化函数
输入参数:
eMode,Modubus协议模式
UCHARucSlaveAddress,
子模块地址
UCHARucPort,
端口
ULONGulBaudRate,
波特率
eParity
串行数据的奇偶校验
eMBTCPInit
(USHORTusTCPPort)
如果使用了TCP协议,则用该函数进行初始化
eMBClose
(void)
关闭Modbus协议栈
eMBEnable
使能Modbus协议栈
eMBDisable
禁止Modbus协议栈
eMBPoll
周期性调用的协议处理函数
eMBSetSlaveID
(UCHARucSlaveID,BOOLxIsRunning,UCHARconst*pucAdditional,USHORTusAdditionalLen)
设置子模块ID
UCHARucSlaveID,
子模块ID
BOOLxIsRunning,
UCHARconst*pucAdditional,
USHORTusAdditionalLen
eMBRegisterCB
(UCHARucFunctionCode,pxMBFunctionHandlerpxHandler)
Modbus协议的寄存器回调函数
UCHARucFunctionCode,
功能码
pxMBFunctionHandlerpxHandler功能码对应的处理函数
DefineDocumentation文档
#defineMB_TCP_PORT_USE_DEFAULT
UsethedefaultModbusTCPport(502).
使用缺省的ModbusTCP移植
Examples:
MCF5235TCP/demo.c,
STR71XTCP/demo.c,and
WIN32TCP/demo.cpp.
EnumerationTypeDocumentation枚举类型定义
enum
eMBErrorCode
Errorcodesusedbyallfunctionintheprotocolstack.
在协议栈中所有函数使用的错误码
Enumerationvalues:
枚举值
MB_ENOERR
noerror.
没有错误
MB_ENOREG
illegalregisteraddress.
无效的寄存器地址
MB_EINVAL
illegalargument.
无效的参数
MB_EPORTERR
portinglayererror.
移植层错误
MB_ENORES
insufficientresources.
资源不足
MB_EIO
I/Oerror.I/O错误
MB_EILLSTATE
protocolstackinillegalstate.
协议栈状态错误
timeouterroroccurred.
超时错误
例子:
AT91SAM7X_ROWLEY/demo.c,
AVR/demo.c,
LINUX/demo.c,
MCF5235/demo.c,
MSP430/demo.c,
STR71X/simple2.c,
STR71XTCP/demo.c,
WIN32/demo.cpp,and
eMBMode
Modbusserialtransmissionmodes(RTU/ASCII).
Modubs串行传输模式
Modbusserialsupportstwotransmissionmodes.EitherASCIIorRTU.RTUisfasterbuthasmorehardwarerequirementsandrequiresanetworkwithalowjitter.ASCIIisslowerandmorereliableonslowerlinks(E.g.modems)
Modbus串行传输支持两种模式,ASCII或者是RTU。
RTU模式较快但是具有较高的硬件要求并且要求网络延迟要低。
ASCII模式较慢,但是在低速连接中更加可靠(如moderms)。
MB_RTU
RTUtransmissionmode.RTU传输模式
MB_ASCII
ASCIItransmissionmode.ASCII传输模式
TCPmode.TCP模式
eMBParity
Parityusedforcharactersinserialmode.
串行模式中字符的极性
Theparitywhichshouldbeappliedtothecharacterssentovertheseriallink.Pleasenotethatthisvaluesareactuallypassedtotheportinglayerandthereforenotallparitymodesmightbeavailable.
串行字符传输时应用的校验格式。
需要注意,这个值其实是传给移植层,并且不是所有的奇偶模式都能有效。
MB_PAR_NONE
Noparity.
无奇偶校验
MB_PAR_ODD
Oddparity.
偶校验
Evenparity.
奇校验
eMBRegisterMode
Ifregistershouldbewrittenorread.
寄存器是读还是写
Thisvalueispassedtothecallbackfunctionswhichsupporteitherreadingorwritingregistervalues.Writingmeansthattheapplicationregisterssh