|
| 1 | +/* |
| 2 | + Modbus RTU Client Kitchen Sink |
| 3 | +
|
| 4 | + This sketch creates a Modbus RTU Client and demostrates |
| 5 | + how to use various Modbus Client APIs. |
| 6 | +
|
| 7 | + Circuit: |
| 8 | + - MKR board |
| 9 | + - MKR 485 shield |
| 10 | + - ISO GND connected to GND of the Modbus RTU server |
| 11 | + - Y connected to A/Y of the Modbus RTU server |
| 12 | + - Z connected to B/Z of the Modbus RTU server |
| 13 | + - Jumper positions |
| 14 | + - FULL set to OFF |
| 15 | + - Z \/\/ Y set to ON |
| 16 | +
|
| 17 | + created 18 July 2018 |
| 18 | + by Sandeep Mistry |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library |
| 22 | +#include <ArduinoModbus.h> |
| 23 | + |
| 24 | +int counter = 0; |
| 25 | + |
| 26 | +void setup() { |
| 27 | + Serial.begin(9600); |
| 28 | + while (!Serial); |
| 29 | + |
| 30 | + Serial.println("Modbus RTU Client Kitchen Sink"); |
| 31 | + |
| 32 | + // start the Modbus RTU client |
| 33 | + if (!ModbusRTUClient.begin(9600)) { |
| 34 | + Serial.println("Failed to start Modbus RTU Client!"); |
| 35 | + while (1); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void loop() { |
| 40 | + writeCoilValues(); |
| 41 | + |
| 42 | + readCoilValues(); |
| 43 | + |
| 44 | + readDiscreteInputValues(); |
| 45 | + |
| 46 | + writeHoldingRegisterValues(); |
| 47 | + |
| 48 | + readHoldingRegisterValues(); |
| 49 | + |
| 50 | + readInputRegisterValues(); |
| 51 | + |
| 52 | + counter++; |
| 53 | + |
| 54 | + delay(5000); |
| 55 | + Serial.println(); |
| 56 | +} |
| 57 | + |
| 58 | +void writeCoilValues() { |
| 59 | + // set the coils to 1 when counter is odd |
| 60 | + byte coilValue = ((counter % 2) == 0) ? 0x00 : 0x01; |
| 61 | + |
| 62 | + Serial.print("Writing Coil values ... "); |
| 63 | + |
| 64 | + // write 10 Coil values to (slave) id 42, address 0x00 |
| 65 | + ModbusRTUClient.beginTransmission(42, COILS, 0x00, 10); |
| 66 | + for (int i = 0; i < 10; i++) { |
| 67 | + ModbusRTUClient.write(coilValue); |
| 68 | + } |
| 69 | + if (!ModbusRTUClient.endTransmission()) { |
| 70 | + Serial.print("failed! "); |
| 71 | + Serial.println(ModbusRTUClient.lastError()); |
| 72 | + } else { |
| 73 | + Serial.println("success"); |
| 74 | + } |
| 75 | + |
| 76 | + // Alternatively, to write a single Coil value use: |
| 77 | + // ModbusRTUClient.coilWrite(...) |
| 78 | +} |
| 79 | + |
| 80 | +void readCoilValues() { |
| 81 | + Serial.print("Reading Coil values ... "); |
| 82 | + |
| 83 | + // read 10 Coil values from (slave) id 42, address 0x00 |
| 84 | + if (!ModbusRTUClient.requestFrom(42, COILS, 0x00, 10)) { |
| 85 | + Serial.print("failed! "); |
| 86 | + Serial.println(ModbusRTUClient.lastError()); |
| 87 | + } else { |
| 88 | + Serial.println("success"); |
| 89 | + |
| 90 | + while (ModbusRTUClient.available()) { |
| 91 | + Serial.print(ModbusRTUClient.read()); |
| 92 | + Serial.print(' '); |
| 93 | + } |
| 94 | + Serial.println(); |
| 95 | + } |
| 96 | + |
| 97 | + // Alternatively, to read a single Coil value use: |
| 98 | + // ModbusRTUClient.coilRead(...) |
| 99 | +} |
| 100 | + |
| 101 | +void readDiscreteInputValues() { |
| 102 | + Serial.print("Reading Discrete Input values ... "); |
| 103 | + |
| 104 | + // read 10 Discrete Input values from (slave) id 42, address 0x00 |
| 105 | + if (!ModbusRTUClient.requestFrom(42, DISCRETE_INPUTS, 0x00, 10)) { |
| 106 | + Serial.print("failed! "); |
| 107 | + Serial.println(ModbusRTUClient.lastError()); |
| 108 | + } else { |
| 109 | + Serial.println("success"); |
| 110 | + |
| 111 | + while (ModbusRTUClient.available()) { |
| 112 | + Serial.print(ModbusRTUClient.read()); |
| 113 | + Serial.print(' '); |
| 114 | + } |
| 115 | + Serial.println(); |
| 116 | + } |
| 117 | + |
| 118 | + // Alternatively, to read a single Discrete Input value use: |
| 119 | + // ModbusRTUClient.discreteInputRead(...) |
| 120 | +} |
| 121 | + |
| 122 | +void writeHoldingRegisterValues() { |
| 123 | + // set the Holding Register values to counter |
| 124 | + |
| 125 | + Serial.print("Writing Holding Registers values ... "); |
| 126 | + |
| 127 | + // write 10 coil values to (slave) id 42, address 0x00 |
| 128 | + ModbusRTUClient.beginTransmission(42, HOLDING_REGISTERS, 0x00, 10); |
| 129 | + for (int i = 0; i < 10; i++) { |
| 130 | + ModbusRTUClient.write(counter); |
| 131 | + } |
| 132 | + if (!ModbusRTUClient.endTransmission()) { |
| 133 | + Serial.print("failed! "); |
| 134 | + Serial.println(ModbusRTUClient.lastError()); |
| 135 | + } else { |
| 136 | + Serial.println("success"); |
| 137 | + } |
| 138 | + |
| 139 | + // Alternatively, to write a single Holding Register value use: |
| 140 | + // ModbusRTUClient.holdingRegisterWrite(...) |
| 141 | +} |
| 142 | + |
| 143 | +void readHoldingRegisterValues() { |
| 144 | + Serial.print("Reading Input Register values ... "); |
| 145 | + |
| 146 | + // read 10 Input Register values from (slave) id 42, address 0x00 |
| 147 | + if (!ModbusRTUClient.requestFrom(42, HOLDING_REGISTERS, 0x00, 10)) { |
| 148 | + Serial.print("failed! "); |
| 149 | + Serial.println(ModbusRTUClient.lastError()); |
| 150 | + } else { |
| 151 | + Serial.println("success"); |
| 152 | + |
| 153 | + while (ModbusRTUClient.available()) { |
| 154 | + Serial.print(ModbusRTUClient.read()); |
| 155 | + Serial.print(' '); |
| 156 | + } |
| 157 | + Serial.println(); |
| 158 | + } |
| 159 | + |
| 160 | + // Alternatively, to read a single Holding Register value use: |
| 161 | + // ModbusRTUClient.holdingRegisterRead(...) |
| 162 | +} |
| 163 | + |
| 164 | +void readInputRegisterValues() { |
| 165 | + Serial.print("Reading input register values ... "); |
| 166 | + |
| 167 | + // read 10 dsicrete input values from (slave) id 42, |
| 168 | + if (!ModbusRTUClient.requestFrom(42, INPUT_REGISTERS, 0x00, 10)) { |
| 169 | + Serial.print("failed! "); |
| 170 | + Serial.println(ModbusRTUClient.lastError()); |
| 171 | + } else { |
| 172 | + Serial.println("success"); |
| 173 | + |
| 174 | + while (ModbusRTUClient.available()) { |
| 175 | + Serial.print(ModbusRTUClient.read()); |
| 176 | + Serial.print(' '); |
| 177 | + } |
| 178 | + Serial.println(); |
| 179 | + } |
| 180 | + |
| 181 | + // Alternatively, to read a single Input Register value use: |
| 182 | + // ModbusRTUClient.inputRegisterRead(...) |
| 183 | +} |
| 184 | + |
0 commit comments