BusBricks: CommInterface_modbusRTU Class Reference
BusBricks  0.1
Customize bus-communication
Loading...
Searching...
No Matches
CommInterface_modbusRTU Class Reference

The Modbus-RTU CommInterface-class specifies a communication interface with SoftwareSerial as interface-type and Monitors modbus-specific timeouts. Ensures correct data-transfer between standardized Frame-Objects in the Interface-Stack and SoftwareSerial. More...

#include <CommInterface_modbusRTU.h>

Inheritance diagram for CommInterface_modbusRTU:
[legend]
Collaboration diagram for CommInterface_modbusRTU:
[legend]

Public Member Functions

 CommInterface_modbusRTU (SoftwareSerial *softwareserial, uint16_t baudrate, char deviceId)
 Construct a new CommInterface_modbusRTU object.
 
 ~CommInterface_modbusRTU ()
 Destroy the CommInterface_modbusRTU object.
 
bool send () override
 Override of CommInterface send-function. Send the frame, sendBuffer is pointing to by writing and flushing SoftwareSerial-interface.
 
bool receive () override
 Receives a Modbus RTU frame and writes it to receiveBuffer.
 
char getDeviceId ()
 Get Modbus-specific device-identifier.
 
- Public Member Functions inherited from CommInterface< SoftwareSerial >
 CommInterface (SoftwareSerial *interface, uint32_t baudrate)
 Construct a new Comm-Interface object.
 
void sendNewFrame (CharArray *sendFrame)
 Specify the next frame to be sent by the interface, no overwrite, if the sendBuffer is already pointing to another frame.
 
bool finishedSending ()
 Check, if the Frame was sent and the CommInterface is ready to send the next Frame Interface is ready to send a new frame, if the sendBuffer is nullpointer.
 
void getReceivedFrame (CharArray *externalRecBuffer)
 Define the destination, the next received Frame should be stored at.
 
bool receivedNewFrame ()
 Check, if a new Frame was received and stored at the in getReceivedFrame specified location the interface is waiting waiting for getReceivedFrame-call to specify the location, the next received frame should be stored at.
 
virtual void receiveCycle ()
 Execution of receive-cycle, set the receiveBuffer to nullptr after a new frame was received.
 
virtual void sendCycle ()
 Execution of send-cycle, set the sendBuffer to nullptr, after the frame, sendBuffer was pointing at, was send.
 
- Public Member Functions inherited from CommInterfaceBase
virtual void setup_interface ()
 Setup the Interface has to be called in Setup-function.
 
 CommInterfaceBase ()
 Construct a new Comm Interface Base object.
 
 ~CommInterfaceBase ()
 Destroy the Comm-Interface Base object.
 
- Public Member Functions inherited from ErrorState
 ErrorState ()
 Default constructor for ErrorState.
 
 ~ErrorState ()
 Destroy the ErrorState object.
 
void raiseError (errorCodes code)
 raises a new error by setting the given error-code as errorState
 
errorCodes getErrorState ()
 Get the currently active error-code.
 
void clearErrorState ()
 sets the current error-state to "no Error"
 

Additional Inherited Members

- Protected Member Functions inherited from CommInterface< SoftwareSerial >
- Protected Attributes inherited from CommInterface< SoftwareSerial >
SoftwareSerialinterface
 pointer to an instance of the native bus-interface (setup outside of BusBricks)
 
CharArraysendBuffer
 pointer to the next frame to be send, set to nullptr if Frame was sent
 
CharArrayreceiveBuffer
 pointer to CharArray-object, a received frame should be stored at, set to nullptr if Frame was copied to destination
 

Detailed Description

The Modbus-RTU CommInterface-class specifies a communication interface with SoftwareSerial as interface-type and Monitors modbus-specific timeouts. Ensures correct data-transfer between standardized Frame-Objects in the Interface-Stack and SoftwareSerial.

Constructor & Destructor Documentation

◆ CommInterface_modbusRTU()

CommInterface_modbusRTU::CommInterface_modbusRTU ( SoftwareSerial * softwareserial,
uint16_t baudrate,
char deviceId )

Construct a new CommInterface_modbusRTU object.

Parameters
softwareserialpointer to the SoftwareSerial-instance, the Comminterface should use
baudratebaudrate in bps to calculate timeouts
deviceIdModbus-specific device-identifier

Member Function Documentation

◆ getDeviceId()

char CommInterface_modbusRTU::getDeviceId ( )

Get Modbus-specific device-identifier.

Returns
char Modbus-specific device-identifier

◆ receive()

bool CommInterface_modbusRTU::receive ( )
overridevirtual

Receives a Modbus RTU frame and writes it to receiveBuffer.

Waits for incoming bytes for a duration defined by _recTimeout. If no bytes are received within this period, the function exits. When bytes are received via SoftwareSerial, the function starts writing the frame to receiveBuffer once a byte containing the device ID of the CommInterface instance is detected. If the device ID is the null terminator (master mode), all incoming frames are captured. Bytes related to the frame must be received within the _charTimeout.

The function continues receiving until either MAXFRAMESIZE is reached or _charTimeout expires. After receiving, it waits for _frameTimeout before clearing the SoftwareSerial receive buffer by calling _clearRxBuffer.

Arbitration-errors (new frame received during bus-silence) are handled by waiting for a complete bus-silence-cycle.

Returns
true if a frame was successfully received and written to receiveBuffer within the receive timeout.
false if no frame was received during _recTimeout.

The function operates in two modes:

  • Slave Mode: Begins receiving only if the frame is addressed to the device's ID.
  • Master Mode: Captures any incoming frame.

The function does not validate the content of the received frame. It uses timeouts to ensure the entire frame is received and then clears the communication interface's buffer.

Note
If DEBUG is defined, debugging information is printed to the serial monitor.

Implements CommInterface< SoftwareSerial >.

Here is the call graph for this function:

◆ send()

bool CommInterface_modbusRTU::send ( )
overridevirtual

Override of CommInterface send-function. Send the frame, sendBuffer is pointing to by writing and flushing SoftwareSerial-interface.

Returns
true frame was flushed to serial-bus
false sendBuffer is nullptr

Implements CommInterface< SoftwareSerial >.

Here is the call graph for this function:

The documentation for this class was generated from the following files: