FieldTalk Modbus® Slave Library
C++ Editions
FOCUS Software Engineering

Main Page | Modules | Class Hierarchy | Class List | Class Members | Related Pages

MbusSerialSlaveProtocol Class Reference

Inheritance diagram for MbusSerialSlaveProtocol:

Inheritance graph
Collaboration diagram for MbusSerialSlaveProtocol:

Collaboration graph
List of all members.

Detailed Description

This base class realises the Modbus® serial slave protocols.

These methods apply to RTU and ASCII protocol flavours via inheritance. These classes provide functions to start-up and to execute the server engine which includes opening and closing of the serial port. Upon receipt of a valid master query the server engine calls Data Provider methods to exchange data with the user application. For a more detailed description which Modbus data and control functions have been implemented in the server engine see section Server Functions common to all Protocol Flavours.

It is possible to instantiate multiple instances for establishing multiple connections on different serial ports (They should be executed in separate threads).

See also:
Server Functions common to all Protocol Flavours, MbusSlaveServer


Serial Server Management Functions

virtual int startupServer (const char *const portName, long baudRate, int dataBits, int stopBits, int parity)
 Puts the Modbus server into operation.
int startupServer (const char *const portName, long baudRate)
 Puts the Modbus RTU server into operation and opens the associated serial port with default port parameters.
virtual int startupServer (int slaveAddr, const char *const portName, long baudRate, int dataBits, int stopBits, int parity)
 Puts the Modbus server into operation using a single slave address and data table.
int startupServer (int slaveAddr, const char *const portName, long baudRate)
 Puts the Modbus RTU server into operation and opens the associated serial port with default port parameters.
void shutdownServer ()
 Shuts down the Modbus server.
int isStarted ()
 Returns if server has been started up.
int getConnectionStatus ()
 Checks if a Modbus master is polling periodically.
virtual int enableRs485Mode (int rtsDelay)
 Enables RS485 mode.

Server Management Functions

int addDataTable (int slaveAddr, MbusDataTableInterface *dataTablePtr)
 Associates a protocol object with a Data Provider and a slave address.
virtual int serverLoop ()=0
 Modbus slave server loop.

Protocol Configuration

long setTimeout (long timeOut)
 Configures master transmit time-out supervision.
long getTimeout ()
 Returns the master time-out supervision value.

Transmission Statistic Functions

unsigned long getTotalCounter ()
 Returns how often a message transfer has been executed.
void resetTotalCounter ()
 Resets total message transfer counter.
unsigned long getSuccessCounter ()
 Returns how often a message transfer was successful.
void resetSuccessCounter ()
 Resets successful message transfer counter.

Utility Functions

static char * getPackageVersion ()
 Returns the package version number.

Public Types

enum  { SER_DATABITS_7 = SerialPort::SER_DATABITS_7, SER_DATABITS_8 = SerialPort::SER_DATABITS_8 }
enum  { SER_STOPBITS_1 = SerialPort::SER_STOPBITS_1, SER_STOPBITS_2 = SerialPort::SER_STOPBITS_2 }
enum  { SER_PARITY_NONE = SerialPort::SER_PARITY_NONE, SER_PARITY_EVEN = SerialPort::SER_PARITY_EVEN, SER_PARITY_ODD = SerialPort::SER_PARITY_ODD }

Protected Types

enum  { SER_RS232, SER_RS485 }

Member Enumeration Documentation

anonymous enum
 

Enumeration values:
SER_DATABITS_7  7 data bits
SER_DATABITS_8  8 data bits

anonymous enum
 

Enumeration values:
SER_STOPBITS_1  1 stop bit
SER_STOPBITS_2  2 stop bits

anonymous enum
 

Enumeration values:
SER_PARITY_NONE  No parity.
SER_PARITY_EVEN  Even parity.
SER_PARITY_ODD  Odd parity.

anonymous enum [protected]
 

Enumeration values:
SER_RS232  RS232 mode w/o RTS/CTS handshake.
SER_RS485  RS485 mode: RTS enables/disables transmitter.

Member Function Documentation

int startupServer const char *const   portName,
long  baudRate,
int  dataBits,
int  stopBits,
int  parity
[virtual]
 

Puts the Modbus server into operation.

This function opens the serial port. After the port has been opened queries from a Modbus master will be processed.

Parameters:
portName Serial port identifier (e.g. "COM1", "/dev/ser1 or /dev/ttyS0")
baudRate The port baudRate in bps (typically 1200 - 9600).
dataBits Must be SER_DATABITS_8 for RTU
stopBits SER_STOPBITS_1: 1 stop bit, SER_STOPBITS_2: 2 stop bits
parity SER_PARITY_NONE: no parity, SER_PARITY_ODD: odd parity, SER_PARITY_EVEN: even parity
Returns:
FTALK_SUCCESS on success or error code. See Protocol Errors and Exceptions for a list of error codes.

