Skip to content

Commit 14654a6

Browse files
committed
Initial documention
1 parent b709f93 commit 14654a6

File tree

7 files changed

+191
-11
lines changed

7 files changed

+191
-11
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
= Modbus Library for Arduino =
22

3+
Use Modbus with your Arduino.
4+
5+
Using the RS485 shields, like the MKR 485 shield. This library depends on the ArduinoRS485 library.
6+
37
For more information about this library please visit us at
48
http://www.arduino.cc/en/Reference/ArduinoModbus
59

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name=ArduinoModbus
22
version=1.0.0
33
author=Arduino
44
maintainer=Arduino <info@arduino.cc>
5-
sentence=
6-
paragraph=
5+
sentence=Use Modbus equipment with your Arduino.
6+
paragraph=Using the RS485 shields, like the MKR 485 shield. This library depends on the RS485 library.
77
category=Communication
88
url=http://www.arduino.cc/en/Reference/ArduinoModbus
99
architectures=*

src/ModbusClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int ModbusClient::writeCoils(int address, const uint8_t values[], int nb)
165165
return 1;
166166
}
167167

168-
int ModbusClient::writeRegister(int address, uint16_t value)
168+
int ModbusClient::writeHoldingRegister(int address, uint16_t value)
169169
{
170170
if (modbus_write_register(_mb, address, value) < 0) {
171171
return 0;
@@ -174,7 +174,7 @@ int ModbusClient::writeRegister(int address, uint16_t value)
174174
return 1;
175175
}
176176

177-
int ModbusClient::writeRegisters(int address, const uint16_t values[], int nb)
177+
int ModbusClient::writeHoldingRegisters(int address, const uint16_t values[], int nb)
178178
{
179179
if (modbus_write_registers(_mb, address, nb, values) < 0) {
180180
return 0;

src/ModbusClient.h

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,184 @@ extern "C" {
2929
class ModbusClient {
3030

3131
public:
32+
/**
33+
* Set the (slave) id to use for read and write operations.
34+
* Defaults to 0x00 (broadcast).
35+
*
36+
* @param id id to use
37+
*/
3238
void setId(int id);
3339

40+
/**
41+
* Perform a "Read Coils" operation for the specified address for a single
42+
* coil.
43+
*
44+
* @param address address to use for operation
45+
*
46+
* @return coil value on success, -1 on failure.
47+
*/
3448
int readCoil(int address);
49+
50+
/**
51+
* Perform a "Read Coils" operation for the specified address and number of
52+
* coils.
53+
*
54+
* @param address start address to use for operation
55+
* @param values array of bytes to store coil values
56+
* @param nb number of coils to read
57+
*
58+
* @return 1 success, 0 on failure.
59+
*/
3560
int readCoils(int address, uint8_t values[], int nb);
61+
62+
/**
63+
* Perform a "Read Discrete Inputs" operation for the specified address for a
64+
* single discrete input.
65+
*
66+
* @param address address to use for operation
67+
*
68+
* @return discrete input value on success, -1 on failure.
69+
*/
3670
int readDiscreteInput(int address);
71+
72+
/**
73+
* Perform a "Read Discrete Inputs" operation for the specified address and
74+
* number of inputs.
75+
*
76+
* @param address start address to use for operation
77+
* @param values array of bytes to store discrete input values
78+
* @param nb number of discrete inputs to read
79+
*
80+
* @return 1 success, 0 on failure.
81+
*/
3782
int readDiscreteInputs(int address, uint8_t values[], int nb);
83+
84+
/**
85+
* Perform a "Read Holding Registers" operation for a single holding
86+
* register.
87+
*
88+
* @param address start address to use for operation
89+
*
90+
* @return holiding register value on success, -1 on failure.
91+
*/
3892
long readHoldingRegister(int address);
93+
94+
/**
95+
* Perform a "Read Holding Registers" operation for the specified address and
96+
* number of holding registers.
97+
*
98+
* @param address start address to use for operation
99+
* @param values array of words to store holding register values
100+
* @param nb number of holding registers to read
101+
*
102+
* @return 1 success, 0 on failure.
103+
*/
39104
int readHoldingRegisters(int address, uint16_t values[], int nb);
105+
106+
/**
107+
* Perform a "Read Input Registers" operation for a single input
108+
* register.
109+
*
110+
* @param address address to use for operation
111+
*
112+
* @return input register value on success, -1 on failure.
113+
*/
40114
long readInputRegister(int address);
115+
116+
/**
117+
* Perform a "Read Input Registers" operation for the specified address and
118+
* number of input registers.
119+
*
120+
* @param address start address to use for operation
121+
* @param values array of words to store input register values
122+
* @param nb number of holding input to read
123+
*
124+
* @return 1 success, 0 on failure.
125+
*/
41126
int readInputRegisters(int address, uint16_t values[], int nb);
42127

128+
/**
129+
* Perform a "Write Single Coil" operation for the specified address and
130+
* value.
131+
*
132+
* @param address address to use for operation
133+
* @param value coil value to write
134+
*
135+
* @return 1 on success, 0 on failure.
136+
*/
43137
int writeCoil(int address, uint8_t value);
138+
139+
/**
140+
* Perform a "Write Multiple Coils" operation for the specified address and
141+
* values.
142+
*
143+
* @param address start address to use for operation
144+
* @param values array of coil values to write
145+
* @param nb number of coil values to write
146+
*
147+
* @return 1 on success, 0 on failure.
148+
*/
44149
int writeCoils(int address, const uint8_t values[], int nb);
45-
int writeRegister(int address, uint16_t value);
46-
int writeRegisters(int address, const uint16_t values[], int nb);
150+
151+
/**
152+
* Perform a "Write Single Holding Register" operation for the specified
153+
* address and value.
154+
*
155+
* @param address address to use for operation
156+
* @param value holding register value to write
157+
*
158+
* @return 1 on success, 0 on failure.
159+
*/
160+
int writeHoldingRegister(int address, uint16_t value);
161+
162+
/**
163+
* Perform a "Write Multiple Holding Registers" operation for the specified
164+
* address and values.
165+
*
166+
* @param address start address to use for operation
167+
* @param values array of holding register values to write
168+
* @param nb number of holding register values to write
169+
*
170+
* @return 1 on success, 0 on failure.
171+
*/
172+
int writeHoldingRegisters(int address, const uint16_t values[], int nb);
173+
174+
/**
175+
* Perform a "Mask Write Registers" operation for the specified
176+
* address, AND mask and OR mask.
177+
*
178+
* @param address address to use for operation
179+
* @param andMask AND mask to use for operation
180+
* @param orMask OR mask to use for operation
181+
*
182+
* @return 1 on success, 0 on failure.
183+
*/
47184
int maskWriteRegister(int address, uint16_t andMask, uint16_t orMask);
48185

186+
/**
187+
* Perform a "Read/Write Registers" operation.
188+
*
189+
* @param writeAddress write address to use for operation
190+
* @param writeValues array of words to write
191+
* @param writeNb number of registers to write
192+
* @param readAddress read address to use for operation
193+
* @param readValues array of words to store register values
194+
* @param readNb number of registers to read
195+
*
196+
* @return 1 on success, 0 on failure.
197+
*/
49198
int writeAndReadRegisters(int writeAddress, const uint16_t writeValues[], int writeNb, int readAddress, uint16_t readValues[], int readNb);
50199

200+
/**
201+
* Read the last error reason as a string
202+
*
203+
* @return Last error reason as a C string
204+
*/
51205
const char* lastError();
52206

207+
/**
208+
* Stop the client and clean up
209+
*/
53210
void end();
54211

55212
protected:

src/ModbusRTUClient.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ ModbusRTUClientClass::~ModbusRTUClientClass()
3434
{
3535
}
3636

37-
int ModbusRTUClientClass::begin(unsigned long baudRate, uint16_t config)
37+
int ModbusRTUClientClass::begin(unsigned long baudrate, uint16_t config)
3838
{
39-
modbus_t* mb = modbus_new_rtu(baudRate, config);
39+
modbus_t* mb = modbus_new_rtu(baudrate, config);
4040

4141
return ModbusClient::begin(mb);
4242
}

src/ModbusRTUClient.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ class ModbusRTUClientClass : public ModbusClient {
2727
ModbusRTUClientClass();
2828
virtual ~ModbusRTUClientClass();
2929

30-
int begin(unsigned long baudRate, uint16_t config = SERIAL_8N1);
31-
void end();
30+
/**
31+
* Start the Modbus RTU client with the specified parameters
32+
*
33+
* @param baudrate Baud rate to use
34+
* @param config serial config. to use defaults to SERIAL_8N1
35+
*
36+
* Return 1 on success, 0 on failure
37+
*/
38+
int begin(unsigned long baudrate, uint16_t config = SERIAL_8N1);
3239
};
3340

3441
extern ModbusRTUClientClass ModbusRTUClient;

src/ModbusTCPClient.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,23 @@
2727

2828
class ModbusTCPClient : public ModbusClient {
2929
public:
30+
/**
31+
* ModbusTCPClient constructor
32+
*
33+
* @param client Client to use for TCP connection
34+
*/
3035
ModbusTCPClient(Client& client);
3136
virtual ~ModbusTCPClient();
3237

38+
/**
39+
* Start the Modbus TCP client with the specified parameters
40+
*
41+
* @param ip IP Address of the Modbus server
42+
* @param port TCP port number of Modbus server, defaults to 502
43+
*
44+
* Return 1 on success, 0 on failure
45+
*/
3346
int begin(IPAddress ip, uint16_t port = 502);
34-
void end();
3547

3648
private:
3749
Client* _client;

0 commit comments

Comments
 (0)