This sketch shows the use of the Modbus communication protocol over RS-485 standard, and the use of the main functions of the library. Modbus allows for communication between many devices connected to the same network. There are many variants of Modbus protocols, but Waspmote implements the RTU format. Modbus RTU is the most common implementation available for Modbus.
Required Materials
1 x Waspmote
1 x Battery
1 x RS-485
Notes
- Waspmote must be powered with a battery
- There are many variants of Modbus protocols, but Waspmote implements the RTU format.
- Modbus RTU is the most common implementation available for Modbus.
- This example can be executed in Waspmote v15
Code
/* * ------ [RS-485_12] - Modbus Registers Map -------- * * This sketch shows the use of the Modbus communication protocol over * RS-485 standard, and the use of the main functions of the library. * Modbus allows for communication between many devices connected * to the same network. There are many variants of Modbus protocols, * but Waspmote implements the RTU format. Modbus RTU is the most * common implementation available for Modbus. * * Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version: 3.1 * Design: David Gascón * Implementation: Ahmad Saad *////////////////////////////////////////////////////////////////////////////////// RS-485 Modbus parameters///////////////////////////////////////////////////////////////////////////////// Include these libraries for using the RS-485 and Modbus functions#include<ModbusMaster.h>// TABLE contents#defineNAME0#defineADDRESS1#defineBYTES2#defineFCODE3// Define the size of the TABLE#defineNUM_REGISTERS5#defineNUM_PARAMETERS4// Registers names#defineSERIALNUMBER0#defineSOFTVERSION1#defineMODBUSADDRESS2#defineTEMPERATURE3#defineHUMIDITY4// Function codes used#defineREAD_HOLDING_REGISTERS0x03// Modbus Table Registersstaticconstint TABLE[NUM_REGISTERS][NUM_PARAMETERS] ={// NAME, ADDRESS, BYTES, FCODE { SERIALNUMBER,0,4, READ_HOLDING_REGISTERS } , { SOFTVERSION,4,2, READ_HOLDING_REGISTERS } , { MODBUSADDRESS,6,2, READ_HOLDING_REGISTERS } , { TEMPERATURE,101,2, READ_HOLDING_REGISTERS } , { HUMIDITY,304,2, READ_HOLDING_REGISTERS } };// Default address of Modbus device#defineDEFAULTADDRESS254// Instantiate ModbusMaster object as slave ID 254ModbusMaster node(DEFAULTADDRESS);// Variable to store the results of Modbus communicationint result;// Serial monitor messagesstaticconstchar* messages[]={"SERIAL NUMBER","SOFTWARE VERSION","MODBUS ADDRESS","TEMPERATURE","HUMIDITY",};voidsetup(){// Initialize USB port for debuggingUSB.ON();USB.println(F("Modbus table registers example"));// Initialize Modbus communication baud ratenode.begin(19200);}voidloop(){// Initial messageUSB.println(F("Reading Modbus device..."));USB.println(F("*****************************************"));// This loop will read all the registers// And prints in the serial monitor the resultsfor ( int i = SERIALNUMBER ; i <= HUMIDITY; i++) { // General function to read registers result =node.readRegisters(TABLE[i][ADDRESS] , TABLE[i][BYTES], TABLE[i][FCODE]);// result = 0: no errors// result != 0: error occurredif (result !=0) {// If no response from the slave, print an error messageUSB.print(F("Communication error while trying to read"));USB.println(messages[i]);USB.print(F("Result: "));USB.println(result);delay(100); } else {USB.print(messages[i]);USB.print(F(" => "));// Some registers return more than one valuefor (int i =0; i <node.available(); i++) {USB.print(node.getResponseBuffer(i) , DEC); } }USB.println(); }USB.println(F("*****************************************"));USB.println();delay(10000);}