/*
	RS485.C

   - This program demonstrates the slave side of an RS485 half-duplex
	  communication with a master running the program RS232.C
		
   - The program monitors for any valid message. The program then
     adds 3 to each byte of the message and sends it back to the master.

       Hardware connection:
        master                       slave
        TX+  |  -----------------  |  TX+
        RX+  |                     |  RX+
        TX-  |  -----------------  |  TX-
        RX-  |                     |  RX-

   Master slave communication uses the new opto22 9th bit binary
	protocol. This means that only the targeted slave will open its
	communication to receive the information other than the slave
	address broadcast during the 9th bit mode.

	A more elaborate program would be z0rem.c for the master side and
	sremote.c for the slave side.

	Need the following libraries: modem232.lib and network.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

// uncomment the following line if you are using a pk23xx controller
//#use eziopk23.lib

#define SLAVE_NO 2
char op_rbuf[255];           // receiver buffer
char op_tbuf[255];           // transmitter buffer

char myself;

main()
{
    int i;
    unsigned int testcrc;

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

    op_kill_z1();
    op_init_z1(19200 / 1200, op_rbuf, SLAVE_NO);
    while (1)
    {
    	  hitwd();
        if (check_opto_command() == 1)
        {
            for (i = 0; i < op_rbuf[1] -2; i++)  {
            	 hitwd();
                op_tbuf[i] = op_rbuf[2 + i] + 3;
            }
            replyOpto22(op_tbuf, op_rbuf[1] -2, 0);
        }
    }
}
