Skip to content

Commit fa75989

Browse files
committed
Update examples
1 parent b9079e6 commit fa75989

File tree

15 files changed

+92
-25
lines changed

15 files changed

+92
-25
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ http://www.modbus.org/docs/Modbus_over_serial_line_V1_02.pdf
7171
+ added ability to auto connect to slave. Setting is global. Disabled by default.
7272
// 2.0.1
7373
+ Fix readCoil\Hreg\Ists\Ireg not read value from slave
74-
+ Fix cresh on disconnect with Arduino Core 2.5.x
74+
+ Fix crash on disconnect with Arduino Core 2.5.x
7575
// 2.1.0
76-
+ Fix slave error response on write multiple Hreg\Coils
76+
+ Fix slave error response on write multiple Hregs\Coils
7777
+ Fix writeCoil() for multiple coils
7878
+ dropTransactions()
7979
+ disconnect()
8080
+ ~ModbusIP()
8181
+ task() cleanup
82-
- Modify examples
82+
+ Modify examples
8383
// 2.2.0
8484
- code cleanup
8585
- Implement Private Reg/Coil

examples/AnalogInput/AnalogInput.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void setup() {
4343
Serial.println("IP address: ");
4444
Serial.println(WiFi.localIP());
4545

46-
mb.begin(); //Start Modbus IP
46+
mb.slave(); //Start Modbus IP
4747
// Add SENSOR_IREG register - Use addIreg() for analog Inputs
4848
mb.addIreg(SENSOR_IREG);
4949

@@ -60,5 +60,5 @@ void loop() {
6060
//Setting raw value (0-1024)
6161
mb.Ireg(SENSOR_IREG, analogRead(A0));
6262
}
63-
delay(100);
63+
delay(10);
6464
}

