Frequently Asked Questions

Using Multiple RS-232 Ports on a PIC® MCU or PIC® DSC

In order to use multiple RS-232 connections on a PIC® MCU or PIC® DSC, multiple #USE RS232 pre-processor directives will need to be setup using the STREAM option. A #USE RS232 pre-processor directive will remain in effect for GETC, PUTC, PRINTF and KBHIT functions until another #USE RS232 is encountered.

The following example program shows how to read from one RS-232 port (A) and echo the data to both the first RS-232 port (A) and a second RS-232 port (B) without using the STREAM option for #USE RS232:

    #USE RS232 (BAUD = 9600, XMIT = PIN_B0, RCV = PIN_B1)
    
    void put_to_a(char c) {
       put(c);
    }
    
    char get_from_a(void) {
       return(getc());
    }
    
    #USE RS232 (BAUD = 9600, XMIT = PIN_B2, RCV = PIN_B3)
    
    void put_to_b(char b) {
       putc(c);
    }
    
    void main(void) {
       char c;
       
       put_to_a("Online\n\r");
       put_to_b("Online\n\r");
       
       while(TRUE) {
          c = get_from_a();
          
          put_to_b(c);
          put_to_a(c);
       }
    }
    

The following example program will perform the same operations as the one above. The STREAM option in the #USE RS232 pre-processor directive makes the code more readable and is therefore recommended method:

    #USE RS232 (BAUD = 9600, XMIT = PIN_B0, RCV = PIN_B1, STREAM = COM_A)
    #USE RS232 (BAUD = 9600, XMIT = PIN_B2, RCV = PIN_B3, STREAM = COM_B)
    
    void main(void) {
       char c;
       
       fprintf(COM_A, "Online\n\r");
       fprintf(COM_B, "Online\n\r");
       
       while(TRUE) {
          c = fgetc(COM_A);
          
          fputc(c, COM_A);
          fputc(c, COM_B);
       }
    }
    

For more information or additional help about using multiple RS-232 ports on a PIC® MCU or PIC® DSC, contact CCS Technical Support.


C-Aware IDE Demo
Embedded C Learners Kit
EZ App Lynx