/*
	UART232.C

	Simple XP8700/RS232 communications with a terminal.
	
   Sample RS232 comm program using the uart expansion card for any
   Z-World board that has the PLCBus.

   Need the following libraries: modem232.lib and uart232.lib.
   Drivers.lib has to be current also ( 5/17/93 or later).
*/

#use "default.h"

#if ((BOARD_TYPE==CPLC_BOARD) || (BOARD_TYPE==L_STAR))
#use cplc.lib
#endif

#use "uart232.lib"

#define USE_UARTEXP          // define this to enable uart interrupt
                             // service
char baud = 9600 / 1200;     // 9600 baud
char mode = 0x04;            //   bit 0  0  1 stop bit
                             //          1  2 stop bit
                             //   bit 1  0  no parity
                             //          1  parity on
                             //   bit 2  0  seven data bits
                             //          1  eight data bits
                             //   bit 3  0  even parity
                             //          1  odd parity
                             //   bit 4  0  no cts rts control
                             //          1  cts, rts control on
char ismodem = 0;            // no modem is connected
char isecho  = 1;            // received data are echoed back (dumb terminals)
char tbuf[100];              // circular transmit buffer
char rbuf[100];              // circular receive buffer

main()
{
char buf[20];            // message max len is set by this array size.

#if ((BOARD_TYPE==CPLC_BOARD) + (BOARD_TYPE==L_STAR))
        uplc_init();
#endif

    reset_pbus();
    reset_pbus_wait();
    reset_pbus_wait();       // double wait to accomodate Little Star 18.432Mhz


    relocate_int1();        // relocate int1 vector during run time
                            // this function will not do anything for ROM file

    while (Dinit_uart(rbuf, tbuf, 100, 100, mode, baud, ismodem, isecho) == -1)
    {
       hitwd();
       runwatch();
    }
                             //  initialized properly,  continue
    Duartsend_prompt();      // send a prompt
    
    while (1)
    {
        // wait for a message terminated with  ENTER.
        while (Dread_uart(buf, ENTER) == 0) {
        		hitwd();
            runwatch();
        }

        // Here you can do whatever you want with the message
        // in this example the message is sent back, then some more ....
        Duartsend_prompt();            // send a prompt
        Dwrite_uart(buf, strlen(buf)); // send back the message
        Duartsend_prompt();            // send another prompt
        Dwrite_uart("it works?", 9);   // write another message that is 9 bytes long
        Duartsend_prompt();
    }
}

