2

I want to send data between Arduino Uno and Mega via MAX485.

When I set the data Enable pin to HIGH on Mega (to transmit data) and set the same pin on UNO to LOW (to receive data) I can read data from Mega into Uno properly. However, when I want to do it vice versa (send data from uno to mega) can't get any data from Uno into Mega. Tried different pins on both boards but still no answer.

This problem happens when I want to change data Enable pins to low or high inside loop(), I mean when I use for example Uno as permanent master and mega as permanent slave, it works properly but when I try to change them from slave to master on runtime, data cannot be sent from uno to mega!

PS: Im newbie on RS485 and MAX485. Here is my code in MEGA side:

#include <SoftwareSerial.h>'
SoftwareSerial mySerial(2, 3);
String inputS = "";
boolean stringComplete = false;
int i = 0;
bool b= false;

void setup() {
  mySerial.begin(9600);//Using SoftwareSerial Port
  Serial.begin(9600);
  pinMode(8, OUTPUT);//DE/RE Controling pin of RS-485
  digitalWrite(8, HIGH);//DE/RE=HIGH Transmit Enabled M1
}

void loop() {
  mySerial.println("MEGA:Hi");
  Serial.println("Hi Sent");
  digitalWrite(8, LOW);
  CheckAndReceive();
  if(stringComplete)
  {
  stringComplete = false;
  inputS.trim();
  Serial.print(inputS);
  Serial.println(": " + String(i++));
  inputS = "";
  }
  digitalWrite(8, HIGH);
    delay(1000);
}

void CheckAndReceive()
{
  while (mySerial.available()) {
    // get the new byte:
    char inChar = (char)mySerial.read();
    // add it to the inputString:
    inputS += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

This code is supposed to send a string to UNO and listen to it.

UPDATE: UNO cannot send data to MEGA at all. I think there is a problem with my UNO

4
  • 1
    Are you waiting for the data to actually send before switching...?
    – Majenko
    Commented Dec 10, 2017 at 19:18
  • I set the dataenable pin on uno to high and on mega to low and then try to send data. Doesnt that mean switching ?
    – Safa Dana
    Commented Dec 10, 2017 at 19:21
  • 2
    And what do you do next after sending? Please show your code.
    – Majenko
    Commented Dec 10, 2017 at 19:22
  • Im far away from my pc, but will show that as soon as i sit behind my pc. After i send data from uno i check serial1.available() on mega. But it never runs
    – Safa Dana
    Commented Dec 10, 2017 at 19:28

1 Answer 1

3

You have forgotten to add mySerial.flush().

mySerial.flush() waits for the transmission of outgoing serial data to complete.

Add it right after you have sent the data and want to change the DE pin status.

2
  • Care to elaborate on that? What is the purpose of flush()? What is the effect that it is working around and why does that effect exist? Teach a man to fish...
    – Majenko
    Commented Dec 13, 2017 at 10:45
  • When i added this line to my code, it worked properly! Strange!
    – Safa Dana
    Commented Dec 13, 2017 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.