Friends, I am trying to achieve half duplex communication between two Arduinos using RS485 Protocol. The hardware I am using is a MAX485 module and an Arduino Uno. I am using the SoftwareSerial library for Arduino, but I am not be able to transmit data from sender to receiver. The length of the wire between MAX485 modules is 1 meter.
My question is, what wrong I am doing here, please guide me.
The code:
SenderDevice
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7);
String strCmd;
int counter;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
counter = 0;
}
void loop()
{
counter++;
strCmd = "Counter" + String(counter) + "$";
Serial.println("Sending " + strCmd);
mySerial.print(strCmd);
delay(10);
mySerial.flush();
Serial.flush();
memset(&strCmd, 0, sizeof(strCmd));
delay(2000);
}
ReceiverDevice
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6, 7);
String strCmd;
void setup()
{
Serial.begin(9600);
Serial.println("ready");
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
mySerial.begin(9600);
}
void loop()
{
while (mySerial.available() > 0) {
char c = mySerial.read();
if (c == '$') {
Serial.println("Received " + strCmd);
memset(&strCmd, 0, sizeof(strCmd));
} else {
strCmd = strCmd + c;
}
}
}
My Wire Connections are :
(Both Sender & Receiver Device) Arduino Uno 6 -> Max485 RO Arduino Uno 7 -> Max485 DI Arduino Uno 5 -> Max485 DE Arduino Uno 5 -> Max485 RE Arduino Uno 5V -> Max485 VCC Arduino Uno GND -> Max485 GND Sender Max485 A -> Receiver Max485 A Sender Max485 B -> Receiver Max485 B