1
+ /*
2
+ Ethernet Modbus TCP Server LED using existing variables as modbus memory
3
+
4
+ This sketch creates a Modbus TCP Server with a simulated coil.
5
+ The value of the simulated coil is set on the LED
6
+
7
+ Circuit:
8
+ - Any Arduino MKR Board
9
+ - MKR ETH Shield
10
+
11
+ created 16 July 2018
12
+ by Sandeep Mistry
13
+ */
14
+
15
+ #include < SPI.h>
16
+ #include < Ethernet.h>
17
+
18
+ #include < ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
19
+ #include < ArduinoModbus.h>
20
+
21
+ // Enter a MAC address for your controller below.
22
+ // Newer Ethernet shields have a MAC address printed on a sticker on the shield
23
+ // The IP address will be dependent on your local network:
24
+ byte mac[] = {
25
+ 0xDE , 0xAD , 0xBE , 0xEF , 0xFE , 0xED
26
+ };
27
+ IPAddress ip (192 , 168 , 1 , 177 );
28
+
29
+ EthernetServer ethServer (502 );
30
+
31
+ ModbusTCPServer modbusTCPServer;
32
+
33
+ const int ledPin = LED_BUILTIN;
34
+
35
+ // create an example struct of a modbus memory map
36
+ struct mb_context {
37
+ uint8_t coils[10 ];
38
+ uint8_t inputStats[10 ];
39
+ uint16_t inputRegs[10 ];
40
+ uint16_t holdingRegs[10 ];
41
+ };
42
+
43
+ mb_context myModbusMap;
44
+
45
+ void updateLED () {
46
+ // read the current value of the coil
47
+
48
+
49
+ if (myModbusMap.coils [0 ]) {
50
+ // coil value set, turn LED on
51
+ digitalWrite (ledPin, HIGH);
52
+ } else {
53
+ // coild value clear, turn LED off
54
+ digitalWrite (ledPin, LOW);
55
+ }
56
+ }
57
+
58
+ void setup () {
59
+ // You can use Ethernet.init(pin) to configure the CS pin
60
+ // Ethernet.init(10); // Most Arduino shields
61
+ // Ethernet.init(5); // MKR ETH shield
62
+ // Ethernet.init(0); // Teensy 2.0
63
+ // Ethernet.init(20); // Teensy++ 2.0
64
+ // Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
65
+ // Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
66
+
67
+ // Open serial communications and wait for port to open:
68
+ Serial.begin (9600 );
69
+ while (!Serial) {
70
+ ; // wait for serial port to connect. Needed for native USB port only
71
+ }
72
+ Serial.println (" Ethernet Modbus TCP Example" );
73
+
74
+ // start the Ethernet connection and the server:
75
+ Ethernet.begin (mac, ip);
76
+
77
+ // Check for Ethernet hardware present
78
+ if (Ethernet.hardwareStatus () == EthernetNoHardware) {
79
+ Serial.println (" Ethernet shield was not found. Sorry, can't run without hardware. :(" );
80
+ while (true ) {
81
+ delay (1 ); // do nothing, no point running without Ethernet hardware
82
+ }
83
+ }
84
+ if (Ethernet.linkStatus () == LinkOFF) {
85
+ Serial.println (" Ethernet cable is not connected." );
86
+ }
87
+
88
+ // start the server
89
+ ethServer.begin ();
90
+
91
+ // start the Modbus TCP server
92
+ if (!modbusTCPServer.begin ()) {
93
+ Serial.println (" Failed to start Modbus TCP Server!" );
94
+ while (1 );
95
+ }
96
+
97
+ // configure the LED
98
+ pinMode (ledPin, OUTPUT);
99
+ digitalWrite (ledPin, LOW);
100
+
101
+ // configure a each modbus table's pointer and length
102
+ modbusTCPServer.configureCoilPointer (myModbusMap.coils , 0x00 , 10 );
103
+ modbusTCPServer.configureDiscreteInputPointer (myModbusMap.inputStats , 0x00 , 10 );
104
+ modbusTCPServer.configureInputRegisterPointer (myModbusMap.inputRegs , 0x00 , 10 );
105
+ modbusTCPServer.configureHoldingRegisterPointer (myModbusMap.holdingRegs , 0x00 , 10 );
106
+ }
107
+
108
+ void loop () {
109
+ // listen for incoming clients
110
+ EthernetClient client = ethServer.available ();
111
+
112
+ if (client) {
113
+ // a new client connected
114
+ Serial.println (" new client" );
115
+
116
+ // let the Modbus TCP accept the connection
117
+ modbusTCPServer.accept (client);
118
+
119
+ while (client.connected ()) {
120
+ // poll for Modbus TCP requests, while client connected
121
+ modbusTCPServer.poll ();
122
+
123
+ // update the LED
124
+ updateLED ();
125
+ }
126
+
127
+ Serial.println (" client disconnected" );
128
+ }
129
+ }
0 commit comments