Lydia - Printhead
Loading...
Searching...
No Matches
Modbus Class Reference

Arduino class library for communicating with Modbus devices over USB/RS232/485 (via RTU protocol). More...

#include <ModbusRtu.h>

Public Member Functions

void begin ()
 
void begin (long u32speed)
 Initialize class object.
 
void begin (long u32speed, uint8_t u8config)
 Initialize class object.
 
void begin (SoftwareSerial *sPort, long u32speed)
 Initialize class object.
 
void clearError ()
 
void end ()
 finish any communication and release serial communication port
 
uint16_t getErrCnt ()
 error counter
 
uint8_t getID ()
 get slave ID between 1 and 247
 
uint16_t getInCnt ()
 number of incoming messages
 
uint8_t getLastError ()
 get last error message
 
uint16_t getOutCnt ()
 number of outcoming messages
 
uint8_t getState ()
 
uint16_t getTimeOut ()
 get communication watch-dog timer value
 
boolean getTimeOutState ()
 get communication watch-dog timer state
 
bool isLocked ()
 
 Modbus ()
 Default Constructor for Master through Serial.
 
 Modbus (uint8_t u8id)
 
 Modbus (uint8_t u8id, uint8_t u8serno)
 
 Modbus (uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
 
int8_t poll ()
 cyclic poll for master
 
int8_t poll (uint16_t *regs, uint8_t u8size)
 cyclic poll for slave
 
int8_t query (modbus_t telegram)
 only for master
 
void setID (uint8_t u8id)
 write new ID for the slave
 
void setTimeOut (uint16_t u16timeout)
 write communication watch-dog timer
 

Public Attributes

bool debugSend
 
uint8_t rxBuffer [MAX_BUFFER]
 
uint8_t rxSize
 

Private Member Functions

void buildException (uint8_t u8exception)
 This method builds an exception message.
 
uint16_t calcCRC (uint8_t u8length)
 This method calculates CRC.
 
void get_FC1 ()
 
void get_FC3 ()
 
int8_t getRxBuffer ()
 This method moves Serial buffer data to the Modbus au8Buffer.
 
void init (uint8_t u8id)
 
void init (uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
 
int8_t process_FC1 (uint16_t *regs, uint8_t u8size)
 This method processes functions 1 & 2 This method reads a bit array and transfers it to the master.
 
int8_t process_FC15 (uint16_t *regs, uint8_t u8size)
 This method processes function 15 This method writes a bit array assigned by the master.
 
int8_t process_FC16 (uint16_t *regs, uint8_t u8size)
 This method processes function 16 This method writes a word array assigned by the master.
 
int8_t process_FC3 (uint16_t *regs, uint8_t u8size)
 This method processes functions 3 & 4 This method reads a word array and transfers it to the master.
 
int8_t process_FC5 (uint16_t *regs, uint8_t u8size)
 This method processes function 5 This method writes a value assigned by the master to a single bit.
 
int8_t process_FC6 (uint16_t *regs, uint8_t u8size)
 This method processes function 6 This method writes a value assigned by the master to a single word.
 
void sendTxBuffer ()
 This method transmits au8Buffer to Serial line. Only if u8txenpin != 0, there is a flow handling in order to keep the RS485 transceiver in output state as long as the message is being sent. This is done with UCSRxA register. The CRC is appended to the buffer before starting to send it.
 
uint8_t validateAnswer ()
 This method validates master incoming messages.
 
uint8_t validateRequest ()
 This method validates slave incoming messages.
 

Private Attributes

uint16_tau16regs
 
uint8_t au8Buffer [MAX_BUFFER]
 
modbus_tcurrentQuery
 
HardwareSerialport
 Pointer to Serial class object.
 
SoftwareSerialsoftPort
 Pointer to SoftwareSerial class object.
 
uint16_t u16errCnt
 
uint16_t u16InCnt
 
uint16_t u16OutCnt
 
uint16_t u16timeOut
 
uint32_t u32time
 
uint32_t u32timeOut
 
uint8_t u8BufferSize
 
uint8_t u8id
 0=master, 1..247=slave number
 
uint8_t u8lastError
 
uint8_t u8lastRec
 
uint8_t u8regsize
 
uint8_t u8serno
 serial port: 0-Serial, 1..3-Serial1..Serial3; 4: use software serial
 
uint8_t u8state
 
uint8_t u8txenpin
 flow control pin: 0=USB or RS-232 mode, >0=RS-485 mode
 

Detailed Description

Arduino class library for communicating with Modbus devices over USB/RS232/485 (via RTU protocol).

Definition at line 152 of file ModbusRtu.h.

Constructor & Destructor Documentation

◆ Modbus() [1/3]

Modbus::Modbus ( uint8_t u8id,
uint8_t u8serno )

Definition at line 245 of file ModbusRtu.h.

246{
247 init(u8id, u8serno, 0);
248}
uint8_t u8id
0=master, 1..247=slave number
Definition ModbusRtu.h:157
uint8_t u8serno
serial port: 0-Serial, 1..3-Serial1..Serial3; 4: use software serial
Definition ModbusRtu.h:158
void init(uint8_t u8id, uint8_t u8serno, uint8_t u8txenpin)
Definition ModbusRtu.h:879

◆ Modbus() [2/3]

Modbus::Modbus ( uint8_t u8id,
uint8_t u8serno,
uint8_t u8txenpin )

Definition at line 263 of file ModbusRtu.h.

264{
266}
uint8_t u8txenpin
flow control pin: 0=USB or RS-232 mode, >0=RS-485 mode
Definition ModbusRtu.h:159

◆ Modbus() [3/3]

Modbus::Modbus ( uint8_t u8id)

Definition at line 282 of file ModbusRtu.h.

283{
284 init(u8id);
285}

Member Function Documentation

◆ begin()

void Modbus::begin ( )

Definition at line 450 of file ModbusRtu.h.

451{
452 begin(19200);
453}
void begin()
Definition ModbusRtu.h:450

◆ clearError()

void Modbus::clearError ( )

Definition at line 550 of file ModbusRtu.h.

551{
552 u16errCnt = 0;
553}
uint16_t u16errCnt
Definition ModbusRtu.h:166

◆ end()

void Modbus::end ( )

finish any communication and release serial communication port

◆ getTimeOut()

uint16_t Modbus::getTimeOut ( )

get communication watch-dog timer value

◆ init() [1/2]

void Modbus::init ( uint8_t u8id)
private

Definition at line 890 of file ModbusRtu.h.

891{
892 this->u8id = u8id;
893 this->u8serno = 4;
894 this->u8txenpin = 0;
895 this->u16timeOut = 1000;
896}
uint16_t u16timeOut
Definition ModbusRtu.h:167

◆ init() [2/2]

void Modbus::init ( uint8_t u8id,
uint8_t u8serno,
uint8_t u8txenpin )
private

Definition at line 879 of file ModbusRtu.h.

880{
881 this->u8id = u8id;
882 this->u8serno = (u8serno > 3) ? 0 : u8serno;
883
884 // this->u8txenpin = u8txenpin;
885 this->u8txenpin = 2; // Ignore input parameter u8txenpin and hardcode 2 for compatibility
886
887 this->u16timeOut = 1000;
888}

◆ isLocked()

bool Modbus::isLocked ( )

Definition at line 344 of file ModbusRtu.h.

345{
347}
modbus_t * currentQuery
Definition ModbusRtu.h:178
uint8_t flags
Definition ModbusRtu.h:74

Member Data Documentation

◆ au16regs

uint16_t* Modbus::au16regs
private

Definition at line 165 of file ModbusRtu.h.

◆ au8Buffer

uint8_t Modbus::au8Buffer[MAX_BUFFER]
private

Definition at line 162 of file ModbusRtu.h.

◆ currentQuery

modbus_t* Modbus::currentQuery
private

Definition at line 178 of file ModbusRtu.h.

◆ debugSend

bool Modbus::debugSend

Definition at line 214 of file ModbusRtu.h.

◆ port

HardwareSerial* Modbus::port
private

Pointer to Serial class object.

Definition at line 155 of file ModbusRtu.h.

◆ rxBuffer

uint8_t Modbus::rxBuffer[MAX_BUFFER]

Definition at line 217 of file ModbusRtu.h.

◆ rxSize

uint8_t Modbus::rxSize

Definition at line 216 of file ModbusRtu.h.

◆ softPort

SoftwareSerial* Modbus::softPort
private

Pointer to SoftwareSerial class object.

Definition at line 156 of file ModbusRtu.h.

◆ u16errCnt

uint16_t Modbus::u16errCnt
private

Definition at line 166 of file ModbusRtu.h.

◆ u16InCnt

uint16_t Modbus::u16InCnt
private

Definition at line 166 of file ModbusRtu.h.

◆ u16OutCnt

uint16_t Modbus::u16OutCnt
private

Definition at line 166 of file ModbusRtu.h.

◆ u16timeOut

uint16_t Modbus::u16timeOut
private

Definition at line 167 of file ModbusRtu.h.

◆ u32time

uint32_t Modbus::u32time
private

Definition at line 168 of file ModbusRtu.h.

◆ u32timeOut

uint32_t Modbus::u32timeOut
private

Definition at line 168 of file ModbusRtu.h.

◆ u8BufferSize

uint8_t Modbus::u8BufferSize
private

Definition at line 163 of file ModbusRtu.h.

◆ u8id

uint8_t Modbus::u8id
private

0=master, 1..247=slave number

Definition at line 157 of file ModbusRtu.h.

◆ u8lastError

uint8_t Modbus::u8lastError
private

Definition at line 161 of file ModbusRtu.h.

◆ u8lastRec

uint8_t Modbus::u8lastRec
private

Definition at line 164 of file ModbusRtu.h.

◆ u8regsize

uint8_t Modbus::u8regsize
private

Definition at line 169 of file ModbusRtu.h.

◆ u8serno

uint8_t Modbus::u8serno
private

serial port: 0-Serial, 1..3-Serial1..Serial3; 4: use software serial

Definition at line 158 of file ModbusRtu.h.

◆ u8state

uint8_t Modbus::u8state
private

Definition at line 160 of file ModbusRtu.h.

◆ u8txenpin

uint8_t Modbus::u8txenpin
private

flow control pin: 0=USB or RS-232 mode, >0=RS-485 mode

Definition at line 159 of file ModbusRtu.h.


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