#include "mb.h"
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().
// 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 } |
enum | eMBRegisterMode { MB_REG_READ, MB_REG_WRITE } |
enum | eMBErrorCode { MB_ENOERR, MB_ENOREG, MB_EINVAL, MB_EPORTERR, MB_ENORES, MB_EIO, MB_EILLSTATE, MB_ETIMEDOUT } |
enum | eMBParity { MB_PAR_NONE, MB_PAR_ODD, MB_PAR_EVEN } |
Functions | |
eMBErrorCode | eMBInit (eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity) |
eMBErrorCode | eMBTCPInit (USHORT usTCPPort) |
eMBErrorCode | eMBClose (void) |
eMBErrorCode | eMBEnable (void) |
eMBErrorCode | eMBDisable (void) |
eMBErrorCode | eMBPoll (void) |
eMBErrorCode | eMBSetSlaveID (UCHAR ucSlaveID, BOOL xIsRunning, UCHAR const *pucAdditional, USHORT usAdditionalLen) |
eMBErrorCode | eMBRegisterCB (UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler) |
|
Use the default Modbus TCP port (502).
|
|
Errorcodes used by all function in the protocol stack.
|
|
Modbus serial transmission modes (RTU/ASCII). 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) |
|
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 parity modes might be available. |
|
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.
|
|
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.
|
|
Disable the Modbus protocol stack. This function disables processing of Modbus frames.
|
|
Enable the Modbus protocol stack. This function enables processing of Modbus frames. Enabling the protocol stack is only possible if it is in the disabled state.
|
|
Initialize the Modbus protocol stack. 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.
|
|
The main pooling loop of the Modbus protocol stack. 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.
|
|
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.
|
|
Configure the slave id of the device. 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 ).
|
|
Initialize the Modbus protocol stack for Modbus TCP. This function initializes the Modbus TCP Module. Please note that frame processing is still disabled until eMBEnable( ) is called.
|