|
| 1 | +#include <ModbusRTUSlave.h> |
| 2 | + |
| 3 | +// size of data which will be read and written |
| 4 | +#define DATA_SIZE 100 |
| 5 | +// data array which will be read and written |
| 6 | +u16 _D[DATA_SIZE]; |
| 7 | + |
| 8 | +// address (kind of name) of above data, may be anything |
| 9 | +#define VIRTUAL_ADDRESS 0x0000 |
| 10 | + |
| 11 | +#define SLAVE_ID 2 |
| 12 | +#define DE_RE_PIN 2 |
| 13 | + |
| 14 | +ModbusRTUSlave rtu(SLAVE_ID, &Serial1, DE_RE_PIN); |
| 15 | + |
| 16 | +void setup() |
| 17 | +{ |
| 18 | + rtu.addWordArea(VIRTUAL_ADDRESS, _D, DATA_SIZE); |
| 19 | + rtu.begin(9600); |
| 20 | + |
| 21 | + pinMode(A0, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 22 | + pinMode(A1, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 23 | + pinMode(A2, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 24 | + pinMode(A3, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 25 | + pinMode(A4, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 26 | + pinMode(A5, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 27 | + pinMode(A6, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 28 | + pinMode(A7, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 29 | + pinMode(A8, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 30 | + pinMode(A9, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 31 | + pinMode(A10, INPUT_PULLUP); // set pull-up on analog pin 0 |
| 32 | +// pinMode(2, INPUT_PULLUP); |
| 33 | +// pinMode(3, INPUT_PULLUP); |
| 34 | +// pinMode(4, INPUT_PULLUP); |
| 35 | +// pinMode(5, INPUT_PULLUP); |
| 36 | +// pinMode(6, INPUT_PULLUP); |
| 37 | +// pinMode(7, INPUT_PULLUP); |
| 38 | +// pinMode(8, INPUT_PULLUP); |
| 39 | +// pinMode(9, INPUT_PULLUP); |
| 40 | +// pinMode(10, INPUT_PULLUP); |
| 41 | +// pinMode(11, INPUT_PULLUP); |
| 42 | + |
| 43 | + Serial.begin(9600); // not needed, for logging purpose only |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +void loop() |
| 48 | +{ |
| 49 | + // set some value in data array to test if master can read and modify it |
| 50 | + _D[0] = analogRead(A1); |
| 51 | + delay(10); |
| 52 | + _D[1] = analogRead(A2); |
| 53 | + delay(10); |
| 54 | + _D[2] = analogRead(A3); |
| 55 | + delay(10); |
| 56 | + _D[3] = analogRead(A4); |
| 57 | + delay(10); |
| 58 | + _D[4] = analogRead(A5); |
| 59 | + delay(10); |
| 60 | + _D[5] = analogRead(A6); |
| 61 | + delay(10); |
| 62 | + _D[6] = analogRead(A7); |
| 63 | + delay(10); |
| 64 | + _D[7] = analogRead(A8); |
| 65 | + delay(10); |
| 66 | + _D[8] = analogRead(A9); |
| 67 | + delay(10); |
| 68 | + _D[9] = analogRead(A10); |
| 69 | + delay(10); |
| 70 | + _D[10] = analogRead(A0); |
| 71 | + delay(10); |
| 72 | + |
| 73 | + for (int i = 0; i <= 10; i++) { |
| 74 | + |
| 75 | + if (i < 10) { |
| 76 | + if (_D[i] > 600) { _D[i] = 1; } else { _D[i] = 0; } |
| 77 | + delay(10); |
| 78 | + } |
| 79 | + |
| 80 | + if (i==10) {Serial.print(" - ");} else {Serial.print(" ");} |
| 81 | + Serial.print(_D[i]); |
| 82 | +// delay(10); |
| 83 | +} |
| 84 | + Serial.println(""); |
| 85 | + |
| 86 | + // waiting for requests from master |
| 87 | + // reading and writing _D according to requests from master happens here |
| 88 | + rtu.process(); |
| 89 | +// delay(10); |
| 90 | +} |
0 commit comments