I am working on a project that involves two ATMEGA328p MCUs communicating over long distance (between two floors of a building). One is master and the other one is slave. The master always initiates the communication by sending custom made AT commands which have a Carriage return '\r' and line feed '\n' as terminators at the end.
I am using UART (9600 bps with even parity). Here is what I noticed:
When direct UART is used, everything works as expected. But when I replace the communication path with a very long wire (approx 5-10m) the UART started causing issues.
So I had to use RS-485 modules for obvious reasons.
The problem I am seeing is that the whole AT command is transmitted over the RS-485 path but the two ASCII codes of '\r' and '\n' are not received at all. I have checked using an FTDI clone that input to the RS-485 transmitter-side module is having CR and LF as expected but at the receiving end (after RS485 to UART conversion), whole command is received except CR and LF.
Please suggest me what to do as my whole software code depends on detection of CR and LF as a protocol. I have gone through many blogs/QAs but couldn't get my issue resolved. It seems like RS-485 module is not sending CR and LF.
These are the two UART functions that work at the lowest level. I have checked that my code always passes 1 as the trm argument
void UART_TxSTRING (char string[], int trm) //This string function does not sends the NULL character '\0'
{
int i = 0;
while (string[i] != '\0') //Detect end of string
{
UART_TxByte(string[i]);
i++;
}
if (trm == 1) //if trm=1, terminators will be sent with string
{
UART_TxByte('\r'); //Terminator
UART_TxByte('\n'); //Terminator
}
}
void UART_TxByte(char tx_data)
{
UCSR0B &= ~RXIE; //Disable Rx interrupts while sending data
while (!(UCSR0A & TXC_FLAG)); //Check flag before transmitting
UDR0 = (tx_data & 0xFF); //Send the ASCII code
UCSR0B |= RXIE; //Enable Rx interrupts again after transmission
}
Update: I modified the code to replace the CR with @ and replaced LF with $ Still only the termination characters are lost, rest of the command (long or short) is transmitted normally. I am feeling like there is some issue related to timing as the if(trm == 1) statement is causing the RS-485 IC to drop subsequent characters.