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.
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.
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.
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.
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.