Arduino Modbus ASCII

Hello

I am new to work with RS485.

Soil moisture sensor
Baud rate - 9600
Data bits - 8
Parity bit - none
Stop bit - 1

I tried this with Termite application where I am getting responses from the sensor.
Commands used
"addrTR" - 000TR to take a reading
"addrT0" - 000T0 to retrive data

I want to code with Arduino UNO to fetch the data out of it. Wiring connections were perfect. Please help out with the coding.
Attached measurement commands below.

That doesn't look like Modbus. That looks more like plain old ASCII over RS485.

Are the responses you see using Termite what you expect?

EDIT: Oh, and provide a link to the sensor datasheet.

As you are using an UNO with one hardware serial port (hardwired to the USB serial port), you will need to use a software serial port to talk to the device. Have a look at this web page where the author shows how they used RS485 modules with a software serial port.

@gopikrishna1793 - the link you provided was to the glossy marketing product flyer.

Regarding your PM, I'm not going to write the code for you. I'll point you in what I consider to be the right direction, which I believe is the link I gave you previously.

The "Master Arduino Sketch" on that page would be a good starting point. I think you can connect up your RS485 module as detailed in the code comments. You should change the software serial port baud rate to 9600 to match your device.

You should then be able to use the IDE serial monitor to send the device commands to your UNO, which will then send them out over the software serial RS485 link and report back anything received.

That should be a good starting point to enable you to progress with your own code.

This is the link to the Stevens Hydraprobe Soil Sensor manual / datasheet.

Please check from page number 51 onwards for relevant information.

I tried out this code. It seems to be sending the commands that I type in the serial monitor, to the sensor correctly, but not getting any readings from the sensor. Please let me know what might be the problem and what may be the changes to be made.

/-----( Import needed libraries )-----/
#include <SoftwareSerial.h>
/-----( Declare Constants and Pin Numbers )-----/
#define SSerialRX 10 //Serial Receive pin
#define SSerialTX 11 //Serial Transmit pin

#define SSerialTxControl 3 //RS485 Direction control

#define RS485Transmit HIGH
#define RS485Receive LOW

#define Pin13LED 13

/-----( Declare objects )-----/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/-----( Declare Variables )-----/
int byteReceived;
int dataReceived;
int byteSend;

void setup() {
// Start the built-in serial port, probably to Serial Monitor
Serial.begin(9600);
Serial.println("YourDuino.com SoftwareSerial remote loop example");
Serial.println("Use Serial Monitor, type in upper window, ENTER");

pinMode(Pin13LED, OUTPUT);
pinMode(SSerialTxControl, OUTPUT);

digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver

// Start the software serial port, to another device
RS485Serial.begin(9600); // set the data rate

}

void loop()
{
digitalWrite(Pin13LED, HIGH); // Show activity
if (Serial.available())
{
byteReceived = Serial.read();
Serial.println(byteReceived);

digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
RS485Serial.write(byteReceived); // Send byte to sensor

digitalWrite(Pin13LED, LOW); // Show activity
delay(10);
digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
}

if (RS485Serial.available()) //Look for data from the sensor
{
Serial.println("data available");
digitalWrite(Pin13LED, HIGH); // Show activity
dataReceived = RS485Serial.read(); // Read received byte
Serial.write(dataReceived); // Show on Serial Monitor
delay(10);
digitalWrite(Pin13LED, LOW); // Show activity
}

}

@markd833
`

As you are using an UNO with one hardware serial port (hardwired to the USB serial port), you will need to use a software serial port to talk to the device.

`
How sure are you about your statement?
I have here many V1R1 shields that do RS485 on the UNO with the on chip UART (but then you cannot use the serial monitor on the usb at the same time). But RS485 on the on board UART is for sure possible.

@JOHI , how would you load and debug your code if you've repurposed the hardware UART?

I suppose you could unplug the shield whilst you loaded the code, or maybe the shield has a pull up resistor to stop the RO pin becoming active when RE is floating.

And you could use a software serial port for debugging if you wished.

Loading is done by selecting the switch UART-Softserial in the position Softserial, in this position the shield connects to pin 2 and 3 and not to 0 and 1, then you load your shield. Later on you can change the position. Debug output is then not available over USB. You can use softserial to send your debugging info back to the pc if necessary.
The advantage of using the hardware UART is you can go up to 115K whereas the softserial is limited to 19,2K. On top there is also a switch RS485 - RS232C. The jumpers you can use for termination of the RS485 bus.
Of course you can also go the RS485/RS232C to softserial way if needed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.