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
}
}
}