/*
   NPORT.C
   Demo program for I-8142 as NPORT server.
	 There are 2 COM ports in the I-8142.That is COM1 and COM2.
*/

/* ------------------------------------------------------------- */
#include <conio.h>
#include <stdlib.h>
#include "..\..\lib\vp2k.h"



int slotno;
void Test8142Com0Com1Rs485(int slot);
void main()
{
	int slot;
	char kin1;
	char *str2;
	InitLib();
			Print ("Input slot of 8142 :");
			kin1=Getch();
			slot = ascii_to_hex(kin1);
			Print ("%d\n\r",slot);
			
	SetInBufSIze(1024);  //set input buffer of com port default:1024bytes
        SetOutBufSIze(1024);			
	
	slot=1;
	InstallCom8000(slot);
	_SetBaudrate(slot,0,115200L);//_SetBaudrate(int slot,int port,unsigned long baud) set slot1 port1 baundrate:115200
	_SetBaudrate(slot,1,115200L);//set slot1 com1 baundrate:115200
	_SetDataFormat(slot,0,8,0,1);//set slot1 com0 8,N,1
	_SetDataFormat(slot,1,8,0,1);
	
	/*
	str2="$01M";
	str2[4]='0';
	ToCom8000Str(slot,0,str2);
	*/
	Test8142Com0Com1Rs485(slot);
//	if(Kbhit() && Getch()=='q') break;
//	}
		RestoreCom8000(slot);		
}
void Test8142Com0Com1Rs485(int slot)
{
	int quit;
	unsigned i,data;
	int ret;
	int error;

	Print("slot %d:COM0->COM1\n\r",slot);
	for(i=0;i<256;i++)
	{	
		ToCom8000(slot,0,i);		
	}
	Print("slot %d:End ToCom0\n\r",slot);
	error=0;
	for(i=0;i<256;i++)
	{
		while(!IsCom8000(slot,1))
		{
			if(Kbhit())
			{   if(Getch()=='q')
				{
					Print("quit test");
					goto end;
				}
				
				else
				{
					Print("error at %d\n\r",i);
					goto end1;
				}
			}
		}
		data=ReadCom8000(slot,1);
		if(i!=data)
		{
			Print("%02X->%02X,%d->%d ",i,data,i,data);
			error++;
		}
		//else Print("\r%d",i);
	}
	Print(" slot=%d,Error=%d\n\r",slot,error);
end1:
	Print("slot %d:COM1->COM0\n\r",slot);
	for(i=0;i<256;i++)
	{		
		ToCom8000(slot,1,i);
		
	}
	Print("slot %d:End ToCom1\n\r",slot);
	error=0;
	for(i=0;i<256;i++)
	{		
		while(!IsCom8000(slot,0))
		{
			if(Kbhit())
			{   if(Getch()=='q')
				{
					Print("quit test");
					goto end;
				}
				else
				{
					Print("error at %d\n\r",i);
					goto end2;
				}
			}
		}
		data=ReadCom8000(slot,0);
		if(data!=i)
		{
			Print("%02X->%02X ",i,data);
			error++;
		}
		//else Print("\r%d",i);
	}
	Print(" slot=%d,Error=%d\n\r",slot,error);
end2:
end:
}