Reimplemented in MbusRtuSlaveProtocol.

int startupServer const char *const   portName,
long  baudRate
 

Puts the Modbus RTU server into operation and opens the associated serial port with default port parameters.

This function opens the serial port with 8 databits, 1 stopbit and even parity and initialises the server engine.

Parameters:
portName Serial port identifier (e.g. "COM1", "/dev/ser1" or "/dev/ttyS0")
baudRate The port baudRate in bps (typically 1200 - 9600).
Returns:
FTALK_SUCCESS on success or error code. See Protocol Errors and Exceptions for a list of error codes.

int startupServer int  slaveAddr,
const char *const   portName,
long  baudRate,
int  dataBits,
int  stopBits,
int  parity
[virtual]
 

Puts the Modbus server into operation using a single slave address and data table.

This function opens the serial port. After the port has been opened queries from a Modbus master will be processed.

Function is kept for compatibility with previous API versions, do not use for new implementations.

Parameters:
slaveAddr Modbus slave address for server to listen on (1-255)
portName Serial port identifier (e.g. "COM1", "/dev/ser1 or /dev/ttyS0")
baudRate The port baudRate in bps (typically 1200 - 9600).
dataBits Must be SER_DATABITS_8 for RTU
stopBits SER_STOPBITS_1: 1 stop bit, SER_STOPBITS_2: 2 stop bits
parity SER_PARITY_NONE: no parity, SER_PARITY_ODD: odd parity, SER_PARITY_EVEN: even parity
Returns:
FTALK_SUCCESS on success or error code. See Protocol Errors and Exceptions for a list of error codes.

Deprecated:
This function is deprecated. The preferred way of assigning a slave address is using the default constructor and configuring data table and slave address using addDataTable method.

Reimplemented in MbusRtuSlaveProtocol.

int startupServer int  slaveAddr,
const char *const   portName,
long  baudRate
 

Puts the Modbus RTU server into operation and opens the associated serial port with default port parameters.

This function opens the serial port with 8 databits, 1 stopbit and even parity and initialises the server engine.

Function is kept for compatibility with previous API versions, do not use for new implementations.

Parameters:
slaveAddr Modbus slave address for server to listen on (1-255)
portName Serial port identifier (e.g. "COM1", "/dev/ser1" or "/dev/ttyS0")
baudRate The port baudRate in bps (typically 1200 - 9600).
Returns:
FTALK_SUCCESS on success or error code. See Protocol Errors and Exceptions for a list of error codes.

Deprecated:
This function is deprecated. The preferred way of assigning a slave address is using the default constructor and configuring data table and slave address using addDataTable method.

void shutdownServer  )  [virtual]
 

Shuts down the Modbus server.

This function also closes the serial port.

Reimplemented from MbusSlaveServer.

int isStarted  )  [virtual]
 

Returns if server has been started up.

Return values:
true = started
false = shutdown

Implements MbusSlaveServer.

int getConnectionStatus  )  [virtual]
 

Checks if a Modbus master is polling periodically.

Return values:
true = A master is polling at a frequency higher than the master transmit time-out value
false = No master is polling within the time-out period
Note:
The master transmit time-out value must be set > 0 in order for this function to work.

Implements MbusSlaveServer.

int enableRs485Mode int  rtsDelay  )  [virtual]
 

Enables RS485 mode.

In RS485 mode the RTS signal can be used to enable and disable the transmitter of a RS232/RS485 converter. The RTS signal is asserted before sending data. It is cleared after the transmit buffer has been emptied and in addition the specified delay time has elapsed. The delay time is necessary because even the transmit buffer is already empty, the UART's FIFO will still contain unsent characters.

Warning:
The use of RTS controlled RS232/RS485 converters should be avoided if possible. It is difficult to determine the exact time when to switch off the transmitter with non real-time operating systems like Windows and Linux. If it is switched off to early characters might still sit in the FIFO or the transmit register of the UART and these characters will be lost. Hence the slave will not recognize the message. On the other hand if it is switched off too late then the slave's message is corrupted and the master will not recognize the message.
Remarks:
The delay value is indicative only and not guaranteed to be maintained. How precise it is followed depends on the operating system used, it's scheduling priority and it's system timer resolution.
Note:
A protocol must be closed in order to configure it.
Parameters:
rtsDelay Delay time in ms (Range: 0 - 100000) which applies after the transmit buffer is empty. 0 disables this mode.
Return values:
FTALK_SUCCESS Success
FTALK_ILLEGAL_ARGUMENT_ERROR Argument out of range
FTALK_ILLEGAL_STATE_ERROR Protocol is already open