Hello,
Hello,
I’m having trouble communicating over Modbus RTU between an Arduino OPTA and a TC4S-Q1TC-RS485 PID temperature controller.
The goal is simply to read the holding register at address 0, which contains the measured temperature.
However, when using the example Modbus RTU code for Arduino OPTA, I always get “Invalid CRC” or “Connection Timeout” errors.
To rule out wiring or configuration issues:
- Using an RS485-to-USB converter and QModMaster, the communication works perfectly.
- I also wrote a Modbus library for a BluePill (STM32F103C8T6), and it communicates successfully with the same PID controller.
It seems the issue is specific to the Arduino OPTA’s RS485 implementation.
Has anyone managed to get stable Modbus RTU communication between Arduino OPTA and external devices? Any suggestions or working examples would be greatly appreciated.
Here's the code I'm using:
#include <Arduino.h> #include <ArduinoModbus.h> #include <ArduinoRS485.h> constexpr auto baudrate{19200}; constexpr auto bitduration{1.f / baudrate}; constexpr auto preDelayBR{bitduration * 9.6f * 3.5f * 1e6}; constexpr auto postDelayBR{bitduration * 9.6f * 3.5f * 1e6}; uint16_t output = 0; void setup() { Serial.begin(9600); RS485.setDelays(preDelayBR, postDelayBR);/* code */ if (!ModbusRTUClient.begin(baudrate, SERIAL_8N1)){ Serial.println("Error while initializing modbus"); while(1){} } } void loop() { Serial.println("Reading PID"); // Request from Slave ID 2, Holding Register start 0, len 1 if(!ModbusRTUClient.requestFrom(2, HOLDING_REGISTERS, 0, 1)){ Serial.print("Error while reading from slave with error "); Serial.println(ModbusRTUClient.lastError()); } else { while(ModbusRTUClient.available()){ output = (uint16_t) ModbusRTUClient.read(); } Serial.print("Value: "); Serial.println(output); } delay(4000); }
Thanks in advance!