-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathmain.cpp
More file actions
96 lines (84 loc) · 3.08 KB
/
Copy pathmain.cpp
File metadata and controls
96 lines (84 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// =================================================================================================
// eModbus: Copyright 2020 by Michael Harwerth, Bert Melis and the contributors to ModbusClient
// MIT license - see license.md for details
// =================================================================================================
// Includes: <Arduino.h> for Serial etc., WiFi.h for WiFi support
#include <Arduino.h>
#include <WiFi.h>
// Modbus server TCP
#include "ModbusServerTCPasync.h"
#ifndef MY_SSID
#define MY_SSID "WiFi network ID"
#endif
#ifndef MY_PASS
#define MY_PASS "WiFi network password"
#endif
char ssid[] = MY_SSID; // SSID and ...
char pass[] = MY_PASS; // password for the WiFi network used
// Create server
ModbusServerTCPasync MBserver;
uint16_t memo[32]; // Test server memory: 32 words
// Server function to handle FC 0x03 and 0x04
ModbusMessage FC03(ModbusMessage request) {
ModbusMessage response; // The Modbus message we are going to give back
uint16_t addr = 0; // Start address
uint16_t words = 0; // # of words requested
request.get(2, addr); // read address from request
request.get(4, words); // read # of words from request
// Address overflow?
if ((addr + words) > 20) {
// Yes - send respective error response
response.setError(request.getServerID(), request.getFunctionCode(), ILLEGAL_DATA_ADDRESS);
}
// Set up response
response.add(request.getServerID(), request.getFunctionCode(), (uint8_t)(words * 2));
// Request for FC 0x03?
if (request.getFunctionCode() == READ_HOLD_REGISTER) {
// Yes. Complete response
for (uint8_t i = 0; i < words; ++i) {
// send increasing data values
response.add((uint16_t)(addr + i));
}
} else {
// No, this is for FC 0x04. Response is random
for (uint8_t i = 0; i < words; ++i) {
// send increasing data values
response.add((uint16_t)random(1, 65535));
}
}
// Send response back
return response;
}
// Setup() - initialization happens here
void setup() {
// Init Serial monitor
Serial.begin(115200);
while (!Serial) {}
Serial.println("__ OK __");
// Connect to WiFi
WiFi.begin(ssid, pass);
delay(200);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(". ");
delay(500);
}
IPAddress wIP = WiFi.localIP();
Serial.printf("WIFi IP address: %u.%u.%u.%u\n", wIP[0], wIP[1], wIP[2], wIP[3]);
// Set up test memory
for (uint16_t i = 0; i < 32; ++i) {
memo[i] = (i * 2) << 8 | ((i * 2) + 1);
}
// Define and start RTU server
MBserver.registerWorker(1, READ_HOLD_REGISTER, &FC03); // FC=03 for serverID=1
MBserver.registerWorker(1, READ_INPUT_REGISTER, &FC03); // FC=04 for serverID=1
MBserver.registerWorker(2, READ_HOLD_REGISTER, &FC03); // FC=03 for serverID=2
MBserver.start(502, 1, 20000);
}
// loop() - nothing done here today!
void loop() {
static unsigned long lastMillis = 0;
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Serial.printf("free heap: %d\n", ESP.getFreeHeap());
}
}