free modbus详解.docx

上传人:b****5 文档编号:6686925 上传时间:2023-01-09 格式:DOCX 页数:50 大小:29.82KB
下载 相关 举报
free modbus详解.docx_第1页
第1页 / 共50页
free modbus详解.docx_第2页
第2页 / 共50页
free modbus详解.docx_第3页
第3页 / 共50页
free modbus详解.docx_第4页
第4页 / 共50页
free modbus详解.docx_第5页
第5页 / 共50页
点击查看更多>>
下载资源
资源描述

free modbus详解.docx

《free modbus详解.docx》由会员分享,可在线阅读,更多相关《free modbus详解.docx(50页珍藏版)》请在冰豆网上搜索。

free modbus详解.docx

freemodbus详解

freemodbus之协议模块

2007-12-2710:

43:

32  作者:

  来源:

freemodbus之Modbus协议配置   浏览次数:

844  文字大小:

【大】【中】【小】

简介:

DetailedDescription详细描述#include"mb.h"Thismoduledefinestheinterfacefortheapplication.ItcontainsthebasicfunctionsandtypesrequiredtousetheModbusprotocolstack.Aty...

DetailedDescription详细描述

 #include"mb.h"

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   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,UCHARucSlaveAddress,UCHARucPort,ULONGulBaudRate, eMBParity eParity)

协议初始化函数

输入参数:

eMBMode eMode,Modubus协议模式

UCHARucSlaveAddress, 子模块地址

UCHARucPort, 端口

ULONGulBaudRate, 波特率

eMBParity eParity 串行数据的奇偶校验

eMBErrorCode 

eMBTCPInit (USHORTusTCPPort)

如果使用了TCP协议,则用该函数进行初始化

eMBErrorCode 

eMBClose (void)

关闭Modbus协议栈

eMBErrorCode 

eMBEnable (void)

使能Modbus协议栈

eMBErrorCode 

eMBDisable (void)

禁止Modbus协议栈

eMBErrorCode 

eMBPoll (void)

周期性调用的协议处理函数

eMBErrorCode 

eMBSetSlaveID (UCHARucSlaveID,BOOLxIsRunning,UCHARconst*pucAdditional,USHORTusAdditionalLen)

设置子模块ID

输入参数:

UCHARucSlaveID, 子模块ID

BOOLxIsRunning,

UCHARconst*pucAdditional,

USHORTusAdditionalLen

eMBErrorCode 

eMBRegisterCB (UCHARucFunctionCode,pxMBFunctionHandlerpxHandler)

Modbus协议的寄存器回调函数

输入参数:

UCHARucFunctionCode, 功能码

pxMBFunctionHandlerpxHandler功能码对应的处理函数

DefineDocumentation文档

#defineMB_TCP_PORT_USE_DEFAULT   0

 

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. 协议栈状态错误

MB_ETIMEDOUT 

timeouterroroccurred. 超时错误

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.

enum eMBMode

 

Modbusserialtransmissionmodes(RTU/ASCII).

Modubs串行传输模式

Modbusserialsupportstwotransmissionmodes.EitherASCIIorRTU.RTUisfasterbuthasmorehardwarerequirementsandrequiresanetworkwithalowjitter.ASCIIisslowerandmorereliableonslowerlinks(E.g.modems)

Modbus串行传输支持两种模式,ASCII或者是RTU。

RTU模式较快但是具有较高的硬件要求并且要求网络延迟要低。

ASCII模式较慢,但是在低速连接中更加可靠(如moderms)。

Enumerationvalues:

MB_RTU 

RTUtransmissionmode.RTU传输模式

MB_ASCII 

ASCIItransmissionmode.ASCII传输模式

MB_TCP 

TCPmode.TCP模式

enum eMBParity

 

Parityusedforcharactersinserialmode.

串行模式中字符的极性

Theparitywhichshouldbeappliedtothecharacterssentovertheseriallink.Pleasenotethatthisvaluesareactuallypassedtotheportinglayerandthereforenotallparitymodesmightbeavailable.

串行字符传输时应用的校验格式。

需要注意,这个值其实是传给移植层,并且不是所有的奇偶模式都能有效。

Enumerationvalues:

MB_PAR_NONE 

Noparity.  无奇偶校验

MB_PAR_ODD 

Oddparity. 偶校验

MB_PAR_EVEN 

Evenparity. 奇校验

enum eMBRegisterMode

 

Ifregistershouldbewrittenorread.

寄存器是读还是写

Thisvalueispassedtothecallbackfunctionswhichsupporteitherreadingorwritingregistervalues.Writingmeansthattheapplicationregistersshouldbeupdatedandreadingmeansthatthemodbusprotocolstackneedstoknowthecurrentregistervalues.

值将传给支持读或者写寄存器的回调函数。

写意味着应用寄存器更新,读表示Modbus协议栈需要知道当前寄存器的数值。

Seealso:

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

