MODBUS Master


Detailed Description

 #include "mbm.h" 

The API documentation is splitted into different parts. A typical application will only to have use the API function shown in this module. The most important functions are the functions for opening and closing a MODBUS instances. There are different function depending on the type of transmission you are going to use. For serial transmission modes the function eMBMSerialInit should be used. For MODBUS/TCP the function eMBMTCPInit should be used.
After a new instance has been created the API function for querying a slave can be used. There are two different kind of API functions. The first one are blocking API functions which can be used in case of an RTOS or a single instance of the stack. The non blocking API functions can be used in any other case.

Using a blocking version of the API:
In this example we use a single master which tries to read 5 holding registers starting at address 10 from the slave 1.

void
MBMExReadHoldingBlockingAPI( void )
{
    eMBMErrorCode   eStatus;
    xMBMHandle      xMBMMasterHdl1;
    USHORT          usNRegs[5];

    /* Initialize MODBUS stack here. */
    ...eStatus = eMBMReadHoldingRegisters( xMBMMasterHdl1, 1, 10, 5, usNRegs );
    if( MBM_ENOERR == eStatus )
    {
        /* You can safely access now usNRegs. */
    }
}
Using a non-blocking version of the API:
In this example we assume two master stacks using two different serial ports. Furthermore no RTOS is available and therefore the polled version of the funtions have to be used.
void
MBMExReadHoldingNonBlockingAPI( void )
{
    eMBMErrorCode   eStatus1, eStatus2;
    eMBMQueryState  eState1;
    eMBMQueryState  eState2;
    xMBMHandle      xMBMMasterHdl1;
    xMBMHandle      xMBMMasterHdl2;
    USHORT          usNRegs1[5], usNRegs2[2];

    /* Initialize MODBUS stacks here. */
    ...
        /* Don't forget to initialize the state variables. */
        eState1 = eState2 = MBM_STATE_NONE;
    do
    {
        if( MBM_STATE_NONE != eState1 )
        {
            vMBMReadHoldingRegistersPolled( xMBMMasterHdl1, 1, 10, 5, usNRegs1, &eState1, &eStatus1 );
        }
        if( MBM_STATE_NONE != eState2 )
        {
            vMBMReadHoldingRegistersPolled( xMBMMasterHdl2, 23, 7, 2, usNRegs2, &eState2, &eStatus2 );
        }
    }
    while( ( eState1 != MBM_STATE_DONE ) || ( eState2 != MBM_STATE_DONE ) );

    if( MBM_ENOERR == eStatus1 )
    {
        /* You can safely access now usNRegs1. */
    }
    if( MBM_ENOERR == eStatus2 )
    {
        /* You can safely access now usNRegs2. */
    }
}


Modules

 MODBUS Configuration

Data Structures

struct  xMBMFileSubReadReq_t
struct  xMBMFileSubReadResp_t
struct  xMBMFileSubWriteReq_t

Typedefs

typedef void * xMBMHandle

Enumerations

enum  eMBMQueryState {
  MBM_STATE_NONE, MBM_STATE_SEND, MBM_STATE_WAITING, MBM_STATE_DISASSEMBLE,
  MBM_STATE_ERROR, MBM_STATE_DONE
}

Functions

