UART - getting gibberish data from MAX232 with PIC16F877A mcu

I have created a serial communication between my laptop and 16F877A mcu. I have used MAX232EPE IC as level converter also. According to my code If I send a character to the MCU, MCU will return a String related to the character.

MAX232EPE IC datasheet:http://www.kynix.com/uploadfiles/pdf9675/MAX232EPE.pdf for your reference

Now, what's happening is, when I am sending a character, MCU is getting it properly, because corresponding LED is getting turned on but what MCU is return as String is complete gibberish.

   // Configuration Byte
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000
void main() {
//Initialize USART with baud rate 9600
USARTInit(9600);
TRISB
= 0; PORTBbits.RB0 = 0; //pin 33-Green
int green_led = 0; PORTBbits.RB2 = 0; //pin 35 - Red
int red_led = 0; PORTBbits.RB4 = 0; //pin 37 - Blue
int blue_led = 0; PORTBbits.RB5 = 0; //pin 38 - Yellow
int yellow_led = 0; USARTWriteLine(“Connected”); while(1) {
uint8_t n = USARTDataAvailable();
  </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">n </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">!=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">)</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">char</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTReadData</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">();</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">

  </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">'G'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">green_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB0 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          green_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Green LED turned on"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB0 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          green_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Green LED turned off"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">'R'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">red_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB2 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          red_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Red LED turned on"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB2 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          red_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Red LED turned off"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">'B'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">blue_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB4 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          blue_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Blue LED turned on"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB4 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          blue_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Blue LED turned off"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">data </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">'Y'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">if</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">yellow_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">==</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">){</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB5 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          yellow_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">1</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Yellow LED turned on"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">PORTBbits</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">.</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">RB5 </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          yellow_led </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
          </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteLine</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Yellow LED turned off"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="kwd" style="margin: 0px; padding: 0px; border: 0px; color: #101094;">else</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteChar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">13</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteChar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">10</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteString</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="str" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">"Invalid command : "</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteChar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">data</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteChar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">13</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTWriteChar</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">(</span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">10</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">);</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      n </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">=</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;"> </span><span class="lit" style="margin: 0px; padding: 0px; border: 0px; color: #7d2727;">0</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">;</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
      </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; color: #2b91af;">USARTFlushBuffer</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">();</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
  </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">}</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: #303336;">
}
}

When I am sending “G”, I am receiving “��g����wf�}w��]w���” where I should receive “Green LED turned on”, both are 19 character in length

If anyone can help me, it will be really great.

I can’t help with the code

I can’t help with the code however, what you can try is to test if your max232 circuit is working properly. Do this by connecting tx and rx together. Now you should see everything you type in the terminal being echoed.

 

 

 

 

Gibberish

You did not post the code for the UART. Make sure that the TX port , RC6, data direction register is setup for output.