Trying to make Modbus code into a lib

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Modbus

Enumerations

enum  eMBMode { MB_RTU, MB_ASCII, MB_TCP }
 

Modbus serial transmission modes (RTU/ASCII).

More...
enum  eMBRegisterMode { MB_REG_READ, MB_REG_WRITE }
 

If register should be written or read.

More...
enum  eMBErrorCode {
  MB_ENOERR, MB_ENOREG, MB_EINVAL, MB_EPORTERR,
  MB_ENORES, MB_EIO, MB_EILLSTATE, MB_ETIMEDOUT
}
 

Errorcodes used by all function in the protocol stack.

More...
enum  eMBParity { MB_PAR_NONE, MB_PAR_ODD, MB_PAR_EVEN }
 

Parity used for characters in serial mode.

More...

Functions

eMBErrorCode eMBInit (eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity)
 Initialize the Modbus protocol stack.
eMBErrorCode eMBTCPInit (USHORT usTCPPort)
 Initialize the Modbus protocol stack for Modbus TCP.
eMBErrorCode eMBClose (void)
 Release resources used by the protocol stack.
eMBErrorCode eMBEnable (void)
 Enable the Modbus protocol stack.
eMBErrorCode eMBDisable (void)
 Disable the Modbus protocol stack.
eMBErrorCode eMBPoll (void)
 The main pooling loop of the Modbus protocol stack.
eMBErrorCode eMBSetSlaveID (UCHAR ucSlaveID, BOOL xIsRunning, UCHAR const *pucAdditional, USHORT usAdditionalLen)
 Configure the slave id of the device.
eMBErrorCode eMBRegisterCB (UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler)
 Registers a callback handler for a given function code.

Detailed Description

 #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(  );
     ...
 }

Enumeration Type Documentation

Errorcodes used by all function in the protocol stack.

Enumerator:
MB_ENOERR 

no error.

MB_ENOREG 

illegal register address.

MB_EINVAL 

illegal argument.

MB_EPORTERR 

porting layer error.

MB_ENORES 

insufficient resources.

MB_EIO 

I/O error.

MB_EILLSTATE 

protocol stack in illegal state.

MB_ETIMEDOUT 

timeout error occurred.

Definition at line 112 of file mb.h.

enum eMBMode

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)

Enumerator:
MB_RTU 

RTU transmission mode.

MB_ASCII 

ASCII transmission mode.

MB_TCP 

TCP mode.

Definition at line 85 of file mb.h.

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 parity modes might be available.

Enumerator:
MB_PAR_NONE 

No parity.

MB_PAR_ODD 

Odd parity.

MB_PAR_EVEN 

Even parity.

Definition at line 55 of file mbport.h.

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.

See also:
eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and eMBRegInputCB( ).
Enumerator:
MB_REG_READ 

Read register values and pass to protocol stack.

MB_REG_WRITE 

Update register values.

Definition at line 103 of file mb.h.


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.

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.

Definition at line 247 of file mb.cpp.

eMBErrorCode eMBDisable ( void   )

Disable the Modbus protocol stack.

This function disables processing of Modbus frames.

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.

Definition at line 275 of file mb.cpp.

eMBErrorCode eMBEnable ( void   )

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.

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.

Definition at line 261 of file mb.cpp.

eMBErrorCode eMBInit ( eMBMode  eMode,
UCHAR  ucSlaveAddress,
UCHAR  ucPort,
ULONG  ulBaudRate,
eMBParity  eParity 
)

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.

Parameters:
eModeIf ASCII or RTU mode should be used.
ucSlaveAddressThe slave address. Only frames sent to this address or to the broadcast address are processed.
ucPortThe port to use. E.g. 1 for COM1 on windows. This value is platform dependent and some ports simply choose to ignore it.
ulBaudRateThe baudrate. E.g. 19200. Supported baudrates depend on the porting layer.
eParityParity 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 slave addresses are in the range 1 - 247.
  • eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.

Definition at line 129 of file mb.cpp.

eMBErrorCode eMBPoll ( void   )

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.

Returns:
If the protocol stack is not in the enabled state the function returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns eMBErrorCode::MB_ENOERR.

Definition at line 291 of file mb.cpp.

eMBErrorCode eMBRegisterCB ( UCHAR  ucFunctionCode,
pxMBFunctionHandler  pxHandler 
)

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.

Parameters:
ucFunctionCodeThe Modbus function code for which this handler should be registers. Valid function codes are in the range 1 to 127.
pxHandlerThe function handler which should be called in case such a frame is 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.

Definition at line 211 of file mb.cpp.

eMBErrorCode eMBSetSlaveID ( UCHAR  ucSlaveID,
BOOL  xIsRunning,
UCHAR const *  pucAdditional,
USHORT  usAdditionalLen 
)

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

Parameters:
ucSlaveIDValues is returned in the Slave ID byte of the Report Slave ID response.
xIsRunningIf TRUE the Run Indicator Status byte is set to 0xFF. otherwise the Run Indicator Status is 0x00.
pucAdditionalValues which should be returned in the Additional bytes of the Report Slave ID response.
usAdditionalLenLength of the buffer pucAdditonal.
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.

Definition at line 53 of file mbfuncother.cpp.

eMBErrorCode eMBTCPInit ( USHORT  usTCPPort )

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.

Parameters:
usTCPPortThe TCP port to listen on.
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 slave addresses are in the range 1 - 247.
  • eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.

Definition at line 187 of file mb.cpp.