/*

This program works with znslv01.c of various controllers to blink
an LED, click a relay or do something else to verify RS485 communication
via the ZNet is successful. Replace #use eziopk25.lib with the appropriate
library if you are not using the PK2500 controller board.

*/

#define ZNMASTER 1		//	I am the master
#define RUNKERNEL 1		//	need background task
#use vdriver.lib
#use aasczn.lib
#use eziopk25.lib		// needed only if you are using a pk23xx controller.
#use srtk.lib

#funcchain _srtk_hightask znPollMaster

#define __ZN_POLLINTERVAL 25

#define BUFSIZ 16

main() {
	char InFrom2[BUFSIZ], OutTo2[BUFSIZ];
	CHANNEL Slave1;
	char locBuf[30];
	char curState;
	
	VdInit();
	znInit(ZN1200*16,0,NULL,0);
	znSetMap(1,ZNMUSTPOLL);
	init_srtkernel();
	
	Slave1 = aascOpen(DEV_ZNET,0,ZN1200*16|ZNSLAVE*1,NULL);
	znID = 0;

	aascSetReadBuf(Slave1,InFrom2,sizeof(InFrom2));
	aascSetWriteBuf(Slave1,OutTo2,sizeof(OutTo2));
	aascTxSwitch(Slave1,1);
	aascRxSwitch(Slave1,1);
	aascFlush(Slave1);
	
	while (1) {
		costate {
			waitfor(DelayMs(500));
			locBuf[0] = !locBuf[0];
			waitfor(aascWriteChar(Slave1,locBuf[0]));
			waitfor(aascReadChar(Slave1,&curState));
			if (curState != locBuf[0]) sysForceReset();
			hitwd();
			}
		}
	}