_DLLEXP eMBErrorCode eMBMSetSlaveTimeout (xMBMHandle xHdl, USHORT usNMilliSeconds)
_DLLEXP eMBErrorCode eMBMClose (xMBMHandle xHdl)
_DLLEXP eMBErrorCode eMBMGetStatistics (xMBMHandle xHdl, xMBStat *pxMBMCurrentStat)
_DLLEXP eMBErrorCode eMBMResetStatistics (xMBMHandle xHdl)
_DLLEXP eMBErrorCode eMBMRegisterProtAnalyzer (xMBMHandle xHdl, void *pvCtxArg, pvMBAnalyzerCallbackCB pvMBAnalyzerCallbackFN)
_DLLEXP eMBErrorCode eMBMReadHoldingRegisters (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, USHORT arusBufferOut[])
_DLLEXP eMBErrorCode eMBMWriteSingleRegister (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT usValue)
_DLLEXP eMBErrorCode eMBMReadInputRegisters (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, USHORT arusBufferOut[])
_DLLEXP eMBErrorCode eMBMWriteMultipleRegisters (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, const USHORT arusBufferIn[])
_DLLEXP eMBErrorCode eMBMReadWriteMultipleRegisters (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usWriteRegStartAddress, UBYTE ubWriteNRegs, const USHORT arusBufferIn[], USHORT usReadRegStartAddress, UBYTE ubReadNRegs, USHORT arusBufferOut[])
_DLLEXP eMBErrorCode eMBMReadDiscreteInputs (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usInputStartAddress, USHORT usNInputs, UBYTE arubBufferOut[])
_DLLEXP eMBErrorCode eMBMReadCoils (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usCoilStartAddress, USHORT usNCoils, UBYTE arubBufferOut[])
_DLLEXP eMBErrorCode eMBMWriteSingleCoil (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usOutputAddress, BOOL bOn)
_DLLEXP eMBErrorCode eMBMWriteCoils (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usCoilStartAddress, USHORT usNCoils, const UBYTE arubCoilsIn[])
_DLLEXP eMBErrorCode eMBMReadWriteRAWPDU (xMBMHandle xHdl, UCHAR ucSlaveAddress, UCHAR ucFunctionCode, const UBYTE arubPayloadIn[], UBYTE ubPayloadInLength, UBYTE arubPayloadOut[], UBYTE ubPayloadOutLengthMax, UBYTE *pubPayloadOutLength)
_DLLEXP eMBErrorCode eMBMReportSlaveID (xMBHandle xHdl, UCHAR ucSlaveAddress, UBYTE arubBufferOut[], UBYTE ubBufferMax, UBYTE *pubLength)
_DLLEXP eMBErrorCode eMBMReadFileRecord (xMBHandle xHdl, UCHAR ucSlaveAddress, const xMBMFileSubReadReq_t arxSubRequests[], xMBMFileSubReadResp_t arxSubResponses[], USHORT usNSubRequests)
_DLLEXP eMBErrorCode eMBMWriteFileRecord (xMBHandle xHdl, UCHAR ucSlaveAddress, const xMBMFileSubWriteReq_t arxSubRequests[], USHORT usNSubRequests)
_DLLEXP void vMBMReadHoldingRegistersPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, USHORT arusBufferOut[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMWriteSingleRegisterPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegAddress, USHORT usValue, eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReadInputRegistersPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, USHORT arusBufferOut[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMWriteMultipleRegistersPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usRegStartAddress, UBYTE ubNRegs, const USHORT arusBufferIn[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReadWriteMultipleRegistersPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usWriteRegStartAddress, UBYTE ubWriteNRegs, const USHORT arusBufferIn[], USHORT usReadRegStartAddress, UBYTE ubReadNRegs, USHORT arusBufferOut[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReadDiscreteInputsPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usInputStartAddress, USHORT usNInputs, UBYTE arubBufferOut[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReadCoilsPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usCoilStartAddress, USHORT usNCoils, UBYTE arubBufferOut[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMWriteSingleCoilPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usOutputAddress, BOOL bOn, eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMWriteCoilsPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, USHORT usCoilStartAddress, USHORT usNCoils, const UBYTE arubCoilsIn[], eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReadWriteRAWPDUPolled (xMBMHandle xHdl, UCHAR ucSlaveAddress, UCHAR ucFunctionCode, const UBYTE arubPayloadIn[], UBYTE ubPayloadInLength, UBYTE arubPayloadOut[], UBYTE ubPayloadOutLengthMax, UBYTE *pubPayloadOutLength, eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMReportSlaveID (xMBHandle xHdl, UCHAR ucSlaveAddress, UBYTE arubBufferOut[], UBYTE ubBufferMax, UBYTE *pubLength, eMBMQueryState *peState, eMBErrorCode *peStatus)
void vMBMReadFileRecordPolled (xMBHandle xHdl, UCHAR ucSlaveAddress, const xMBMFileSubReadReq_t arxSubRequests[], xMBMFileSubReadResp_t arxSubResponses[], USHORT usNSubRequests, eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP void vMBMWriteFileRecordPolled (xMBHandle xHdl, UCHAR ucSlaveAddress, const xMBMFileSubWriteReq_t arxSubRequests[], USHORT usNSubRequests, eMBMQueryState *peState, eMBErrorCode *peStatus)
_DLLEXP eMBErrorCode eMBMSerialInit (xMBMHandle *pxHdl, eMBSerialMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBSerialParity eParity)
_DLLEXP eMBErrorCode eMBMSerialInitExt (xMBMHandle *pxHdl, eMBSerialMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBSerialParity eParity, UCHAR ucStopBits)
_DLLEXP eMBErrorCode eMBMTCPInit (xMBMHandle *pxHdl)
_DLLEXP eMBErrorCode eMBMTCPConnect (xMBMHandle xHdl, const CHAR *pcTCPClientAddress, USHORT usTCPPort)
_DLLEXP eMBErrorCode eMBMTCPDisconnect (xMBMHandle xHdl)
_DLLEXP eMBErrorCode eMBMUDPInit (xMBMHandle *pxHdl, const CHAR *pcUDPBindAddress, LONG uUDPListenPort)
_DLLEXP eMBErrorCode eMBMUDPSetSlave (xMBMHandle xHdl, const CHAR *pcUDPClientAddress, USHORT usUDPSlavePort)


Typedef Documentation

typedef void* xMBMHandle
 

A handle to a MODBUS master instance.


Enumeration Type Documentation

enum eMBMQueryState
 

States used by the MODBUS stack internally.

Note:
These values are not important for the user and should not be accessed or used. They are used by the polled versions of the API calls.
Enumeration values:
MBM_STATE_NONE  Not yet started.
MBM_STATE_SEND  Frame will be sent.
MBM_STATE_WAITING  Waiting for an event.
MBM_STATE_DISASSEMBLE  Disassembling the frame.
MBM_STATE_ERROR  An error occurred.
MBM_STATE_DONE  We are done processing the request.


Function Documentation

_DLLEXP eMBErrorCode eMBMClose xMBMHandle  xHdl  ) 
 

Close the stack.

Shutdown the master stack. This function should not be called when there are still pending requests.

Parameters:
xHdl A handle for a MODBUS master instances.
Returns:
eMBErrorCode::MB_ENOERR if the stack has been shut down.
Examples:
AVR/demo.c.

_DLLEXP eMBErrorCode eMBMGetStatistics xMBMHandle  xHdl,
xMBStat pxMBMCurrentStat
 

Retrieve the current master statistics.

This function populates the argument pxMBMCurrentStat with the current internal counters.

Parameters:
xHdl A valid MODBUS master handle.
pxMBMCurrentStat A pointer to an (potentially unitialized) eMBMStat datastructure. When the return value is eMBErrorCode::MB_ENOERR this data structure holds a copy of the internal counters.
Returns:
eMBErrorCode::MB_ENOERR if successful. In case of an invalid argument the function returns eMBErrorCode::MB_EINVAL.

_DLLEXP eMBErrorCode eMBMReadCoils xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usCoilStartAddress,
USHORT  usNCoils,
UBYTE  arubBufferOut[]
 

Read Coils from a slave using the MODBUS function code 0x01

The coils are packed as one coil per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the coil addressed in the query. The other coils follow toward the high order of the byte. If the input quantity is not a multiple of eight the final data byte is padded with zeros.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usCoilStartAddress Address of first coil.
usNCoils Number of coils to read.
arubBufferOut An array with a size of at least usNCoils/8 bytes. The size must be rounded up to next integer.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arubBufferIn contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMReadDiscreteInputs xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usInputStartAddress,
USHORT  usNInputs,
UBYTE  arubBufferOut[]
 

Read Discrete Inputs from a slave using the MODBUS function code 0x02

The discrete inputs are packed as one input per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the input addressed in the query. The other inputs follow toward the high order of the byte. If the input quantity is not a multiple of eight the final data byte is padded with zeros.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usInputStartAddress Address of first discrete input.
usNInputs Number of discrete inputs to read.
arubBufferOut An array with a size of at least usNInputs/8 bytes. The size must be rounded up to next integer.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arubBufferIn contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMReadFileRecord xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
const xMBMFileSubReadReq_t  arxSubRequests[],
xMBMFileSubReadResp_t  arxSubResponses[],
USHORT  usNSubRequests
 

)Read File Record from a MODBUS slave with function code 0x14

This function issues a read file record request. The caller of this function is required to provide an (possibly unitialized) array of arxSubResponses. The size of this array must be equal or larger than the number of sub requests within arxSubRequests.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arxSubRequests Sub requests for file records.
arxSubResponses A sub response for every requested arxSubRequests.
usNSubRequests Size of the arxSubRequests arrays.
Returns:
eMBErrorCode::MB_ENOERR if the read file record command was successfull. If the arguments are not valid, e.g the number of sub requests would exceed the maximum length of a MODBUS frame, the function returns eMBErrorCode::MB_EINVAL. In case of an exception returned by the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMReadHoldingRegisters xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
USHORT  arusBufferOut[]
 

Read Holding Registers from a slave using the MODBUS function code 0x03

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Note that a broadcast address is not allowed for a function which expects a response.
usRegStartAddress The first holding register to be read. We use protocol addresses starting at zero.
ubNRegs Number of registers to read.
arusBufferOut An array of USHORT values of at least ubNRegs elements.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arusBufferOut contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT. Any other errors are IO errors.
Examples:
AVR/demo.c.

_DLLEXP eMBErrorCode eMBMReadInputRegisters xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
USHORT  arusBufferOut[]
 

Read Input Registers from a slave using the MODBUS function code 0x04.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Not that a broadcast address is not allowed.
usRegStartAddress First register to read.
ubNRegs Numer of registers to read.
arusBufferOut An array of USHORT values of at least ubNRegs elements.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arusBufferOut contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT. Any other errors are IO errors.
Examples:
AVR/demo.c.

_DLLEXP eMBErrorCode eMBMReadWriteMultipleRegisters xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usWriteRegStartAddress,
UBYTE  ubWriteNRegs,
const USHORT  arusBufferIn[],
USHORT  usReadRegStartAddress,
UBYTE  ubReadNRegs,
USHORT  arusBufferOut[]
 

Read/Write Multiple Registers from a slave using the MODBUS function code 0x17

Note:
The pointers arusBufferIn and arusBufferOut can point to the same memory location. In this case the old contents is overwritten with the returned registers values.
Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usWriteRegStartAddress First register to write on the slave.
ubWriteNRegs Number of registers to write.
arusBufferIn An Array of USHORT values of ubWriteNRegs elements which should be written on the slave.
usReadRegStartAddress First register to read from the slave.
ubReadNRegs Number of registers to read.
arusBufferOut An Array of ubReadNRegs elements where the registers values returned by the slave should be stored.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arusBufferOut contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT. Any other errors are IO errors.

_DLLEXP eMBErrorCode eMBMReadWriteRAWPDU xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
UCHAR  ucFunctionCode,
const UBYTE  arubPayloadIn[],
UBYTE  ubPayloadInLength,
UBYTE  arubPayloadOut[],
UBYTE  ubPayloadOutLengthMax,
UBYTE pubPayloadOutLength
 

A function for sending raw MODBUS PDUs to a MODBUS slave. This function can be used if you want to transport custom data over a MODBUS connection or for briding other protocols. Please note that of course most standard slaves won't know how to deal with these custom data payload and therefore its use should be limited.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
ucFunctionCode MODBUS function code used for transport. Must be between 1 and 127. Recommend values are between 65-72 and 100-110.
arubPayloadIn MODBUS request which will be sent to the slave.
ubPayloadInLength Length of MODBUS request. Maximum is 252 bytes.
arubPayloadOut Holds the MODBUS response sent by the slave. arubPayloadOut can be NULL is ubPayloadOutLengthMax is set to zero.
ubPayloadOutLengthMax Size of the buffer arubPayloadOut in bytes. If the slave response exceeds the number of bytes available in the buffer the function returns eMBErrorCode::MB_EINVAL.
pubPayloadOutLength Actual number of bytes returned by the slave.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. If the input arguments are invalid, for example arubPayloadIn is a NULL pointer, the function returns eMBErrorCode::MB_EINVAL. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMRegisterProtAnalyzer xMBMHandle  xHdl,
void *  pvCtxArg,
pvMBAnalyzerCallbackCB  pvMBAnalyzerCallbackFN
 

Register an protocol analyzer.

If a protocol analyzer has been registered a callback is made whenever a frame has been sent or received.

Parameters:
xHdl A valid MODBUS master handle.
pvMBAnalyzerCallbackFN A valid pointer to a callback handler or NULL if the analyzer should be removed.
pvCtxArg A user defined context. Can be NULL.
Returns:
eMBErrorCode::MB_ENOERR if the analyzer has been added or removed. eMBErrorCode::MB_EINVAL in case of an invalid MODBUS handle.

_DLLEXP eMBErrorCode eMBMReportSlaveID xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
UBYTE  arubBufferOut[],
UBYTE  ubBufferMax,
UBYTE pubLength
 

Report slave ID from a MODBUS slave with function code 0x11

This function issues the report slave id command to a MODBUS slave. The response is then stored in the buffer provided by arubBufferOut where the caller has to allocate sufficient space. The maximum amount of space available is determined by ubBufferMax. If there is not enough space available the function returns eMBErrorCode::MB_ENORES.

There is no chance for the MODBUS stack to tell something about the content since the content is vendor specific.

Note:
There are some non vendor specific fields but most vendors do not implement them correctly. Therefore we have completely avoided interpreting the data.
Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arubBufferOut Buffer which can hold ubBufferMax bytes.
ubBufferMax Size of buffer.
pubLength If the call succeeds contains the number of bytes written to the buffer.
Returns:
eMBErrorCode::MB_ENOERR if the slave report command succeeded. If the buffer does not have enough space the function returns eMBErrorCode::MB_ENORES. If any of the input arguments are invalid or the buffer are NULL the function return eMBErrorCode::MB_EINVAL. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMResetStatistics xMBMHandle  xHdl  ) 
 

Clears the current statistic counters.

Parameters:
xHdl A valid MODBUS master handle.
Returns:
eMBErrorCode::MB_ENOERR if successful. In case of an invalid argument the function returns eMBErrorCode::MB_EINVAL.

_DLLEXP eMBErrorCode eMBMSerialInit xMBMHandle pxHdl,
eMBSerialMode  eMode,
UCHAR  ucPort,
ULONG  ulBaudRate,
eMBSerialParity  eParity
 

Create a new instances for a serial MODBUS master instance using either ASCII or RTU transmission mode.

Note:
In RTU mode 11 bits are used for each data byte. The coding system is 8bit binary.
  • 1 start bit.
  • 8 data bits with LSB sent first.
  • 1 bit for parity (Even, Odd)
  • 1 or 2 stop bits (Two stopbits if no parity is used).
In ASCII mode 10 bits are used. The coding system uses the hexadecimal ASCII characters 0-8 and A-F. One hexadecimal characters contains 4-bits of data.
  • 1 start bit
  • 7 data bits with LSB sent first.
  • 1 bit for parity (Even, Odd)
  • 1 or 2 stop bits (Two stopbits if no parity is used).

Parameters:
pxHdl A pointer to a MODBUS handle. If the function returns MB_ENOERR the handle is updated to hold a new and valid master handle. This handle should never be modified by the user.
eMode The serial transmission mode to use. Either MB_RTU or MB_ASCII.
ucPort The serial port to use. The meaning of this value depends on the porting layer.
ulBaudRate The baudrate. For example 38400.
eParity The parity to use.
Returns:
eMBErrorCode::MB_ENOERR if a new MASTER instances is ready.
Examples:
AVR/demo.c.

_DLLEXP eMBErrorCode eMBMSerialInitExt xMBMHandle pxHdl,
eMBSerialMode  eMode,
UCHAR  ucPort,
ULONG  ulBaudRate,
eMBSerialParity  eParity,
UCHAR  ucStopBits
 

Create a new instances for a serial MODBUS master instance using either ASCII or RTU transmission mode.

Identical to eMBMSerialInit with the exception that the number of stopbits can be set. Note that this can result in non standard conforming behavior.

Parameters:
pxHdl A pointer to a MODBUS handle. If the function returns MB_ENOERR the handle is updated to hold a new and valid master handle. This handle should never be modified by the user.
eMode The serial transmission mode to use. Either MB_RTU or MB_ASCII.
ucPort The serial port to use. The meaning of this value depends on the porting layer.
ulBaudRate The baudrate. For example 38400.
eParity The parity to use.
ucStopBits Number of stopbits to use.
Returns:
eMBErrorCode::MB_ENOERR if a new MASTER instances is ready.

_DLLEXP eMBErrorCode eMBMSetSlaveTimeout xMBMHandle  xHdl,
USHORT  usNMilliSeconds
 

Set the slave timeout for the next request.

Parameters:
xHdl A valid MODBUS master handle.
usNMilliSeconds The new slave device timeout. If no response is received within this time window after a MODBUS request has been sent to the slave the API functions will return eMBErrorCode::MB_ETIMEDOUT to indicate an timeout error.
Returns:
eMBErrorCode::MB_ENOERR if the new slave timeout will be used on the next request.

_DLLEXP eMBErrorCode eMBMTCPConnect xMBMHandle  xHdl,
const CHAR pcTCPClientAddress,
USHORT  usTCPPort
 

Connect to a new MODBUS TCP slave.

If a connection is already open the old connection is closed.

Parameters:
xHdl A valid MODBUS master handle.
pcTCPClientAddress A TCP client address. This address is directly passed to the porting layer and therefore its meaning is platform dependent.
usTCPPort The TCP port to use. 502 is standard.
Returns:
eMBErrorCode::MB_ENOERR if a connection has been opened. In case of a connection error eMBErrorCode::MB_EIO.

_DLLEXP eMBErrorCode eMBMTCPDisconnect xMBMHandle  xHdl  ) 
 

Disconnects a MODBUS TCP slave.

Parameters:
xHdl A valid MODBUS master handle.

_DLLEXP eMBErrorCode eMBMTCPInit xMBMHandle pxHdl  ) 
 

Create a new instaces for a MODBUS TCP master.

Parameters:
pxHdl A pointer to a MODBUS handle. If the function returns MB_ENOERR the handle is updated to hold a new and valid master handle. This handle should never be modified by the user.
Returns:
eMBErrorCode::MB_ENOERR if a new MASTER instances is ready.

_DLLEXP eMBErrorCode eMBMUDPInit xMBMHandle pxHdl,
const CHAR pcUDPBindAddress,
LONG  uUDPListenPort
 

Create a new instaces for a MODBUS UDP master.

Parameters:
pxHdl A pointer to a MODBUS handle. If the function returns MB_ENOERR the handle is updated to hold a new and valid master handle. This handle should never be modified by the user.
pcUDPBindAddress Not used within the stack but passed to the porting layer. This argument can be used to bind only to a specific IP address.
uUDPListenPort Use -1 if the listing port should be allocated dynamically by the porting layer. Otherwise pass a valid port number between 0 and 65535.
Returns:
eMBErrorCode::MB_ENOERR if a new MASTER instances is ready.

_DLLEXP eMBErrorCode eMBMUDPSetSlave xMBMHandle  xHdl,
const CHAR pcUDPClientAddress,
USHORT  usUDPSlavePort
 

Set the IP address of the MODBUS UDP slave.

Not data is actually sent by specifying the client address. All future data sent by any of the read/write registers calls will be directed to this client address. The previous client address is overwritten.

Parameters:
xHdl A valid MODBUS master handle.
pcUDPClientAddress A UDP client address. This address is directly passed to the porting layer and therefore its meaning is platform dependent.
usUDPSlavePort The UDP port to use. 502 is standard.
Returns:
eMBErrorCode::MB_ENOERR if a connection has been opened. In case of a connection error eMBErrorCode::MB_EIO.
Warning:
A reference to pcUDPClientAddress is stored internally. As long as this instance is used the data where pcUDPClientAddress points to must not change. This specially implies that extra care has to be taken when the address is stored on the stack.

_DLLEXP eMBErrorCode eMBMWriteCoils xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usCoilStartAddress,
USHORT  usNCoils,
const UBYTE  arubCoilsIn[]
 

Write Coils from a slave using the MODBUS function code 0x0F

The coils are packed as one coil per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the coil addressed in the query. The other coils follow toward the high order of the byte.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usCoilStartAddress Address of first coil.
usNCoils Number of coils to read.
arubCoilsIn An array with a size of at least usNCoils/8 bytes. The size must be rounded up to next integer. A
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arubBufferIn contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMWriteFileRecord xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
const xMBMFileSubWriteReq_t  arxSubRequests[],
USHORT  usNSubRequests
 

)Write File Record to a MODBUS slave with function code 0x15

This function issues a write file record request. The caller of this function is required to provide an array of arxSubRequests.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arxSubRequests Sub requests for file records.
usNSubRequests Size of the arxSubRequests arrays.
Returns:
eMBErrorCode::MB_ENOERR if the write file record command was successfull. If the arguments are not valid, e.g the number of sub requests would exceed the maximum length of a MODBUS frame, the function returns eMBErrorCode::MB_EINVAL. In case of an exception returned by the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMWriteMultipleRegisters xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
const USHORT  arusBufferIn[]
 

Write Multiple Registers from a slave using the MODBUS function code 0x10.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usRegStartAddress First register to write to.
ubNRegs Number of registers to write.
arusBufferIn An Array of USHORT values of at least ubNRegs elements.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arusBufferOut contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT. Any other errors are IO errors.
Examples:
AVR/demo.c.

_DLLEXP eMBErrorCode eMBMWriteSingleCoil xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usOutputAddress,
BOOL  bOn
 

Write Coil on slave using the MODBUS function code 0x05

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usOutputAddress Address of coil.
bOn Either TRUE or FALSE.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT.

_DLLEXP eMBErrorCode eMBMWriteSingleRegister xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegAddress,
USHORT  usValue
 

Write Single Register in a slave using the MODBUS function code 0x06.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Note that a broadcast address is not allowed for a function which expects a response.
usRegAddress The register address to write.
usValue The value which should be written to the register.
Returns:
eMBErrorCode::MB_ENOERR if the slave responded within the timeout and the the reply conforms to the MODBUS protocol specification. In this case the array arusBufferOut contains the values returned by the slave. In case of an exception from the slave any of the MODBUS exceptions can be returned. If the slave did not respond within the timeout the function returns eMBErrorCode::MB_ETIMEDOUT. Any other errors are IO errors.
Examples:
AVR/demo.c.

_DLLEXP void vMBMReadCoilsPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usCoilStartAddress,
USHORT  usNCoils,
UBYTE  arubBufferOut[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Read Coils from a slave using the MODBUS function code 0x01

The coils are packed as one coil per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the coil addressed in the query. The other coils follow toward the high order of the byte. If the input quantity is not a multiple of eight the final data byte is padded with zeros.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usCoilStartAddress Address of first coil.
usNCoils Number of coils to read.
arubBufferOut An array with a size of at least udNInputs/8 bytes. The size must be rounded up to next integer.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMReadDiscreteInputsPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usInputStartAddress,
USHORT  usNInputs,
UBYTE  arubBufferOut[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Read Discrete Inputs from a slave using the MODBUS function code 0x02

The discrete inputs are packed as one input per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the input addressed in the query. The other inputs follow toward the high order of the byte. If the input quantity is not a multiple of eight the final data byte is padded with zeros.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usInputStartAddress Address of first discrete input.
usNInputs Number of discrete inputs to read.
arubBufferOut An array with a size of at least udNInputs/8 bytes. The size must be rounded up to next integer.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

void vMBMReadFileRecordPolled xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
const xMBMFileSubReadReq_t  arxSubRequests[],
xMBMFileSubReadResp_t  arxSubResponses[],
USHORT  usNSubRequests,
eMBMQueryState peState,
eMBErrorCode peStatus
 

)Read File Record from a MODBUS slave with function code 0x14

This function issues a read file record request. The caller of this function is required to provide an (possibly unitialized) array of arxSubResponses. The size of this array must be equal or larger than the number of sub requests within arxSubRequests.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arxSubRequests Sub requests for file records.
arxSubResponses A sub response for every requested arxSubRequests.
usNSubRequests Size of the arxSubRequests arrays.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version eMBMReadFileRecord.

_DLLEXP void vMBMReadHoldingRegistersPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
USHORT  arusBufferOut[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Read Holding Registers from a slave using the MODBUS function code 0x03 with the Non-Blocking API.

This function provides the same functionality as the function eMBMReadHoldingRegisters but uses a different interface.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Note that a broadcast address is not allowed for a function which expects a response.
usRegStartAddress The first holding register to be read. We use protocol addresses starting at zero.
ubNRegs Number of registers to read.
arusBufferOut An array of USHORT values of at least ubNRegs elements.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMReadInputRegistersPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
USHORT  arusBufferOut[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Read Input Registers from a slave using the MODBUS function code 0x04 with the Non-Blocking API.

This function provides the same functionality as the function eMBMReadInputRegisters but uses a different interface.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Note that a broadcast address is not allowed.
usRegStartAddress The first holding register to be read starting at zero.
ubNRegs Number of registers to read.
arusBufferOut An array of USHORT values of at least ubNRegs elements.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMReadWriteMultipleRegistersPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usWriteRegStartAddress,
UBYTE  ubWriteNRegs,
const USHORT  arusBufferIn[],
USHORT  usReadRegStartAddress,
UBYTE  ubReadNRegs,
USHORT  arusBufferOut[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Read/Write Multiple Registers from a slave using the MODBUS function code 0x17

Note:
The pointers arusBufferIn and arusBufferOut can point to the same memory location. In this case the old contents is overwritten with the returned registers values.
Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usWriteRegStartAddress First register to write on the slave.
ubWriteNRegs Number of registers to write.
arusBufferIn An Array of USHORT values of ubWriteNRegs elements which should be written on the slave.
usReadRegStartAddress First register to read from the slave.
ubReadNRegs Number of registers to read.
arusBufferOut An Array of ubReadNRegs elements where the registers values returned by the slave should be stored.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMReadWriteRAWPDUPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
UCHAR  ucFunctionCode,
const UBYTE  arubPayloadIn[],
UBYTE  ubPayloadInLength,
UBYTE  arubPayloadOut[],
UBYTE  ubPayloadOutLengthMax,
UBYTE pubPayloadOutLength,
eMBMQueryState peState,
eMBErrorCode peStatus
 

A function for sending raw MODBUS PDUs to a MODBUS slave. This function can be used if you want to transport custom data over a MODBUS connection or for briding other protocols. Please note that of course most standard slaves won't know how to deal with these custom data payload and therefore its use should be limited.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
ucFunctionCode MODBUS function code used for transport. Must be between 1 and 127. Recommend values are between 65-72 and 100-110.
arubPayloadIn MODBUS request which will be sent to the slave.
ubPayloadInLength Length of MODBUS request. Maximum is 252 bytes.
arubPayloadOut Holds the MODBUS response sent by the slave. arubPayloadOut can be NULL is ubPayloadOutLengthMax is set to zero.
ubPayloadOutLengthMax Size of the buffer arubPayloadOut in bytes. If the slave response exceeds the number of bytes available in the buffer the function set peStatus to eMBErrorCode::MB_EINVAL.
pubPayloadOutLength Actual number of bytes returned by the slave.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMReportSlaveID xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
UBYTE  arubBufferOut[],
UBYTE  ubBufferMax,
UBYTE pubLength,
eMBMQueryState peState,
eMBErrorCode peStatus
 

Report slave ID from a MODBUS slave with function code 0x11

This function issues the report slave id command to a MODBUS slave. The response is then stored in the buffer provided by arubBufferOut where the caller has to allocate sufficient space. The maximum amount of space available is determined by ubBufferMax. If there is not enough space available the function returns eMBErrorCode::MB_ENORES.

There is no chance for the MODBUS stack to tell something about the content since the content is vendor specific.

Note:
There are some non vendor specific fields but most vendors do not implement them correctly. Therefore we have completely avoided interpreting the data.
Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arubBufferOut Buffer which can hold ubBufferMax bytes.
ubBufferMax Size of buffer.
pubLength If the call succeeds contains the number of bytes written to the buffer.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMWriteCoilsPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usCoilStartAddress,
USHORT  usNCoils,
const UBYTE  arubCoilsIn[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Write Coils from a slave using the MODBUS function code 0x0F

The coils are packed as one coil per bit. Statis is 1=ON and 0=OFF. The LSB of the first data byte contains the coil addressed in the query. The other coils follow toward the high order of the byte.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usCoilStartAddress Address of first coil.
usNCoils Number of coils to read.
arubCoilsIn An array with a size of at least usNCoils/8 bytes. The size must be rounded up to next integer. A
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMWriteFileRecordPolled xMBHandle  xHdl,
UCHAR  ucSlaveAddress,
const xMBMFileSubWriteReq_t  arxSubRequests[],
USHORT  usNSubRequests,
eMBMQueryState peState,
eMBErrorCode peStatus
 

)Write File Record to a MODBUS slave with function code 0x15

This function issues a write file record request. The caller of this function is required to provide an array of arxSubRequests.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
arxSubRequests Sub requests for file records.
usNSubRequests Size of the arxSubRequests arrays.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version eMBMWriteFileRecord.

_DLLEXP void vMBMWriteMultipleRegistersPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegStartAddress,
UBYTE  ubNRegs,
const USHORT  arusBufferIn[],
eMBMQueryState peState,
eMBErrorCode peStatus
 

Write Multiple Registers from a slave using the MODBUS function code 0x10.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usRegStartAddress First register to write to.
ubNRegs Number of registers to write.
arusBufferIn An Array of USHORT values of at least ubNRegs elements.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMWriteSingleCoilPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usOutputAddress,
BOOL  bOn,
eMBMQueryState peState,
eMBErrorCode peStatus
 

Write Coil on slave using the MODBUS function code 0x06

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address.
usOutputAddress Address of coil.
bOn Either TRUE or FALSE.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.

_DLLEXP void vMBMWriteSingleRegisterPolled xMBMHandle  xHdl,
UCHAR  ucSlaveAddress,
USHORT  usRegAddress,
USHORT  usValue,
eMBMQueryState peState,
eMBErrorCode peStatus
 

Write Single Register in a slave using the MODBUS function code 0x06 with the Non-Blocking API.

Parameters:
xHdl A valid MODBUS master handle.
ucSlaveAddress Slave address. Note that a broadcast address is not allowed for a function which expects a response.
usRegAddress The register address to write.
usValue The value which should be written to the register.
peState A pointer where the internal state can be stored. This value should be initialized to MBM_STATE_NONE. Violating this rule results in undefined behavior.
peStatus A pointer to a status variable. After the command has finished the status variable contains the final result of the transaction. The error codes are the same as for the blocking version.


(C) 2007 Embedded Solutions. Last updated on 14 Jun 2015.