-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathEthernetModbusServerPointerMapping.ino
129 lines (101 loc) · 3.53 KB
/
EthernetModbusServerPointerMapping.ino
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Ethernet Modbus TCP Server LED using existing variables as modbus memory
This sketch creates a Modbus TCP Server with a simulated coil.
The value of the simulated coil is set on the LED
Circuit:
- Any Arduino MKR Board
- MKR ETH Shield
created 16 July 2018
by Sandeep Mistry
*/
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
#include <ArduinoModbus.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
EthernetServer ethServer(502);
ModbusTCPServer modbusTCPServer;
const int ledPin = LED_BUILTIN;
//create an example struct of a modbus memory map
struct mb_context{
uint8_t coils[10];
uint8_t inputStats[10];
uint16_t inputRegs[10];
uint16_t holdingRegs[10];
};
mb_context myModbusMap;
void updateLED() {
// read the current value of the coil
if (myModbusMap.coils[0]) {
// coil value set, turn LED on
digitalWrite(ledPin, HIGH);
} else {
// coild value clear, turn LED off
digitalWrite(ledPin, LOW);
}
}
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet Modbus TCP Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
ethServer.begin();
// start the Modbus TCP server
if (!modbusTCPServer.begin()) {
Serial.println("Failed to start Modbus TCP Server!");
while (1);
}
// configure the LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// configure a each modbus table's pointer and length
modbusTCPServer.configureCoilPointer(myModbusMap.coils, 0x00, 10);
modbusTCPServer.configureDiscreteInputPointer(myModbusMap.inputStats, 0x00, 10);
modbusTCPServer.configureInputRegisterPointer(myModbusMap.inputRegs, 0x00, 10);
modbusTCPServer.configureHoldingRegisterPointer(myModbusMap.holdingRegs, 0x00, 10);
}
void loop() {
// listen for incoming clients
EthernetClient client = ethServer.available();
if (client) {
// a new client connected
Serial.println("new client");
// let the Modbus TCP accept the connection
modbusTCPServer.accept(client);
while (client.connected()) {
// poll for Modbus TCP requests, while client connected
modbusTCPServer.poll();
// update the LED
updateLED();
}
Serial.println("client disconnected");
}
}