examples/Callback/Callback.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void setup() {
6060
Serial.println(WiFi.localIP());
6161

6262
mb.onConnect(cbConn); // Add callback on connection event
63-
mb.begin();
63+
mb.slave();
6464

6565
pinMode(ledPin, OUTPUT);
6666
mb.addCoil(LED_COIL); // Add Coil. The same as mb.addCoil(COIL_BASE, false, LEN)
@@ -70,5 +70,5 @@ void setup() {
7070
void loop() {
7171
//Call once inside loop() - all magic here
7272
mb.task();
73-
delay(100);
73+
delay(10);
7474
}

examples/HoldingReg/HoldingReg.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ void setup() {
4444
Serial.println("IP address: ");
4545
Serial.println(WiFi.localIP());
4646

47-
mb.begin();
47+
mb.slave();
4848
mb.addHreg(TEST_HREG, 0xABCD);
4949
}
5050

5151
void loop() {
5252
//Call once inside loop() - all magic here
5353
mb.task();
54-
delay(100);
54+
delay(10);
5555
}

examples/Led/Led.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void setup() {
4444
Serial.println("IP address: ");
4545
Serial.println(WiFi.localIP());
4646

47-
mb.begin();
47+
mb.slave();
4848

4949
pinMode(ledPin, OUTPUT);
5050
mb.addCoil(LED_COIL);
@@ -56,5 +56,5 @@ void loop() {
5656

5757
//Attach ledPin to LED_COIL register
5858
digitalWrite(ledPin, mb.Coil(LED_COIL));
59-
delay(100);
59+
delay(10);
6060
}

examples/MassOperations/MassOperations.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void setup() {
6969
for (uint8_t i = 0; i < LEN; i++)
7070
pinMode(pinList[i], INPUT);
7171
mb.onConnect(cbConn); // Add callback on connection event
72-
mb.begin();
72+
mb.slave();
7373

7474
mb.addCoil(COIL_BASE, COIL_VAL(false), LEN); // Add Coils.
7575
mb.onGetCoil(COIL_BASE, cbRead, LEN); // Add callback on Coils value get
@@ -79,5 +79,5 @@ void setup() {
7979
void loop() {
8080
//Call once inside loop() - all magic here
8181
mb.task();
82-
delay(100);
82+
delay(10);
8383
}

examples/Master/Master.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ void loop() {
7171
mb.connect(remote); // Try to connect if no connection
7272
}
7373
mb.task(); // Common local Modbus task
74-
delay(100); // Polling interval
74+
delay(10); // Polling interval
7575
}

examples/MasterSimpleRead/MasterSimpleRead.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void loop() {
5252
}
5353
mb.task(); // Common local Modbus task
5454
delay(100); // Pulling interval
55-
if (show--) { // Display Slave register value one time per second (with default settings)
55+
if (!show--) { // Display Slave register value one time per second (with default settings)
5656
Serial.println(res);
5757
show = LOOP_COUNT;
5858
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Modbus-Arduino Example - Master (Modbus IP ESP8266/ESP32)
3+
Write multiple coils to Slave device
4+
5+
(c)2019 Alexander Emelianov ([email protected])
6+
https://github.com/emelianov/modbus-esp8266
7+
*/
8+
9+
#ifdef ESP8266
10+
#include <ESP8266WiFi.h>
11+
#else
12+
#include <WiFi.h>
13+
#endif
14+
#include <ModbusIP_ESP8266.h>
15+
16+
const int REG = 100; // Modbus Coils Offset
17+
const int COUNT = 5; // Count of Coils
18+
IPAddress remote(192, 168, 20, 102); // Address of Modbus Slave device
19+
20+
ModbusIP mb; // ModbusIP object
21+
22+
void setup() {
23+
#ifdef ESP8266
24+
Serial.begin(74880);
25+
#else
26+
Serial.begin(115200);
27+
#endif
28+
29+
WiFi.begin();
30+
31+
while (WiFi.status() != WL_CONNECTED) {
32+
delay(500);
33+
Serial.print(".");
34+
}
35+
36+
Serial.println("");
37+
Serial.println("WiFi connected");
38+
Serial.println("IP address: ");
39+
Serial.println(WiFi.localIP());
40+
41+
mb.master();
42+
}
43+
44+
bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Modbus Transaction callback
45+
if (event != Modbus::EX_SUCCESS) // If transaction got an error
46+
Serial.printf("Modbus result: %02X\n", event); // Display Modbus error code
47+
if (event == Modbus::EX_TIMEOUT) { // If Transaction timeout took place
48+
mb.disconnect(remote); // Close connection to slave and
49+
mb.dropTransactions(); // Cancel all waiting transactions
50+
}
51+
return true;
52+
}
53+
54+
bool res[COUNT] = {false, true, false, true, true};
55+
56+
void loop() {
57+
if (!mb.isConnected(remote)) { // Check if connection to Modbus Slave is established
58+
mb.connect(remote); // Try to connect if no connection
59+
Serial.print(".");
60+
}
61+
if (!mb.writeCoil(remote, REG, res, COUNT, cb)) // Try to Write array of COUNT of Coils to Modbus Slave
62+
Serial.print("#");
63+
mb.task(); // Modbus task
64+
delay(50); // Pushing interval
65+
}

examples/MultipleHRegDebug/MultipleHRegDebug.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void setup() {
6969
Serial.println(WiFi.localIP());
7070

7171
mb.onConnect(cbConn); // Add callback on connection event
72-
mb.begin();
72+
mb.slave();
7373

7474
if (!mb.addHreg(0, 0xF0F0, LEN)) Serial.println("Error"); // Add Hregs
7575
mb.onGetHreg(0, cbRead, LEN); // Add callback on Coils value get
@@ -79,5 +79,5 @@ void setup() {
7979
void loop() {
8080
//Call once inside loop() - all magic here
8181
mb.task();
82-
delay(100);
82+
delay(10);
8383
}

0 commit comments

Comments
 (0)