Skip to content

Commit 2c3210e

Browse files
committed
Initial Arduino Client API's
1 parent 3b5076a commit 2c3210e

File tree

7 files changed

+422
-2
lines changed

7 files changed

+422
-2
lines changed

src/Modbus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef _MODBUS_H_INCLUDED
2121
#define _MODBUS_H_INCLUDED
2222

23-
#include <Arduino.h>
23+
#include "ModbusRTUClient.h"
24+
#include "ModbusTCPClient.h"
2425

2526
#endif

src/ModbusClient.cpp

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
/*
2+
This file is part of the Modbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <errno.h>
21+
22+
#include "ModbusClient.h"
23+
24+
ModbusClient::ModbusClient() :
25+
_mb(NULL)
26+
{
27+
}
28+
29+
ModbusClient::~ModbusClient()
30+
{
31+
if (_mb != NULL) {
32+
modbus_free(_mb);
33+
}
34+
}
35+
36+
int ModbusClient::begin(modbus_t* mb)
37+
{
38+
end();
39+
40+
_mb = mb;
41+
if (_mb == NULL) {
42+
return 0;
43+
}
44+
45+
if (modbus_connect(_mb) != 0) {
46+
modbus_free(_mb);
47+
48+
_mb = NULL;
49+
return 0;
50+
}
51+
52+
return 1;
53+
}
54+
55+
void ModbusClient::end()
56+
{
57+
if (_mb != NULL) {
58+
modbus_close(_mb);
59+
modbus_free(_mb);
60+
61+
_mb = NULL;
62+
}
63+
}
64+
65+
void ModbusClient::setId(int id)
66+
{
67+
modbus_set_slave(_mb, id);
68+
}
69+
70+
int ModbusClient::readCoil(int address)
71+
{
72+
uint8_t value;
73+
74+
if (readCoils(address, &value, 1) < 0) {
75+
return -1;
76+
}
77+
78+
return value;
79+
}
80+
81+
int ModbusClient::readCoils(int address, uint8_t values[], int nb)
82+
{
83+
if (modbus_read_bits(_mb, address, nb, values) < 0) {
84+
return 0;
85+
}
86+
87+
return 1;
88+
}
89+
90+
int ModbusClient::readDiscreteInput(int address)
91+
{
92+
uint8_t value;
93+
94+
if (!readDiscreteInputs(address, &value, 1)) {
95+
return -1;
96+
}
97+
98+
return value;
99+
}
100+
101+
int ModbusClient::readDiscreteInputs(int address, uint8_t values[], int nb)
102+
{
103+
if (modbus_read_input_bits(_mb, address, nb, values) < 0) {
104+
return 0;
105+
}
106+
107+
return 1;
108+
}
109+
110+
long ModbusClient::readHoldingRegister(int address)
111+
{
112+
uint16_t value;
113+
114+
if (!readHoldingRegisters(address, &value, 1)) {
115+
return -1;
116+
}
117+
118+
return value;
119+
}
120+
121+
int ModbusClient::readHoldingRegisters(int address, uint16_t values[], int nb)
122+
{
123+
if (modbus_read_registers(_mb, address, nb, values) < 0) {
124+
return 0;
125+
}
126+
127+
return 1;
128+
}
129+
130+
long ModbusClient::readInputRegister(int address)
131+
{
132+
uint16_t value;
133+
134+
if (!readInputRegisters(address, &value, 1)) {
135+
return -1;
136+
}
137+
138+
return value;
139+
}
140+
141+
int ModbusClient::readInputRegisters(int address, uint16_t values[], int nb)
142+
{
143+
if (modbus_read_input_registers(_mb, address, nb, values) < 0) {
144+
return 0;
145+
}
146+
147+
return 1;
148+
}
149+
150+
int ModbusClient::writeCoil(int address, uint8_t value)
151+
{
152+
if (modbus_write_bit(_mb, address, value) < 0) {
153+
return 0;
154+
}
155+
156+
return 1;
157+
}
158+
159+
int ModbusClient::writeCoils(int address, const uint8_t values[], int nb)
160+
{
161+
if (modbus_write_bits(_mb, address, nb, values) < 0) {
162+
return 0;
163+
}
164+
165+
return 1;
166+
}
167+
168+
int ModbusClient::writeRegister(int address, uint16_t value)
169+
{
170+
if (modbus_write_register(_mb, address, value) < 0) {
171+
return 0;
172+
}
173+
174+
return 1;
175+
}
176+
177+
int ModbusClient::writeRegisters(int address, const uint16_t values[], int nb)
178+
{
179+
if (modbus_write_registers(_mb, address, nb, values) < 0) {
180+
return 0;
181+
}
182+
183+
return 1;
184+
}
185+
186+
int ModbusClient::maskWriteRegister(int address, uint16_t andMask, uint16_t orMask)
187+
{
188+
if (modbus_mask_write_register(_mb, address, andMask, orMask) < 0) {
189+
return 0;
190+
}
191+
192+
return 1;
193+
}
194+
195+
int ModbusClient::writeAndReadRegisters(int writeAddress, const uint16_t writeValues[], int writeNb, int readAddress, uint16_t readValues[], int readNb)
196+
{
197+
if (modbus_write_and_read_registers(_mb, writeAddress, writeNb, writeValues, readAddress, readNb, readValues) < 0) {
198+
return 0;
199+
}
200+
201+
return 1;
202+
}
203+
204+
const char* ModbusClient::lastError()
205+
{
206+
if (errno == 0) {
207+
return NULL;
208+
}
209+
210+
return modbus_strerror(errno);
211+
}

src/ModbusClient.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
This file is part of the Modbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _MODBUS_CLIENT_H_INCLUDED
21+
#define _MODBUS_CLIENT_H_INCLUDED
22+
23+
extern "C" {
24+
#include "libmodbus/modbus.h"
25+
}
26+
27+
#include <Arduino.h>
28+
29+
class ModbusClient {
30+
31+
public:
32+
void setId(int id);
33+
34+
int readCoil(int address);
35+
int readCoils(int address, uint8_t values[], int nb);
36+
int readDiscreteInput(int address);
37+
int readDiscreteInputs(int address, uint8_t values[], int nb);
38+
long readHoldingRegister(int address);
39+
int readHoldingRegisters(int address, uint16_t values[], int nb);
40+
long readInputRegister(int address);
41+
int readInputRegisters(int address, uint16_t values[], int nb);
42+
43+
int writeCoil(int address, uint8_t value);
44+
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);
47+
int maskWriteRegister(int address, uint16_t andMask, uint16_t orMask);
48+
49+
int writeAndReadRegisters(int writeAddress, const uint16_t writeValues[], int writeNb, int readAddress, uint16_t readValues[], int readNb);
50+
51+
const char* lastError();
52+
53+
void end();
54+
55+
protected:
56+
ModbusClient();
57+
virtual ~ModbusClient();
58+
59+
int begin(modbus_t* _mb);
60+
61+
private:
62+
modbus_t* _mb;
63+
};
64+
65+
#endif

src/ModbusRTUClient.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is part of the Modbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <errno.h>
21+
22+
extern "C" {
23+
#include "libmodbus/modbus.h"
24+
#include "libmodbus/modbus-rtu.h"
25+
}
26+
27+
#include "ModbusRTUClient.h"
28+
29+
ModbusRTUClientClass::ModbusRTUClientClass()
30+
{
31+
}
32+
33+
ModbusRTUClientClass::~ModbusRTUClientClass()
34+
{
35+
}
36+
37+
int ModbusRTUClientClass::begin(unsigned long baudRate, uint16_t config)
38+
{
39+
modbus_t* mb = modbus_new_rtu(baudRate, config);
40+
41+
return ModbusClient::begin(mb);
42+
}
43+
44+
ModbusRTUClientClass ModbusRTUClient;

src/Modbus.cpp renamed to src/ModbusRTUClient.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,20 @@
1717
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919

20-
#include "Modbus.h"
20+
#ifndef _MODBUS_RTU_CLIENT_H_INCLUDED
21+
#define _MODBUS_RTU_CLIENT_H_INCLUDED
22+
23+
#include "ModbusClient.h"
24+
25+
class ModbusRTUClientClass : public ModbusClient {
26+
public:
27+
ModbusRTUClientClass();
28+
virtual ~ModbusRTUClientClass();
29+
30+
int begin(unsigned long baudRate, uint16_t config = SERIAL_8N1);
31+
void end();
32+
};
33+
34+
extern ModbusRTUClientClass ModbusRTUClient;
35+
36+
#endif

src/ModbusTCPClient.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
This file is part of the Modbus library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <errno.h>
21+
22+
extern "C" {
23+
#include "libmodbus/modbus.h"
24+
#include "libmodbus/modbus-tcp.h"
25+
}
26+
27+
#include "ModbusTCPClient.h"
28+
29+
ModbusTCPClient::ModbusTCPClient(Client& client) :
30+
_client(&client)
31+
{
32+
}
33+
34+
ModbusTCPClient::~ModbusTCPClient()
35+
{
36+
}
37+
38+
int ModbusTCPClient::begin(IPAddress ip, uint16_t port)
39+
{
40+
modbus_t* mb = modbus_new_tcp(_client, ip, port);
41+
42+
return ModbusClient::begin(mb);
43+
}

0 commit comments

Comments
 (0)