Enumerationvalues:

MB_REG_READ 

Readregistervaluesandpasstoprotocolstack. 读寄存器数值并且传给协议栈

MB_REG_WRITE 

Updateregistervalues. 更新寄存器数值

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.

FunctionDocumentation

eMBErrorCode eMBClose

( 

void 

 

 ) 

 

 

Releaseresourcesusedbytheprotocolstack.

释放协议栈使用的资源。

ThisfunctiondisablestheModbusprotocolstackandreleaseallhardwareresources.Itmustonlybecalledwhentheprotocolstackisdisabled.

该函数禁止Modbus协议栈并且释放所有的硬件资源。

只有当协议栈被禁止了,才能调用这个函数。

Note:

 注意

Noteallportsimplementthisfunction.AportwhichwantstogetancallbackmustdefinethemacroMB_PORT_HAS_CLOSEto1.

注意所有的移植都要实现这个函数。

需要获取回调

Returns:

Iftheresourceswherereleaseditreturn eMBErrorCode:

:

MB_ENOERR.Iftheprotocolstackisnotinthedisabledstateitreturns 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 

 

 ) 

 

 

DisabletheModbusprotocolstack.

禁止Modbus协议栈。

ThisfunctiondisablesprocessingofModbusframes.

这个函数禁止处理Modbus帧。

Returns:

 返回

Iftheprotocolstackhasbeendisableditreturns eMBErrorCode:

:

MB_ENOERR.Ifitwasnotintheenabledstateitreturns eMBErrorCode:

:

MB_EILLSTATE.

Examples:

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

eMBErrorCode eMBEnable

( 

void 

 

 ) 

 

 

EnabletheModbusprotocolstack.

使能Modbus协议栈

ThisfunctionenablesprocessingofModbusframes.Enablingtheprotocolstackisonlypossibleifitisinthedisabledstate.

本函数使能Modbus帧的处理。

Returns:

Iftheprotocolstackisnowinthestateenableditreturns eMBErrorCode:

:

MB_ENOERR.Ifitwasnotinthedisabledstateitreturn 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

( 

eMBMode 

eMode,

 

 

UCHAR 

ucSlaveAddress,

 

 

UCHAR 

ucPort,

 

 

ULONG 

ulBaudRate,

 

 

eMBParity 

eParity

 

) 

 

 

InitializetheModbusprotocolstack.

初始化Modbus协议栈

ThisfunctionsinitializestheASCIIorRTUmoduleandcallstheinitfunctionsoftheportinglayertopreparethehardware.PleasenotethatthereceiverisstilldisabledandnoModbusframesareprocesseduntil eMBEnable() hasbeencalled.

该函数初始化为ASCII或者RTU模式,并且调用移植层的初始化函数来准备硬件。

需要注意的是,接受依然是禁止的,并且Modbus数据帧都不会得到处理,除非eMBEnable()被调用。

Parameters:

 

eMode 

IfASCIIorRTUmodeshouldbeused.Modbus模式

 

ucSlaveAddress 

Theslaveaddress.Onlyframessenttothisaddressortothebroadcastaddressareprocessed. 子模块地址。

只有发向本模块地址或者广播地址的数据帧能得到处理。

 

ucPort 

Theporttouse.E.g.1forCOM1onwindows.Thisvalueisplatformdependentandsomeportssimplychoosetoignoreit.

使用的端口。

该值是硬件相关的,许多移植都会选择忽略该参数。

 

ulBaudRate 

Thebaudrate.E.g.19200.Supportedbaudratesdependontheportinglayer. 波特率,所支持的波特率决定于移植层。

 

eParity 

Parityusedforserialtransmission.串行传输使用的校验。

Returns:

Ifnoerroroccursthefunctionreturns eMBErrorCode:

:

MB_ENOERR.Theprotocolistheninthedisabledstateandreadyforactivationbycalling eMBEnable().Otherwiseoneofthefollowingerrorcodesisreturned:

∙   eMBErrorCode:

:

MB_EINVAL Iftheslaveaddresswasnotvalid.Validslaveaddressesareintherange1-247.

∙   eMBErrorCode:

:

MB_EPORTERR IFtheportinglayerreturnedanerror.

如果没有错误,这个函数返回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 

 

 ) 

 

 

ThemainpoolingloopoftheModbusprotocolstack.

Modbus协议栈的主轮询函数。

Thisfunctionmustbecalledperiodically.ThetimerintervalrequiredisgivenbytheapplicationdependentModbusslavetimeout.InternallythefunctioncallsxMBPortEventGet()andwaitsforaneventfromthereceiverortransmitterstatemachines.

该函数必须被周期性调用。

两次调用的时间间隔决定于Modbus的超时时间设置。

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

Returns:

Iftheprotocolstackisnotintheenabledstatethefunctionreturns eMBErrorCode:

:

MB_EILLSTATE.Otherwiseitreturns eMB

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1