|
| 1 | +/* |
| 2 | + This is Modbus test code to demonstrate all the Modbus functions with |
| 3 | + with Ethernet IC WIZNET W5100 |
| 4 | +
|
| 5 | + ModbusTCP is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + ModbusTCP is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with ModbusTCP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +
|
| 18 | + Adopted from ModbusMaster for RTU over RS-485 by Doc Walker |
| 19 | + Modified by Narendra Dehury for TCP. |
| 20 | + Modified by Florian K for ESP8266. |
| 21 | + copyright @ phoenixrobotix.com |
| 22 | +
|
| 23 | +*/ |
| 24 | +#define ESP8266 1 |
| 25 | + |
| 26 | +unsigned int param_value_int[7]; |
| 27 | +#include <Ethernet.h> |
| 28 | + |
| 29 | +IPAddress ModbusDeviceIP(10, 10, 108, 211); // Put IP Address of PLC here |
| 30 | +IPAddress moduleIPAddress(10, 10, 108, 23); // Assign Anything other than the PLC IP Address |
| 31 | + |
| 32 | +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xE1 }; |
| 33 | + |
| 34 | + |
| 35 | +#include <ModbusTCP.h> |
| 36 | + |
| 37 | +ModbusTCP node(1); // Unit Identifier. |
| 38 | + |
| 39 | +void setup() |
| 40 | +{ |
| 41 | + |
| 42 | + pinMode(4, OUTPUT); |
| 43 | + digitalWrite(4, HIGH); // To disable slave select for SD card; depricated. |
| 44 | + |
| 45 | + Serial.begin(9600); |
| 46 | + delay(1000); |
| 47 | + Ethernet.begin(mac, moduleIPAddress); |
| 48 | + node.setServerIPAddress(ModbusDeviceIP); |
| 49 | + delay(6000); // To provide sufficient time to initialize. |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +void loop() |
| 55 | +{ |
| 56 | + uint8_t result; |
| 57 | + |
| 58 | + node.setTransactionID(random(100)); // Not necessary; but good to distinguish each frame individually. |
| 59 | + result = node.readHoldingRegisters(1, 12); // Read Holding Registers |
| 60 | + |
| 61 | + Serial.println(result, HEX); |
| 62 | + if(result != 0) |
| 63 | + { |
| 64 | + Serial.println("TimeOut"); |
| 65 | + |
| 66 | + delay(6000); |
| 67 | + } |
| 68 | + |
| 69 | + int len = node.getResponseBufferLength(); |
| 70 | + Serial.println("Response Length: " + String(len));// See the length of data packet received. |
| 71 | + for (byte j = 0; j < len; j++) |
| 72 | + { |
| 73 | + Serial.print(node.getResponseBuffer(j)); // Inspect the data. |
| 74 | + Serial.print(" "); |
| 75 | + } |
| 76 | + Serial.println(); |
| 77 | + node.clearResponseBuffer(); |
| 78 | + delay(100); |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + node.writeSingleRegister(5, 3); // Write single register |
| 84 | + Serial.println(result, HEX); |
| 85 | + delay(500); |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + for (byte i = 0; i < 5; i++) |
| 90 | + { |
| 91 | + node.setTransmitBuffer(i, (i+100)); |
| 92 | + } |
| 93 | + node.writeMultipleRegisters(2, 5); // Write multiple register |
| 94 | + Serial.println(result, HEX); |
| 95 | + delay(500); |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + node.writeSingleCoil(20, 1); // Write Single coil |
| 100 | + delay(500); |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + node.setTransmitBuffer(0, 0b1010101001010101); |
| 105 | + |
| 106 | + node.writeMultipleCoils(20, 16); // Write multiple coils |
| 107 | + delay(500); |
| 108 | + result = node.readCoils(20, 18); |
| 109 | + len = node.getResponseBufferLength(); |
| 110 | + Serial.println("Response Length: " + String(len)); |
| 111 | + Serial.println(node.getResponseBuffer(0), BIN); |
| 112 | + |
| 113 | + |
| 114 | + node.clearResponseBuffer(); |
| 115 | + delay(5000); |
| 116 | +} |
0 commit comments