/*
	MG485SLV.C

	RS485 half duplex communication to another BL14xx/MicroG or BL15xx/MicroG2
	running mg485mst.c.

	This slave is set to slave no 1. The master can talk to up to 32 slaves
	(limited by hardware) with slave number ranging from 1 to 255.

	For other samples, look into ..\samples\network.
*/

char rbuf[255];
char query[40];
char reply[40];

main()
{
int j;

	op_init_z1( 8, rbuf, 1);
	// 8 for 9600 baud
	// rbuf is the receiver buffer
	// 1 is the slave no
	
	j = 1;
	while(1)
	{
		hitwd();
		runwatch();
		if(masterquery( query))
		{
			sprintf(reply, "%s, Slave Reply %d",query, j++);
			replyOpto22( reply, strlen(reply), 0);
		}
	 }



}

// slave check this for master query
int masterquery(char *query)
{
	int i;
	if( check_opto_command() != 1) return 0;
	for( i = 0; i< rbuf[1] - 2; i++)
	{
		query[i] = rbuf[2 + i];
	}

	query[i] = '\0';        // put an end of string
	return 1;
}
