#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. */ } }
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) |
|
A handle to a MODBUS master instance.
|
|
States used by the MODBUS stack internally.
|
|
Close the stack. Shutdown the master stack. This function should not be called when there are still pending requests.
|
|
Retrieve the current master statistics. This function populates the argument pxMBMCurrentStat with the current internal counters.
|
|
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.
|
|
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.
|
|
)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.
|
|
Read Holding Registers from a slave using the MODBUS function code 0x03
|
|
Read Input Registers from a slave using the MODBUS function code 0x04.
|
|
Read/Write Multiple Registers from a slave using the MODBUS function code 0x17
|
|
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.
|
|
Register an protocol analyzer. If a protocol analyzer has been registered a callback is made whenever a frame has been sent or received.
|
|
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.
|
|
Clears the current statistic counters.
|
|
Create a new instances for a serial MODBUS master instance using either ASCII or RTU transmission mode.
|
|
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.
|
|
Set the slave timeout for the next request.
|
|
Connect to a new MODBUS TCP slave. If a connection is already open the old connection is closed.
|
|
Disconnects a MODBUS TCP slave.
|
|
Create a new instaces for a MODBUS TCP master.
|
|
Create a new instaces for a MODBUS UDP master.
|
|
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.
|
|
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.
|
|
)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.
|
|
Write Multiple Registers from a slave using the MODBUS function code 0x10.
|
|
Write Coil on slave using the MODBUS function code 0x05
|
|
Write Single Register in a slave using the MODBUS function code 0x06.
|
|
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.
|
|
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.
|
|
)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.
|
|
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.
|
|
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.
|
|
Read/Write Multiple Registers from a slave using the MODBUS function code 0x17
|
|
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.
|
|
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.
|
|
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.
|
|
)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.
|
|
Write Multiple Registers from a slave using the MODBUS function code 0x10.
|
|
Write Coil on slave using the MODBUS function code 0x06
|
|
Write Single Register in a slave using the MODBUS function code 0x06 with the Non-Blocking API.
|