0
\$\begingroup\$

I want to transfer data (only numeric values) between Arduino Nano and Arduino Mega 2560. I tried with my own code and tried with several libraries also.

Arduino Nano code

 #define RS485Transmit HIGH
 #define RS485Receive LOW
 #define DE_RE 12
 void setup() {
 Serial.begin(9600);
 pinMode(DE_RE, OUTPUT);
 }
 void loop() {
 responseToMaster();
 }
 uint8_t setNum = 0;
 void enaRX() {
 digitalWrite(DE_RE, RS485Receive);
 }
 void enaTX() {
 digitalWrite(DE_RE, RS485Transmit);
 }
 void responseToMaster() {
 enaRX();
 delay(10);
 if (Serial.available() != NULL) {
 setNum = Serial.parseInt();
 if (setNum != 0) {
 Serial.print("setNum = ");
 Serial.println(setNum);
 switch (setNum) {
 case 1:
 enaTX();
 Serial.println(1.2);
 //enaRX();
 break;
 case 2:
 enaTX();
 Serial.println(5);
 //enaRX();
 break;
 }
 }
 }
 }

Arduino Mega code

 #define RS485Transmit HIGH
 #define RS485Receive LOW
 #define DE_RE 12
 uint8_t setNum = 0;
 int setVal = 0;
 void enaRX() {
 digitalWrite(DE_RE, RS485Receive);
 }
 void enaTX() {
 digitalWrite(DE_RE, RS485Transmit);
 }
 void setup() {
 Serial.begin(9600);
 Serial3.begin(9600);
 pinMode(DE_RE, OUTPUT);
 delay(10);
 enaRX();
 }
 void loop() {
 sendRequest(2);
 delay(10);
 getResponse();
 delay(1000);
 }
 void sendRequest(uint8_t number) {
 enaTX();
 Serial3.println(number);
 //enaRX();
 }
 void getResponse() {
 enaRX();
 if (Serial3.available()) {
 while (Serial3.available()) {
 Serial.println(Serial3.read());
 setVal = Serial3.parseInt();
 }
 }
 //Serial.println(setVal);
 }

In this program Arduino Mega transmit a request to Nano. Then Arduino Nano receive it and transmit a value (response) according to request. Then Mega should receive the response.

Arduino Nano receive the value correctly and it transmit correct value according to request. But, Mega didn't receive the response.

What can I do for solve this?

I also tried with few different libraries. But those libraries are complicated for my application. I just simply need to transfer numeric values. Is there any simple library for RS485 communication?

asked Feb 4, 2020 at 15:26
\$\endgroup\$
3
  • \$\begingroup\$ So if your Nano is transmitting the right thing but your Mega doesn't, according to you, receive the response, you should troubleshoot here. Either you're sending the wrong data to the Mega or it's not receiving data at all. You could potentially probe the transmission from the Nano to the Mega to confirm whether or not its getting data. \$\endgroup\$ Commented Feb 4, 2020 at 15:52
  • \$\begingroup\$ Mega doesn't get any data. Because, if (Serial3.available()) { condition not working. \$\endgroup\$ Commented Feb 4, 2020 at 15:57
  • \$\begingroup\$ You might get better answers at Arduino SE. But I suspect that your problems result from your use of delays in your code and you get comms lost there. Ideally you should enable TX, send data, _wait for the send to finish_(might not be the same as when Serial.println returns, then immediately enable RX and wait for incoming comms. I don't know the behavior of Serial.println, but I think that it returns while the device is still transmitting the last byte of the message, and I don't know what the Arduino-ish way is to determine when it's really finished. \$\endgroup\$ Commented Feb 5, 2020 at 12:54

1 Answer 1

3
\$\begingroup\$

From the description that you gave in the question it seems like at the Mega 2560 end did not put its end of the link into receive mode after sending the request.

RS485 is a Half Duplex protocol and you can only send one direction at a time. If the MEGA 2560 is still in transmit mode after sending the request it would be causing a collision on the bus wires when the NANO is trying to send the value back. If you looked at the bus lines with an oscilloscope you could confirm the collision.

answered Feb 4, 2020 at 15:56
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Agreed - the Nano responds as soon as it gets the request, but there's a 10mSec delay in the Mega after it sends before it enables receive. The Nano's response is getting lost in that 10mSec period. \$\endgroup\$ Commented Feb 4, 2020 at 15:59
  • \$\begingroup\$ @brhans I removed the 10mSec delay and check the results. Nano didn't get the request. After I put the delay it works. At least 1mS delay should be between sendRequest(2); and getResponse() \$\endgroup\$ Commented Feb 5, 2020 at 7:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.