/*
	COM232.C

   Sample Program for com1 and com2 of a standard PC serial card mounted
   on the PC104 Bus or the ISA Bus.

   This program monitors for a CR terminated string from a dumb terminal
	from either of the two serial ports. If a completed string is detected,
	the string is sent back to the dumb terminal.

   A null modem is required for direct RS232 link to a PC serial port, 
	since both serial ports are now DTE's (Data Terminal Equipment).

   If you want to try a link thru a modem, change the baud rate to 2400
   and set ismodem=1.  Then using any pc terminal program, you should be
   able to dial up the board.

	We suggest the use of Hayes SMARTMODEM compatible modems. 

  	Need the following libaries: modem232.lib and com232.lib.
*/

#use "default.h"

#if (BOARD_TYPE!=Z104ISA)
#fatal "This program only runs on the Z104 or ZISA boards."
#endif

// CONTROL PARAMETERS

#define COM1   1
#define COM2   2
#define IBAUD  9600          // baud rate
                             // with modem either 2400 or 1200
                             // without modem => 9600, 4800, etc
                             
#define TBUFSIZE 384         // size of transmit buffer
#define RBUFSIZE 384         // size of receive buffer

char ismodem = 0;            // set to 1 if you want modem
char isecho = 1;             // set to 1 if you want character echoed back to the pc

int mode;

int send_prompt(int comport);
int read_serial(char *buf, char terminate, int comport);
int write_serial(char *buf, int len, int comport);

main()
{
int  ercode;
int  ok_count;
char atbuf[TBUFSIZE];    // transmit buffer chan A
char arbuf[RBUFSIZE];    // receive buffer chan A
char btbuf[TBUFSIZE];    // transmit buffer chan B
char brbuf[RBUFSIZE];    // receive buffer chan B
char buf[20];         // dummy buffer for receiving a complete command

    mode = 0x4;              // 8 bit, no parity, 1 stop bit

                    // initialize rs232 serial port for pc to master comm
    if( Dinit_com1(arbuf, atbuf, RBUFSIZE, TBUFSIZE, mode, 
                   IBAUD / 1200, ismodem, isecho) == -1) {
                   
        printf("COM1 is not found!!!\n");
    }

    if( Dinit_com2(brbuf, btbuf, RBUFSIZE, TBUFSIZE, mode, 
                   IBAUD / 1200, ismodem, isecho) == -1) {
                   
        printf("COM2 is not found!!!\n");
    }

    if (!ismodem)
    {
        send_prompt(COM1);
        send_prompt(COM2);
    }

    for (;;)    // endless loop constantly monitoring for new command line
    {
        runwatch();

        if ((ercode = read_serial(buf, ENTER, COM1)) != 0)
        {
            send_prompt(COM1);
            write_serial("COM1 Responding ",16, COM1);
            write_serial(buf, strlen(buf), COM1);
            send_prompt(COM1);
        }
        if ((ercode = read_serial(buf, ENTER, COM2)) != 0)
        {
            send_prompt(COM2);
            write_serial("COM2 Responding ",16, COM2);
            write_serial(buf, strlen(buf), COM2);
            send_prompt(COM2);
        }
    }
}

int send_prompt(int comport)
{
    if (comport == 1)
        Dcom1send_prompt();
    else 
        Dcom2send_prompt();
}

int read_serial(char *buf, char terminate, int comport)
{
    if (comport == 1) 
        return Dread_com1(buf, terminate);
    else 
        return Dread_com2(buf, terminate);
}

int write_serial(char *buf, int len, int comport)
{
    if (comport == 1) 
        return Dwrite_com1(buf, len);
    else 
        return Dwrite_com2(buf, len);
}