#include <Controllino.h>
#include “ModbusRtu.h”
#define MasterModbusAdd 0
#define SlaveModbusAdd 1
#define RS485Serial 3
String result;
uint16_t au16data[16];
uint8_t u8state;
Modbus master(SlaveModbusAdd, RS485Serial, 0);
modbus_t telegram;
unsigned long u32wait;
void setup() {
Serial.begin(9600);
while (!Serial){}
Serial.println(“Connecting to Serial3 “);
master.begin(19200);
master.setTimeOut(5000); // if there is no answer in 5000 ms, roll over
u32wait = millis() + 2000;
u8state = 0;
telegram.u8id = 1; // slave address
telegram.u8fct = 4; // function code (this one is registers read)
telegram.u16RegAdd = 1; // start address in slave
telegram.u16CoilsNo = 1; // number of elements (coils or registers) to read
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
}
void loop() {
switch( u8state ) {
case 0:
if (millis() > u32wait) u8state++; // wait state
break;
case 1:
master.query(telegram); // send query (only once)
u8state++;
break;
case 2:
master.poll(); // check incoming messages
if (master.getState() == COM_IDLE) {
u8state = 0;
u32wait = millis() + 1000;
Serial.print(“ontvangen data: “);
Serial.println(au16data[0]);
}
break;
}
delay(200);
}
-
This reply was modified 3 years ago by Nestor973.
-
This reply was modified 3 years ago by Nestor973.
-
This reply was modified 3 years ago by Nestor973.