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?
1 Answer 1
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.
-
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\$brhans– brhans2020年02月04日 15:59:09 +00:00Commented 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);
andgetResponse()
\$\endgroup\$user_fs10– user_fs102020年02月05日 07:59:57 +00:00Commented Feb 5, 2020 at 7:59
if (Serial3.available()) {
condition not working. \$\endgroup\$Serial.println
returns, then immediately enable RX and wait for incoming comms. I don't know the behavior ofSerial.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\$