Skip to content

Commit 4d4b869

Browse files
committed
Add callback assign methods, callback example
1 parent 1baae6e commit 4d4b869

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Modbus-Arduino Example - Test Led using callback (Modbus IP ESP8266/ESP32)
3+
Control a Led on D4 pin using Write Single Coil Modbus Function
4+
Original library
5+
Copyright by André Sarmento Barbosa
6+
http://github.com/andresarmento/modbus-arduino
7+
8+
Current version
9+
(c)2017 Alexander Emelianov ([email protected])
10+
https://github.com/emelianov/modbus-esp8266
11+
*/
12+
13+
#ifdef ESP8266
14+
#include <ESP8266WiFi.h>
15+
#else
16+
#include <WiFi.h>
17+
#endif
18+
#include <ModbusIP_ESP8266.h>
19+
20+
//Modbus Registers Offsets (0-9999)
21+
const int LED_COIL = 100;
22+
//Used Pins
23+
const int ledPin = D4; // Builtin ESP8266 LED
24+
25+
//ModbusIP object
26+
ModbusIP mb;
27+
28+
// Callback function
29+
uint16_t cbLed(TRegister* reg, uint16_t val) {
30+
//Attach ledPin to LED_COIL register
31+
digitalWrite(ledPin, (val == 0xFF00));
32+
return val;
33+
}
34+
35+
void setup() {
36+
Serial.begin(74880);
37+
38+
WiFi.begin("ssid", "pass");
39+
40+
while (WiFi.status() != WL_CONNECTED) {
41+
delay(500);
42+
Serial.print(".");
43+
}
44+
45+
Serial.println("");
46+
Serial.println("WiFi connected");
47+
Serial.println("IP address: ");
48+
Serial.println(WiFi.localIP());
49+
50+
mb.begin();
51+
52+
pinMode(ledPin, OUTPUT);
53+
mb.addCoil(LED_COIL); // Add Coil
54+
mb.onSet(COIL(LED_COIL), cbLed); // Add callback on Coil LED_COIL value set
55+
}
56+
57+
void loop() {
58+
//Call once inside loop() - all magic here
59+
mb.task();
60+
}

src/Modbus.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
Modbus.h - Header for Modbus Base Library
33
Copyright (C) 2014 André Sarmento Barbosa
4+
2017 Alexander Emelianov ([email protected])
45
*/
56
#include "Modbus.h"
67

@@ -515,6 +516,23 @@ void Modbus::writeMultipleCoils(byte* frame,uint16_t startreg, uint16_t numoutpu
515516
_reply = MB_REPLY_NORMAL;
516517
}
517518
#endif
519+
bool Modbus::onGet(uint16_t address, cbModbus cb) {
520+
TRegister* reg = this->searchRegister(address);
521+
if (reg) {
522+
reg->get = cb;
523+
return true;
524+
}
525+
return false;
526+
}
527+
bool Modbus::onSet(uint16_t address, cbModbus cb) {
528+
TRegister* reg = this->searchRegister(address);
529+
if (reg) {
530+
reg->set = cb;
531+
return true;
532+
}
533+
return false;
534+
}
535+
518536

519537

520538

src/Modbus.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
/*
22
Modbus.h - Header for Modbus Base Library
33
Copyright (C) 2014 André Sarmento Barbosa
4+
2017 Alexander Emelianov ([email protected])
45
*/
56
#include "Arduino.h"
67

78
#ifndef MODBUS_H
89
#define MODBUS_H
910

10-
#define MAX_REGS 32
11+
#define MAX_REGS 32
1112
#define MAX_FRAME 128
1213
#define COIL_BASE 1
1314
#define ISTS_BASE 10001
1415
#define IREG_BASE 30001
1516
#define HREG_BASE 40001
17+
#define COIL(n) (n + COIL_BASE)
18+
#define ISTS(n) (n + ISTS_BASE)
19+
#define IREG(n) (n + IREG_BASE)
20+
#define HERG(n) (n + HREG_BASE)
1621

1722
//#define USE_HOLDING_REGISTERS_ONLY
1823

@@ -57,6 +62,8 @@ typedef struct TRegister {
5762
cbModbus set;
5863
} TRegister;
5964

65+
uint16_t cbDefault(TRegister* reg, uint16_t val);
66+
6067
class Modbus {
6168
private:
6269
TRegister *_regs_head;
@@ -106,6 +113,9 @@ class Modbus {
106113
bool Ists(uint16_t offset);
107114
uint16_t Ireg(uint16_t offset);
108115
#endif
116+
117+
bool onGet(uint16_t address, cbModbus cb = cbDefault);
118+
bool onSet(uint16_t address, cbModbus cb = cbDefault);
109119
};
110120

111121
#endif //MODBUS_H

0 commit comments

Comments
 (0)