/***************************************************************

   SER_DEMO.C

   BL11xx/Little Giant, Tiny Giant, BL13XX/Protocol Switch,
   SmartBlock, PK23xx

   Demonstration of serial port driver for Z180 ASCI port 1. 
   
   BL11xx/Little Giant:
      Connect JW3/JW301 (middle Wago connector)  as follows:

       1 ---................  RXDD
       2 ---........       |  /RXDD
       3 ---       |       |
       4 ---       |       |
       5 ---.......|.......|  TXDD
       6 ---.......|          /TXDD
       7 ---
       8 ---

       Port z1 can be configured as rs485. Uncomment the
       appropriate #define at the top of the program if so.
       
   Tiny Giant:
      Connect JW2/JW201 as follows:

       1 ---
       2 ---
       3 ---................  RXA1D
       4 ---........       |  /RXA1D
       5 ---.......|.......|  TXA1D
       6 ---.......|          /TXA1D
       7 ---
       8 ---

       Port z1 is configured as rs485. Uncomment the
       appropriate #define at the top of the program if so.
       
   BL13xx/Protocol Switch:
      8-pin mini DIN, connect P2 as follows:
                ......
                |    |
           TXA1 6  7 8  RXA1
               5   4  3
                 1  2

      25-pin DB connector, connect 2 to 3.

   SmartBlock:
      Depends on your application board.

   PK23xx:
   	See manual.

       Port z1 can be configured as rs485. Uncomment the
       appropriate #define at the top of the program if so.
       
   This will loop the serial port back on itself so that the signal
   that is sent is received on the same port.  Z-World serial drivers
   are interrupt driven and continue to run while the program executes.

**********************************************************************/

#use "serial.lib"

// Uncomment for PK2300 port z1 configured as rs485
//#define USEPK23_485 1

// Uncomment for Little Giant (BL11xx) or Tiny Giant
//#define USELG_TG 1

#ifdef USEPK23_485
#use eziopk23.lib
#endif

char buf[] = "This message sent over serial line!";
char cc;       /* send buffer counter */
char rbuf[50]; /* receive buffer */
char rcc;      /* receive buffer counter */

main()
{
   int  tt;    /* timeout counter */

   ser_init_z1 (4, 9600/1200);  /* setup serial port 8 bits, no parity */

   cc = sizeof(buf);            /* get number of characters to send */
   rcc = cc;                    /* going to receive the same number */

// Enable RS-485 transmitter for BL11xx/Little Giant, Tiny Giant or PK23xx

#ifdef USEPK23_485
   on_485();
#endif

#ifdef USELG_TG
   outport(ENB485,1);           /* turn on RS-485 transmitter */
#endif

   rbuf[0]=0;                   /* in case print null string */

   ser_rec_z1 (rbuf,&rcc);      /* get receiver ready before sending */
   ser_send_z1 (buf,&cc);       /* OK, now start the transmitter */

   tt=0;                        /* a little hocus pocus */
   while (rcc && --tt) hitwd(); /* wait for receive count = 0 or timeout */

   if (!tt)                     /* if tt == 0 then timed out */
      printf("Timed out - port not loop connected?\n");
   printf("%s\n",rbuf);
}

/*************************************************End of SER_DEMO.C ****/

