0
\$\begingroup\$

I am trying to send data from Arduino mega (message "ping") to Arduino nano, then send back (message pong) to the mega board, I am not receiving "pong" back to the mega board, why is that? I have used uart tx complete interrupt to switch rs485 DE and RE pins as shown here in both Arduino boards

mega code

//Atmega2560
#define SerialTxControl 52 // DE RE
#define RS485Transmit HIGH
#define RS485Receive LOW
char buffer [100];
byte state = 0;
void setup (void) {
    UCSR1B |= (1<<TXCIE1);     // TX Complete Interrupt Enable serial 1
    Serial.begin (9600); // set the serial port to 9600baud speed

    Serial1.begin (9600); // set the serial port to 9600baud speed
    pinMode (SerialTxControl, OUTPUT);
    digitalWrite (SerialTxControl, RS485Transmit); // put the device into transmitter mode
}

ISR (USART_TX_vect)
{
   digitalWrite(SerialTxControl, RS485Receive);
}
void loop (void) {

//transmit rs485
digitalWrite (SerialTxControl, RS485Transmit);
Serial1.print ("ping"); // send text rs485


//receive rs485
int i = 0;
if (Serial1.available ()) {// if any data came to the port
    delay (5); // wait a bit for the entire data packet to be accepted by the port
    while (Serial1.available ()) {
        buffer [i ++] = Serial1.read (); // read data and write it to the buffer
    }
}
if (i> 0) {// if there is something in the buffer
    buffer [i ++] = '\ 0'; // convert the buffer contents to a string by adding a null character
        if (strcmp (buffer, "pong")) {// if the accepted string is equal to the ping text
        digitalWrite (13, state); // blink LED
        state =! state;
        Serial.println("pong"); // print serial monitor
        }
}     
}

nano code

//Nano    
#define SerialTxControl 3 // DE RE
#define RS485Transmit HIGH
#define RS485Receive LOW

char buffer [100];
byte state = 0;

void setup (void) {
    UCSR0B |= (1<<TXCIE0);     // TX Complete Interrupt Enable serial 0
    Serial.begin (9600);
    pinMode (13, OUTPUT); 
    pinMode (SerialTxControl, OUTPUT); 
    digitalWrite (SerialTxControl, RS485Receive); // put the device into receiver mode
}

ISR (USART_TX_vect)
{
    digitalWrite(SerialTxControl, RS485Receive);
}

void loop (void) {

//receive rs485
int i = 0;
if (Serial.available ()) {// if any data came to the port
    delay (5); // wait a bit for the entire data packet to be accepted by the port
    while (Serial.available ()) {
        buffer [i ++] = Serial.read (); // read data and write it to the buffer
    }
}
if (i> 0) {// if there is something in the buffer
    buffer [i ++] = '\ 0'; // convert the buffer contents to a string by adding a null character
    if (strcmp (buffer, "ping")) {// if the accepted string is equal to the ping text
        digitalWrite (13, state); // blink LED
        state =! state;
        digitalWrite (SerialTxControl, RS485Transmit);
        Serial.print ("pong");    //transmit rs485

    }
}
}
\$\endgroup\$
2
  • 3
    \$\begingroup\$ Debugging questions need to show some effort to analyze the problem, not simply report that the attempt does not work. What happens if you temporarily drop the RS485 transceivers and just connect the UARTs? What happens if you look at the RS485 with a scope? And it's enable signal? \$\endgroup\$ Commented Sep 23, 2020 at 4:50
  • \$\begingroup\$ I do notice you are using delays in serial receive code, that is terribly unwise. \$\endgroup\$ Commented Sep 23, 2020 at 4:51

0

Your Answer

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

Browse other questions tagged or ask your own question.