#include <v_lib.h>

int	main(int argc, char *argv[])
{
	vhandle_t comm;
	char buf[256], *ptr;
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	if(argc>1)
		comm=commOpen(2,atoi(argv[1]),8,0,0);
	else
		comm=commOpen(2,9600,8,0,0);
	if(!comm)
		return -1;
	printf("Sending all input to COM2. Type 'exit' to exit.\n");
	for(;;)
	{
    	fgets(buf,256,stdin);
        if(!strncmp(buf,"exit",4))
        	break;
        for(ptr = buf;(ptr-buf) < 256;ptr++)
        	if(!*ptr || *ptr == 0x0D || *ptr == 0x0A)
            	break;
		if(ptr-buf)
		{
        	if(!fileWrite(comm,buf,ptr-buf))
            {
				puts("filerite() failed; re-opening port ...");
				fileClose(comm);
				if(argc>1)
					comm=commOpen(2,atoi(argv[1]),8,0,0);
				else
					comm=commOpen(2,9600,8,0,0);
			}
            fwrite(buf,ptr-buf,1,stdout);
            puts("");
		}
	}
	return 0;
}

