FieldTalk Modbus® Slave Library C++ Editions |
![]() |
After instantiating a Server Engine class of any protocol flavour, you have to associate it with a Data Provider by calling addDataTable and passing a pointer to the Data Provider object.
MbusRtuSlaveProtocol mbusProtocol; mbusProtocol.addDataTable(1, &dataTable);
To create an application specific Data Provider derive a new class from MbusDataTableInterface and override the required data access methods.
A minimal Data Provider which realises a Modbus slave with read access to holding registers would be:
class MyDataProvider: public MbusDataTableInterface { public: MyDataProvider() {} // Override readHoldingRegistersTable method: int readHoldingRegistersTable(int startRef, short regArr[], int refCnt) { ... your application specific implementation } };
Classes | |
class | MbusDataTableInterface |
This class defines the interface between a Modbus slave Server Engine and your application. Descendants of this class are referred to as Data Providers. More... | |
Data Access Methods for Table 4:00000 (Holding Registers) | |
Data Access Methods to support read and write of output registers (holding registers) in table 4:00000. This table is accessed by the following Modbus functions:
| |
virtual int | MbusDataTableInterface::readHoldingRegistersTable (int startRef, short regArr[], int refCnt) |
Override this method to implement a Data Provider function to read Holding Registers. | |
virtual int | MbusDataTableInterface::writeHoldingRegistersTable (int startRef, const short regArr[], int refCnt) |
Override this method to implement a Data Provider function to write Holding Registers. | |
Data Access Methods for Table 3:00000 (Input Registers) | |
Data Access Methods to support read of input registers in table 3:00000. This table is accessed by the following Modbus functions:
| |
virtual int | MbusDataTableInterface::readInputRegistersTable (int startRef, short regArr[], int refCnt) |
Override this method to implement a Data Provider function to read Input Registers. | |
Data Access Methods for Table 0:00000 (Coils) | |
Data Access Methods to support read and write of discrete outputs (coils) in table 0:00000. This table is accessed by the following Modbus functions:
| |
virtual int | MbusDataTableInterface::readCoilsTable (int startRef, char bitArr[], int refCnt) |
Override this method to implement a Data Provider function to read Coils. | |
virtual int | MbusDataTableInterface::writeCoilsTable (int startRef, const char bitArr[], int refCnt) |
Override this method to implement a Data Provider function to write Coils. | |
Data Access Methods for Table 1:00000 (Input Discretes) | |
Data Access Methods to support read discrete inputs (input status) in table 1:00000. This table is accessed by the following Modbus functions:
| |
virtual int | MbusDataTableInterface::readInputDiscretesTable (int startRef, char bitArr[], int refCnt) |
Override this method to implement a Data Provider function to read Coils. | |
Data Access Synchronisation Functions | |
Implementation of these functions may only be required in multithreaded applications, if you are running the server loop in a separate thread and in addition require data consistency over a block of Modbus registers.
Data consistency within a single register is always maintained if the code executes on a 16-bit or 32-bit machine, because the CPU is accessing these data types atomically. | |
virtual void | MbusDataTableInterface::lock () |
You can override this method to implement a semaphore locking mechanism to synchronise data access. | |
virtual void | MbusDataTableInterface::unlock () |
You can override this method to implement a semaphore un-locking mechanism to synchronise data access. | |
Auxiliary Functions | |
virtual void | MbusDataTableInterface::timeOutHandler () |
Override this method to implement a function to handle master poll time-outs. | |
virtual char | MbusDataTableInterface::readExceptionStatus () |
Override this method to implement a function with reports the eight exception status coils (bits) within the slave device. |
|
Override this method to implement a Data Provider function to read Holding Registers. When a slave receives a poll request for the 4:00000 data table he calls this method to retrieve the data.
A simple implementation which holds the application data in an array of shorts ( int readHoldingRegistersTable(int startRef, short regArr[], int refCnt) { startRef--; // Adjust Modbus reference counting if (startRef + refCnt > (int) sizeof(regData) / sizeof(short)) return (0); memcpy(regArr, ®Data[startRef], refCnt * sizeof(short)); return (1); }
|
|
Override this method to implement a Data Provider function to read Input Registers. When a slave receives a poll request for the 3:00000 data table he calls this method to retrieve the data. A simple and very common implementation is to map the Input Registers to the same address space than the Holding Registers table: int readInputRegistersTable(int startRef, short regArr[], int refCnt) { return (readHoldingRegistersTable(startRef, regArr, refCnt); }
|
|
Override this method to implement a Data Provider function to read Coils. When a slave receives a poll request for the 0:00000 data table he calls this method to retrieve the data.
A simple implementation which holds the boolean application data in an array of chars ( int readCoilsTable(int startRef, char bitArr[], int refCnt) { startRef--; // Adjust Modbus reference counting if (startRef + refCnt > (int) sizeof(bitData) / sizeof(char)) return (0); memcpy(bitArr, &bitData[startRef], refCnt * sizeof(char)); return (1); }
|
|
Override this method to implement a Data Provider function to read Coils. When a slave receives a poll request for the 0:00000 data table he calls this method to retrieve the data. A simple and very common implementation is to map the Input Discretes to the same address space than the Coils table: int readInputDiscretesTable(int startRef, char bitArr[], int refCnt) { return (readCoilsTable(startRef, bitArr, refCnt)); }
|
|
You can override this method to implement a semaphore locking mechanism to synchronise data access. This is not needed in single threaded applications but may be necessary in multithreaded applications if you are running the server loop in a separate thread and require data consistency over a block of Modbus registers. Data consistency within a single register is always maintained if the code executes on a 16-bit or 32-bit machine, because the CPU is accessing these data types atomically. This function is called by the server before calling any data read or write functions.
|
|
Override this method to implement a function to handle master poll time-outs. A master should poll a slave cyclically. If no master is polling within the time-out period this method is called. A slave can take certain actions if the master has lost connection, e.g. go into a fail-safe state.
|
|
Override this method to implement a Data Provider function to write Holding Registers. When a slave receives a write request for the 4:00000 data table he calls this method to pass the data to the application.
A simple implementation which holds the application data in an array of shorts ( int writeHoldingRegistersTable(int startRef, const short regArr[], int refCnt) { startRef--; // Adjust Modbus reference counting if (startRef + refCnt > (int) sizeof(regData) / sizeof(short)) return (0); memcpy(®Data[startRef], regArr, refCnt * sizeof(short)); return (1); }
|
|
Override this method to implement a Data Provider function to write Coils. When a slave receives a write request for the 0:00000 data table he calls this method to pass the data to the application.
A simple implementation which holds the boolean application data in an array of chars ( int writeCoilsTable(int startRef, const char bitArr[], int refCnt) { startRef--; // Adjust Modbus reference counting if (startRef + refCnt > (int) sizeof(bitData) / sizeof(char)) return (0); memcpy(&bitData[startRef], bitArr, refCnt * sizeof(char)); return (1); }
|
|
You can override this method to implement a semaphore un-locking mechanism to synchronise data access. This is not needed in single threaded applications but may be necessary in multithreaded applications if you are running the server loop in a separate thread and require data consistency over a block of Modbus registers. Data consistency within a single register is always maintained if the code executes on a 16-bit or 32-bit machine, because the CPU is accessing these data types atomically. This function is called by the server after calling any data read or write functions.
|
|
Override this method to implement a function with reports the eight exception status coils (bits) within the slave device. The exception status coils are device specific and usually used to report a device' principal status or a device' major failure codes as a 8-bit word.
|
Copyright © 2002-2006
FOCUS Software Engineering Pty Ltd, Australia.
All rights reserved.
Please see the Notices page for trademark notices. Last updated: 20 Oct 2006 |