Hi all,
A month ago, I designed my project using the RAK4631 and shared it on the forum. I’m now trying to communicate between my sensor and the RAK4631 using the RS485 Modbus RTU protocol.
Official Arduino library, ArduinoModbus, is not compiling. This issue has already been reported here. I tried the recommended solution: using the Modbus-esp8286 library. I was able to compile it, but I didn’t fully understand how to communicate with the sensor and failed to get a write response (error 0xE4). Here is my adapted code from example.
#include <ModbusRTU.h>
ModbusRTU mb; // Modbus RTU object
// Modbus response
bool cbWrite(Modbus::ResultCode event, uint16_t transactionId, void* data) {
Serial.print("Request result: 0x");
Serial.print(event, HEX);
Serial.println();
return true;
}
void setup() {
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1);
mb.begin(&Serial1);
mb.setBaudrate(9600);
mb.master();
}
bool coils[20];
void loop() {
uint16_t offset = 0; // first coil
uint16_t numregs = 1; // coils to read
uint8_t unit = 1; // Modbus slave 1
if (!mb.slave()) {
// send request
mb.readCoil(1, offset, coils, numregs, cbWrite, unit);
}
mb.task(); // execute Modbus task
yield();
Serial.println(coils);
}
I also found a more recent post suggesting using Modbus-Master-Slave-for-Arduino along with the adapted RUI3_ModbusRtu library. However, this doesn’t compile (dozens of errors such as: RAK11310-Modbus\RUI3_ModbusRtu.cpp:121:11: error: 'Serial' was not declared in this scope 121 | port = &Serial;
and RAK11310-Modbus\RUI3_ModbusRtu.cpp: In member function 'void Modbus::start()':
).
Which library and solution would you recommend?
I’m not very familiar with Modbus communication… I usually manage to read values from my sensor by sending the correct query (from the datasheet) and then parsing the bits containing my data from the response. I was able to get the correct response from the sensor using a USB-RS485 converter and serial communication. Here is my query:
Standard Modbus-RTU protocol, baud rate: 9600; check bit: none; data bits: 8; stop bit: 1
Request
Address | Func Code | Dev Ad High | Dev Ad Low | Register Low | Register High | CRC16 | CRC16 |
---|---|---|---|---|---|---|---|
0x01 | 0x03 | 0x00 | 0x00 | 0x00 | 0x01 | low | high |
Send request: 01 03 00 00 00 01 84 0A
Response
Address | Func Code | Data Length | Register 0 Data High | Register 0 Data Low | CRC16 | CRC16 |
---|---|---|---|---|---|---|
0x01 | 0x03 | 0x02 | 0x1A | 0x0A | low | high |
Data: 1A 0A (hex)
= 6666 / 1 decimal
Reading value: 666.6 NTU
Can someone help me get this communication working?
Thank you !
I’m using :
- ArduinoIDE 2.3.4
- bootloader RUI3
- Board manager RAKwireless RUI nRF Boards 4.1.0
- mobdus-esp8266 4.1.0
- ArduinoRS485 1.0.2