Code examples and extended information
For more information about the Waspmote hardware platform go to:
In the Waspmote Development section you can find complete examples:
Example:
1
/*
2
* ------ [RS-485_12] - Modbus Registers Map --------
3
*
4
* This sketch shows the use of the Modbus communication protocol over
5
* RS-485 standard, and the use of the main functions of the library.
6
* Modbus allows for communication between many devices connected
7
* to the same network. There are many variants of Modbus protocols,
8
* but Waspmote implements the RTU format. Modbus RTU is the most
9
* common implementation available for Modbus.
10
*
11
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
12
* http://www.libelium.com
13
*
14
* This program is free software: you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation, either version 3 of the License, or
17
* (at your option) any later version.
18
*
19
* This program is distributed in the hope that it will be useful,
20
* but WITHOUT ANY WARRANTY; without even the implied warranty of
21
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
* GNU General Public License for more details.
23
*
24
* You should have received a copy of the GNU General Public License
25
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
*
27
* Version: 3.1
28
* Design: David Gascón
29
* Implementation: Ahmad Saad
30
*/
31
32
///////////////////////////////////////////////////////////////////////////////
33
// RS-485 Modbus parameters
34
///////////////////////////////////////////////////////////////////////////////
35
36
// Include these libraries for using the RS-485 and Modbus functions
37
#include <ModbusMaster.h>
38
39
// TABLE contents
40
#define NAME 0
41
#define ADDRESS 1
42
#define BYTES 2
43
#define FCODE 3
44
45
// Define the size of the TABLE
46
#define NUM_REGISTERS 5
47
#define NUM_PARAMETERS 4
48
49
// Registers names
50
#define SERIALNUMBER 0
51
#define SOFTVERSION 1
52
#define MODBUSADDRESS 2
53
#define TEMPERATURE 3
54
#define HUMIDITY 4
55
56
// Function codes used
57
#define READ_HOLDING_REGISTERS 0x03
58
59
60
// Modbus Table Registers
61
static const int TABLE[NUM_REGISTERS][NUM_PARAMETERS] =
62
{
63
// NAME, ADDRESS, BYTES, FCODE
64
{ SERIALNUMBER, 0, 4, READ_HOLDING_REGISTERS } ,
65
{ SOFTVERSION, 4, 2, READ_HOLDING_REGISTERS } ,
66
{ MODBUSADDRESS, 6, 2, READ_HOLDING_REGISTERS } ,
67
{ TEMPERATURE, 101, 2, READ_HOLDING_REGISTERS } ,
68
{ HUMIDITY, 304, 2, READ_HOLDING_REGISTERS }
69
};
70
71
// Default address of Modbus device
72
#define DEFAULTADDRESS 254
73
74
// Instantiate ModbusMaster object as slave ID 254
75
ModbusMaster node(DEFAULTADDRESS);
76
77
// Variable to store the results of Modbus communication
78
int result;
79
80
// Serial monitor messages
81
static const char* messages[] =
82
{
83
"SERIAL NUMBER",
84
"SOFTWARE VERSION",
85
"MODBUS ADDRESS",
86
"TEMPERATURE",
87
"HUMIDITY",
88
};
89
90
void setup()
91
{
92
// Initialize USB port for debugging
93
USB.ON();
94
USB.println(F("Modbus table registers example"));
95
96
// Initialize Modbus communication baud rate
97
node.begin(19200);
98
}
99
100
101
void loop()
102
{
103
// Initial message
104
USB.println(F("Reading Modbus device..."));
105
USB.println(F("*****************************************"));
106
107
// This loop will read all the registers
108
// And prints in the serial monitor the results
109
for ( int i = SERIALNUMBER ; i <= HUMIDITY; i++) {
110
// General function to read registers
111
result = node.readRegisters(TABLE[i][ADDRESS] , TABLE[i][BYTES], TABLE[i][FCODE]);
112
113
// result = 0: no errors
114
// result != 0: error occurred
115
if (result !=0) {
116
// If no response from the slave, print an error message
117
USB.print(F("Communication error while trying to read"));
118
USB.println(messages[i]);
119
USB.print(F("Result: "));
120
USB.println(result);
121
delay(100);
122
}
123
else {
124
USB.print(messages[i]);
125
USB.print(F(" => "));
126
127
// Some registers return more than one value
128
for (int i = 0; i < node.available(); i++)
129
{
130
USB.print(node.getResponseBuffer(i) , DEC);
131
}
132
}
133
USB.println();
134
}
135
136
USB.println(F("*****************************************"));
137
USB.println();
138
delay(10000);
139
}
Copied!
Last modified 1yr ago
Copy link