// multimeter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//#include <vcl.h>

#pragma hdrstop


//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// otevrit COM port, pro win XP a COM vyssi nez 9 uvadet jmena ve formatu "\\\\.\\COM10"
//---------------------------------------------------------------------------
int COMopen(HANDLE *cporth, LPCWSTR cportname)
{
	*cporth=CreateFile(cportname,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
	if(*cporth==INVALID_HANDLE_VALUE)
		return(1);
	return(0);
}
//---------------------------------------------------------------------------
// zavrit COM port
//---------------------------------------------------------------------------
void COMclose(HANDLE *cporth)
{
	CloseHandle(*cporth);
	*cporth=NULL;
}
//---------------------------------------------------------------------------
// nastavit velikost RX, TX bufferu
//---------------------------------------------------------------------------
BOOL COMsetBuf(HANDLE cporth, int inlen, int outlen)
{
	return(SetupComm(cporth,inlen,outlen));
}
//---------------------------------------------------------------------------
// nastavit format ramce (baudrate a pocet stopbitu)
//---------------------------------------------------------------------------
void COMsetFormat(HANDLE cporth, int baud, int stopbit,int bytes)
{
	DCB dcb;
	GetCommState(cporth,&dcb);
	dcb.fBinary=true;
	dcb.fAbortOnError=false;
	dcb.BaudRate=baud;
	dcb.StopBits=stopbit;
	dcb.Parity=NOPARITY;
	dcb.ByteSize=bytes;
	SetCommState(cporth,&dcb);
}
//---------------------------------------------------------------------------
// nastavit timeouty (rxtime/txtime v ms)
//---------------------------------------------------------------------------
void COMsetTimeouts(HANDLE cporth, int rxinterval, int rxtime, int rxmult, int txtime, int txmult)
{
	COMMTIMEOUTS timeouts;
	timeouts.ReadIntervalTimeout=rxinterval;
	timeouts.ReadTotalTimeoutMultiplier=rxmult;
	timeouts.ReadTotalTimeoutConstant=rxtime;
	timeouts.WriteTotalTimeoutMultiplier=txmult;
	timeouts.WriteTotalTimeoutConstant=txtime;
	SetCommTimeouts(cporth,&timeouts);
}
//---------------------------------------------------------------------------
// vymazat vstupni buffer
//---------------------------------------------------------------------------
void COMflushInput(HANDLE cporth)
{
	PurgeComm(cporth,PURGE_RXCLEAR);
}
//---------------------------------------------------------------------------
// odeslat byte (vraci pocet zapsanych bytu)
//---------------------------------------------------------------------------
unsigned long COMtxByte(HANDLE cporth, unsigned char byte)
{
	unsigned long written;
	WriteFile(cporth,&byte,1,&written,NULL);
	return(written);
}
//---------------------------------------------------------------------------
// cist byte (vraci pocet prectenych bytu)
//---------------------------------------------------------------------------
unsigned long COMrxByte(HANDLE cporth, unsigned char *byte)
{
	unsigned long read;
	ReadFile(cporth,byte,1,&read,NULL);
	return(read);
}
//---------------------------------------------------------------------------
// odeslat data (vraci pocet skutecne zapsanych bytu)
//---------------------------------------------------------------------------
unsigned long COMtxData(HANDLE cporth, void *data, unsigned long datalen)
{
	unsigned long written;
	WriteFile(cporth,data,datalen,&written,NULL);
	return(written);
}
//---------------------------------------------------------------------------
// cist data (vraci pocet skutecne nactenych bytu)
//---------------------------------------------------------------------------
unsigned long COMrxData(HANDLE cporth, void *data, unsigned long datalen)
{
	unsigned long read;
	WriteFile(cporth,data,datalen,&read,NULL);
	return(read);
}

void setzdroj(HANDLE cporth, unsigned int M ,unsigned int N){
		int znak_A,znak_B,znak_C,znak_D;
		znak_B = M % 256;
		znak_D = N % 256;
		znak_A = ((M - znak_B) / 256);
		znak_C = ((N - znak_D) / 256);

		COMflushInput(cporth);                  // flushnout RX buffer
		COMtxByte(cporth,znak_A);                  
		COMtxByte(cporth,znak_B);                  
		COMtxByte(cporth,znak_C);                  
		COMtxByte(cporth,znak_D);                  
		COMtxByte(cporth,(znak_A ^ znak_B ^ znak_C ^ znak_D));
}



HANDLE merak=NULL;
HANDLE zdroj=NULL;



int _tmain(int argc, _TCHAR* argv[])
{
	unsigned char rxb;

	// nejdriv otevrit COM port
	if(COMopen(&merak,TEXT("COM2"))!=0)
	{
		printf("Can't open selected COM port!\n");
		getch();
		return(1);
	}

	if(COMopen(&zdroj,TEXT("COM1"))!=0)
	{
		printf("Can't open selected COM port!\n");
		getch();
		return(1);
	}

    //parametry komunikace meraku
	COMsetBuf(merak,1024,256);
	COMsetFormat(merak,19200,ONESTOPBIT,7);
	COMsetTimeouts(merak,0,100,1,100,1);
	
	//parametry komunikace zdroje
	COMsetBuf(zdroj,1024,256);
	COMsetFormat(zdroj,9600,ONESTOPBIT,8);
	COMsetTimeouts(zdroj,0,100,1,100,1);


	int cnt = 0;
	int polohatecky =0;
	char numero[20];
	int offset=0;
	int nastaveni=0;

	FILE *fw = NULL;
	fw = fopen("./mereni.txt","w");

	while(nastaveni < 65535){
		setzdroj(zdroj,nastaveni,nastaveni);
		Sleep(200);
		for(int mer = 0; mer < 2; mer++)
		while(1){
			if(COMrxByte(merak,&rxb)==1){
				if(int(rxb) == 10){
					numero[cnt] = 0;
					offset = 0;
					cnt = 0;
	//				printf("nastaveni %d hodnota : %s\n",nastaveni,numero);
	//				fprintf(fw,"%d;%s\n",nastaveni,numero);
					break;
				}
				if(cnt == 0) polohatecky = int(rxb) - 48;
				if(cnt > 0 && cnt < 5){
					if(cnt-2 == polohatecky){
						offset=1;
						numero[cnt-1]=46;
					}
					numero[cnt-1+offset] = rxb;
				}
				if(cnt > 4) numero[cnt-1+offset] = 0;
			cnt++;
			}
		}
		printf("nastaveni %d hodnota : %s\n",nastaveni,numero);
		fprintf(fw,"%d;%s\n",nastaveni,numero);
	nastaveni = nastaveni + 100;
	}
	// ted ho zase zavrit
	COMclose(&merak);
	COMclose(&zdroj);
	fclose(fw);
	getch();
	return 0;
}

