/*
	BAREZN1.C
	
	This is a sample program to be used with barezn2.c and barezm.c.

	Instructions:

	1.	connect three controllers via common drop 485 wiring
	2.	compile bareznm.c on one controller, mark as the master
	3.	set master to run mode
	4.	compile barezn2.c on another controller, mark as slave2
	5.	set slave2 to run mode
	6.	compile barezn1.c on the third controller, mark as slave1
	7.	DO NOT set slave1 to run mode
	8.	run slave1 from Dynamic C
	9.	you should see the reception of messages from slave2
		"normal " and "timeout" on the stdio window.
*/

#define ZNMASTER 0		//	I am a slave
#use vdriver.lib
#use aasczn.lib
//#use eziopk23.lib			//	needed only if you use a pk2300

#define BUFSIZ 0x10
	
main() {
char InFrom2[BUFSIZ], OutTo2[BUFSIZ];
CHANNEL Slave2;
char locBuf[30];
char *ptr;
int cnt, moved;
int rxCnt;
char ch;
unsigned long pakCnt;
	
	VdInit();

	Slave2 = aascOpen(DEV_ZNET,0,ZN1200*16|ZNSLAVE*2,NULL);
	znID = 1;

	aascSetReadBuf(Slave2,InFrom2,sizeof(InFrom2));
	aascSetWriteBuf(Slave2,OutTo2,sizeof(OutTo2));
	aascTxSwitch(Slave2,1);
	aascRxSwitch(Slave2,1);
	aascFlush(Slave2);
	pakCnt = 0;

	while ( 1) {
		znPollSlave();

		costate {
			waitfor(DelayMs(250));
			hitwd();
			runwatch();
		}

		costate {
			waitfor(rxCnt = aascReadBlk(Slave2,locBuf,sizeof(locBuf)-1,0));
			locBuf[rxCnt] = 0;
			printf("%lu:%s[%d]\n",++pakCnt,locBuf,rxCnt);
			ch = '\x01';
			waitfor(aascWriteChar(Slave2,ch));
		}
	}
}