RS485 without MODBUS using UNO R4 WiFi

Hey everyone, I have a sensor which uses RS-485 over ASCII.

I am supposed to use this command

[AG,01\r]

to get the data from the sensor.

Like any RS-485 sensor, I have connected the A and B pins to the MAX485 module and the other pins as well. The program is attached below:

#include <SoftwareSerial.h>

#define DE_PIN 2     // Driver Enable
#define RE_PIN 3     // Receiver Enable

#define RX_PIN 4     // Arduino RX (connect to MAX485 RO)
#define TX_PIN 5     // Arduino TX (connect to MAX485 DI)

SoftwareSerial rs485(RX_PIN, TX_PIN);

unsigned long lastSendTime = 0;
const unsigned long sendInterval = 2000; // 2 seconds

void setup() {
  Serial.begin(9600);
  rs485.begin(115200);

  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);

  // Start in receive mode
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);

  Serial.println("Starting RS-485 communication...");
}

void sendCommand(const char* cmd) {
  // Switch to transmit
  digitalWrite(DE_PIN, HIGH);
  digitalWrite(RE_PIN, HIGH);
  delay(10);  // Allow driver to enable

  rs485.print(cmd);
  
  delay(10);  // Wait for transmission to complete

  // Switch back to receive
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);
}

void loop() {
  // Send command every 2 seconds
  if (millis() - lastSendTime > sendInterval) {
    sendCommand("[AG,01\r]");
    lastSendTime = millis();
  }

  // Read incoming data from sensor and print to Serial Monitor
  while (rs485.available()) {
    char c = rs485.read();
    Serial.print(c);
  }
}

However, I receive no response from the sensor. Could anyone let me know if there is something wrong with the program? I usually use RS-485 over MODBUS and it works perfectly.

I don't own an R4 Wifi, but do you need software serial? From what I've read in a post by @UKHeliBob quoted below, it seems that if you are using pins 0 & 1 for your RS485 comms then maybe it’s not required :

In addition, you can then use the flush() function to wait for the transmission to complete before switching back to receive.

As you are also using external hardware, you should provide details of how you have connected that up to you UNO R4.

1 Like


Not the best diagram but the best I can do right now.

I need software serial because if I use hardware serial (Pin D0 and D1), I cannot then use the serial monitor.

I can try the flush() function as well later

On R4, serial is USB, serial1 is D0/D1, so you can use both simultaneously here.
Also be aware, that RS485 line labeling is wild west. Afaik standard gives A- and B+, but in practice A+ and B- are more popular. When you connect to device that has D+ and D-, it's better to try also lines flipped.

Also, wouldn't hurt to post some details about your sensor and the communication protocol.
Might not be default 8N1.

1 Like

I have tried flipping the lines as well. My sensor (transducer technically) is the Kanomax 6333-0. The datasheet tells me that it can interface using RS485. Nothing else is mentioned unfortunately. I know the device doesnt use MODBUS, it uses ASCII and the device ID is 01.

Datasheet
Manual

I know my connections are good because I use the same connections for my MODBUS sensors.

Correction: doesnt use MODBUS RTU*

What I have understood so far is that I have to send this command - [AG,01\r] , to the sensor and expect it to reply back with the sensor data.

From where you got the command? You need serial parameters as well...

Are you sure that you need to be sending the square brackets?


This was given ot me by the supplier. Serial parameters have not been given anywhere unfortunately. The Default ID (01) and Baud rate (115200) is provided.

I have put these details in the code as well. Does not work unfortunately.

But your code is using a baud rate of 19200.

Yes, I tried a few baud rates after the program didnt work with 115200 because I thought that would be the issue.

I will change it now. Apologies.

I think you aught to try it without the square brackets as well.

Alright, I will try that right now. I hope thats the solution! Thank you.

#define DE_PIN 2     // Driver Enable
#define RE_PIN 3     // Receiver Enable

unsigned long lastSendTime = 0;
const unsigned long sendInterval = 2000; // 2 seconds

void setup() {
  Serial.begin(9600);      // USB Serial Monitor
  Serial1.begin(115200);    // RS-485 on D0 (RX1), D1 (TX1)

  pinMode(DE_PIN, OUTPUT);
  pinMode(RE_PIN, OUTPUT);

  // Start in receive mode
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);

  Serial.println("Starting RS-485 communication using Serial1...");
}

void sendCommand(const char* cmd) {
  // Switch to transmit mode
  digitalWrite(DE_PIN, HIGH);
  digitalWrite(RE_PIN, HIGH);
  delay(10);  // Allow driver to enable

  Serial1.print(cmd);  // Send the command over RS-485

  delay(10);  // Allow time to transmit

  // Switch back to receive mode
  digitalWrite(DE_PIN, LOW);
  digitalWrite(RE_PIN, LOW);
}

void loop() {
  // Send command every 2 seconds
  if (millis() - lastSendTime > sendInterval) {
    Serial.println("Sending command: DG,01\\r");
    sendCommand("DG,01\r");
    lastSendTime = millis();
  }

  // Read incoming data from sensor and print to Serial Monitor
  while (Serial1.available()) {
    char c = Serial1.read();
    Serial.print(c);
  }
}

Using this program with Serial1 and without brackets. Unfortunately I get this output:

Have you tried using a delay shorter than 10mS? The sensor may be transmitting before you switch the RS-485 to receive mode. As was pointed out, you can use flush() to wait for the serial transmission to complete instead of an arbitrary delay amount. At 115200 baud the 6-character command takes about 520 microseconds (1/2 millisecond) to transmit.

I have tried flush() as well. It seems like I am getting no data from the sensor. Not even garbage values.

Anemometer details.pdf (116.1 KB)

I have attached the communication details here. Hopefully this can help to find a solution.

Usually on the MAX485 modules, you can tie the two enables together, because you want to enable either one, not both.

Never mind, I see you are doing that, just individually.

Yep