ModbusMaster
v2.0.1
Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
|
Arduino class library for communicating with Modbus slaves over RS232/485 (via RTU protocol). More...
#include <ModbusMaster.h>
Public Member Functions | |
ModbusMaster () | |
Constructor. More... | |
void | begin (uint8_t, Stream &serial) |
Initialize class object. More... | |
void | idle (void(*)()) |
Set idle time callback function (cooperative multitasking). More... | |
void | preTransmission (void(*)()) |
Set pre-transmission callback function. More... | |
void | postTransmission (void(*)()) |
Set post-transmission callback function. More... | |
uint16_t | getResponseBuffer (uint8_t) |
Retrieve data from response buffer. More... | |
void | clearResponseBuffer () |
Clear Modbus response buffer. More... | |
uint8_t | setTransmitBuffer (uint8_t, uint16_t) |
Place data in transmit buffer. More... | |
void | clearTransmitBuffer () |
Clear Modbus transmit buffer. More... | |
void | beginTransmission (uint16_t) |
uint8_t | requestFrom (uint16_t, uint16_t) |
void | sendBit (bool) |
void | send (uint8_t) |
void | send (uint16_t) |
void | send (uint32_t) |
uint8_t | available (void) |
uint16_t | receive (void) |
uint8_t | readCoils (uint16_t, uint16_t) |
Modbus function 0x01 Read Coils. More... | |
uint8_t | readDiscreteInputs (uint16_t, uint16_t) |
Modbus function 0x02 Read Discrete Inputs. More... | |
uint8_t | readHoldingRegisters (uint16_t, uint16_t) |
Modbus function 0x03 Read Holding Registers. More... | |
uint8_t | readInputRegisters (uint16_t, uint8_t) |
Modbus function 0x04 Read Input Registers. More... | |
uint8_t | writeSingleCoil (uint16_t, uint8_t) |
Modbus function 0x05 Write Single Coil. More... | |
uint8_t | writeSingleRegister (uint16_t, uint16_t) |
Modbus function 0x06 Write Single Register. More... | |
uint8_t | writeMultipleCoils (uint16_t, uint16_t) |
Modbus function 0x0F Write Multiple Coils. More... | |
uint8_t | writeMultipleCoils () |
uint8_t | writeMultipleRegisters (uint16_t, uint16_t) |
Modbus function 0x10 Write Multiple Registers. More... | |
uint8_t | writeMultipleRegisters () |
uint8_t | maskWriteRegister (uint16_t, uint16_t, uint16_t) |
Modbus function 0x16 Mask Write Register. More... | |
uint8_t | readWriteMultipleRegisters (uint16_t, uint16_t, uint16_t, uint16_t) |
Modbus function 0x17 Read Write Multiple Registers. More... | |
uint8_t | readWriteMultipleRegisters (uint16_t, uint16_t) |
Static Public Attributes | |
static const uint8_t | ku8MBIllegalFunction = 0x01 |
Modbus protocol illegal function exception. More... | |
static const uint8_t | ku8MBIllegalDataAddress = 0x02 |
Modbus protocol illegal data address exception. More... | |
static const uint8_t | ku8MBIllegalDataValue = 0x03 |
Modbus protocol illegal data value exception. More... | |
static const uint8_t | ku8MBSlaveDeviceFailure = 0x04 |
Modbus protocol slave device failure exception. More... | |
static const uint8_t | ku8MBSuccess = 0x00 |
ModbusMaster success. More... | |
static const uint8_t | ku8MBInvalidSlaveID = 0xE0 |
ModbusMaster invalid response slave ID exception. More... | |
static const uint8_t | ku8MBInvalidFunction = 0xE1 |
ModbusMaster invalid response function exception. More... | |
static const uint8_t | ku8MBResponseTimedOut = 0xE2 |
ModbusMaster response timed out exception. More... | |
static const uint8_t | ku8MBInvalidCRC = 0xE3 |
ModbusMaster invalid response CRC exception. More... | |
Private Member Functions | |
uint8_t | ModbusMasterTransaction (uint8_t u8MBFunction) |
Modbus transaction engine. More... | |
Private Attributes | |
Stream * | _serial |
reference to serial port object | |
uint8_t | _u8MBSlave |
Modbus slave (1..255) initialized in begin() | |
uint16_t | _u16ReadAddress |
slave register from which to read | |
uint16_t | _u16ReadQty |
quantity of words to read | |
uint16_t | _u16ResponseBuffer [ku8MaxBufferSize] |
buffer to store Modbus slave response; read via GetResponseBuffer() | |
uint16_t | _u16WriteAddress |
slave register to which to write | |
uint16_t | _u16WriteQty |
quantity of words to write | |
uint16_t | _u16TransmitBuffer [ku8MaxBufferSize] |
buffer containing data to transmit to Modbus slave; set via SetTransmitBuffer() | |
uint16_t * | txBuffer |
uint8_t | _u8TransmitBufferIndex |
uint16_t | u16TransmitBufferLength |
uint16_t * | rxBuffer |
uint8_t | _u8ResponseBufferIndex |
uint8_t | _u8ResponseBufferLength |
void(* | _idle )() |
void(* | _preTransmission )() |
void(* | _postTransmission )() |
Static Private Attributes | |
static const uint8_t | ku8MaxBufferSize = 64 |
size of response/transmit buffers | |
static const uint8_t | ku8MBReadCoils = 0x01 |
Modbus function 0x01 Read Coils. | |
static const uint8_t | ku8MBReadDiscreteInputs = 0x02 |
Modbus function 0x02 Read Discrete Inputs. | |
static const uint8_t | ku8MBWriteSingleCoil = 0x05 |
Modbus function 0x05 Write Single Coil. | |
static const uint8_t | ku8MBWriteMultipleCoils = 0x0F |
Modbus function 0x0F Write Multiple Coils. | |
static const uint8_t | ku8MBReadHoldingRegisters = 0x03 |
Modbus function 0x03 Read Holding Registers. | |
static const uint8_t | ku8MBReadInputRegisters = 0x04 |
Modbus function 0x04 Read Input Registers. | |
static const uint8_t | ku8MBWriteSingleRegister = 0x06 |
Modbus function 0x06 Write Single Register. | |
static const uint8_t | ku8MBWriteMultipleRegisters = 0x10 |
Modbus function 0x10 Write Multiple Registers. | |
static const uint8_t | ku8MBMaskWriteRegister = 0x16 |
Modbus function 0x16 Mask Write Register. | |
static const uint8_t | ku8MBReadWriteMultipleRegisters = 0x17 |
Modbus function 0x17 Read Write Multiple Registers. | |
static const uint16_t | ku16MBResponseTimeout = 2000 |
Modbus timeout [milliseconds]. | |
Arduino class library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
void ModbusMaster::idle | ( | void(*)() | idle | ) |
Set idle time callback function (cooperative multitasking).
This function gets called in the idle time between transmission of data and response from slave. Do not call functions that read from the serial buffer that is used by ModbusMaster. Use of i2c/TWI, 1-Wire, other serial ports, etc. is permitted within callback function.
void ModbusMaster::preTransmission | ( | void(*)() | preTransmission | ) |
Set pre-transmission callback function.
This function gets called just before a Modbus message is sent over serial. Typical usage of this callback is to enable an RS485 transceiver's Driver Enable pin, and optionally disable its Receiver Enable pin.
void ModbusMaster::postTransmission | ( | void(*)() | postTransmission | ) |
Set post-transmission callback function.
This function gets called after a Modbus message has finished sending (i.e. after all data has been physically transmitted onto the serial bus).
Typical usage of this callback is to enable an RS485 transceiver's Receiver Enable pin, and disable its Driver Enable pin.
|
private |
Modbus transaction engine.
Sequence:
u8MBFunction | Modbus function (0x01..0xFF) |