/*******
master_demo.c

connected to a slave with a looped back serial port on channel 0x42

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

#use "master_serial.lib"

#define SP_CHANNEL 0x42

char* const test_string = "Hello There";

main()
{
	char buffer[100];
	int read_length;

	MSinit(0);
	
	//comment this line out if talking to a stream handler
	printf("open returned:0x%x\n", MSopen(SP_CHANNEL, 9600));

	while(1)
	{
		costate
		{	
			wfd{ cof_MSwrite(SP_CHANNEL, test_string, strlen(test_string)); }
			wfd{ cof_MSwrite(SP_CHANNEL, test_string, strlen(test_string)); }
		}

		costate
		{
			wfd{ read_length = cof_MSread(SP_CHANNEL, buffer, 99, 10); }
			if(read_length > 0)
			{
				buffer[read_length] = 0; //null terminator
				printf("Read:%s\n", buffer);
			}
			else if(read_length < 0)
			{
				printf("Got read error: %d\n", read_length);
			}
			printf("wrfree = %d\n", MSwrFree(SP_CHANNEL));
		}
	}	 		
}