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

#include <vcl.h>
#pragma hdrstop
#include <iostream>
#include <math.h>
#include <cstring>
#include <stdio.h>
#include <fstream>
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---snímač polohy---
int i;
	DWORD pocet;
	UCHAR data;
	UCHAR buffer;
	float koeficient;
	float KCelkovaPoloha;
	int DelkaLinky;
	//---end snímač polohy---

using namespace std;
HANDLE hComm = INVALID_HANDLE_VALUE;
AnsiString com;
char qq;
int nn = 1;

Pole[] = {1,3,5,7,9,12,15,18,21};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//-------------------------------------------------------------
 void spravne_zapsano1(void)
{
int q = 0, ciselB;
char B[1000];
int C[1000];
AnsiString R;
int xB = StrToInt(Form1 ->Memo1 -> Lines -> Count);       //pocet radku

  //* zjist je-li počet sloupců Mema1 ve vsech radcich stejny *//
  for( int l=1; l<(xB) ; l++)
   {
    ciselB = Form1 ->Memo1 -> Lines -> Strings [l].Length();       // ciselB=počet řady
    strcpy (B , Form1 ->Memo1 -> Lines -> Strings [l].c_str());    // do B jednotlive řádky
    for ( int k=0; k<ciselB ; k++)
      {
        if ( B[k]==' ')
          {
             q++;                                                      // mezery
          }
      }
   C[l]= (q);
   q = 0;
   }

  for( int l=2; l<(xB) ; l++)
   {
   if ( C[l-1]!= C[l] )                                                     // je ve vsech radcich stejne mezer
    {
      R = "Hodnoty v tabulce jsou špatně zadané. \nZkontrolujte správnost zápisu viz. O aplikaci ";
      MessageDlg (R,mtWarning,TMsgDlgButtons()<<mbOK,0);                  // chybove hlasení
    }
   }
}
//---------------------------------------------------------------------------
short int komunikace(void)
{
//--------TTT-III-MMM-EEE-RRR---------
UCHAR buffer;
UCHAR Data = 0;
signed char Pohyb = 0;    //signed char +-128
int PohybSoucet = 0;

short int Hodnota = 0;

UCHAR PosilanaData = 0x03;	//BYTE adresy registru, ze kterého čteme (dělá problémy při definici jinde)
UCHAR Pole[8];// = {0};
UCHAR Byte = 0x02;     //RESET = 1,ostatní = 0
int i;
int a = 1;


for (i = 0; i < 8; i++) {

	if (PosilanaData & 0x80) Pole[i] = 1;  //zjišťování přítomnosti log.1 na
	else Pole[i] = 0;                      //místě 0x08 v bytu PosilanaData
	PosilanaData <<= a;  //bitovy posuv bytu

	if (Pole[i] == 0) {                    //maskování bitu 0x10 do vysílaného bytu "Byte"
		Byte = Byte & 0xef;		//změna na log.0
	}
	else Byte = Byte | 0x10;    //změna na log.1

//--------------SEND ADRESS BYTE------------------------------------

		Byte = Byte & 0xfe;	//SCLK = 0
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		Byte = Byte | 0x01;		//SCLK = 1
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		Byte = Byte | 0x01;		//SCLK = 1
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		if (i == 7) goto Read;
		else Byte = Byte & 0xfe;	//SCLK = 0
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
	}
//-----------END SEND ADRESS BYTE-----------------------------------
Read:	Byte = Byte | 0x01;		//SCLK = 1 -> ČEKÁNÍ NA PŘÍJEM
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND

//-------------READ DATA BYTE---------------------------------------
	for (i = 0; i < 8; i++) {
		Byte = Byte & 0xfe;	//SCLK = 0
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		Byte = Byte & 0xfe;	//SCLK = 0
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		Byte = Byte | 0x01;		//SCLK = 1
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND
		ftStatus = FT_GetBitMode(ftHandle, &buffer);		//READ
		Byte = Byte | 0x01;		//SCLK = 1
		ftStatus = FT_Write(ftHandle, &Byte, 1, &pocet);	//SEND

		//----UKLADANI PRIJIMANEHO BYTU-----
		if (buffer & 0x08) Data = Data | 0x01;   //VEPÍŠE JEDNIČKU
		else Data = Data & 0xfe;      // 0xfe JE 0x01 NEGOVANE - VEPÍŠE NULU
		if (i != 7) Data <<= a;
		}
	if (Data > 127) {         //VYHODNOCENI PRIJMUTEHO BYTU
		Pohyb = Data - 256;
	}
		else Pohyb = Data;
    
	b++;

	Hodnota = Pohyb;
	return Hodnota;

}
//-------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i=0;
unsigned long int n;
AnsiString out;

if (hComm == INVALID_HANDLE_VALUE)
        {
        com=Edit2->Text;
        hComm = CreateFile( com.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
        DCB dcb;
        if (hComm == INVALID_HANDLE_VALUE)
                {
                StatusBar1->Panels->Items[1]->Text="Prd Error\n";
                return;
				}

        FillMemory(&dcb, sizeof(dcb), 0);            // nastaveni parametru
        dcb.DCBlength = sizeof(dcb);
        //38400,n,8,1
		GetCommState (hComm, &dcb);
        dcb.BaudRate=CBR_19200;
        dcb.fBinary=TRUE;
        dcb.fParity=FALSE;
        dcb.fOutxCtsFlow=FALSE;
        dcb.fOutxDsrFlow=FALSE;
        dcb.fDtrControl=DTR_CONTROL_DISABLE;
        dcb.fDsrSensitivity=FALSE;
        dcb.fTXContinueOnXoff=TRUE; // XOFF continues Tx
        dcb.fOutX=TRUE;
        dcb.fInX=TRUE;
        dcb.fErrorChar=FALSE;       // enable error replacement
        dcb.fNull=FALSE;
        dcb.fRtsControl=RTS_CONTROL_DISABLE;
        dcb.fAbortOnError=FALSE;
        dcb.XonLim=6;               // transmit XON threshold
        dcb.XoffLim=6;              // transmit XOFF threshold
        dcb.ByteSize=8;
        dcb.Parity=NOPARITY;
        dcb.StopBits=ONESTOPBIT;     //TWOSTOPBITS;
        dcb.XonChar=17;              // Tx and Rx XON character
        dcb.XoffChar=19;             // Tx and Rx XOFF character
        dcb.ErrorChar=21;            // error replacement character
        dcb.EofChar=13;              // end of input character
        dcb.EvtChar=0;              // received event character



        if (!SetCommState(hComm, &dcb))
                {
                StatusBar1->Panels->Items[1]->Text="SetCommState Error\n";
                return;
                }

        COMMTIMEOUTS ctmoNew;                      // timeouts intervaly
        ctmoNew.ReadIntervalTimeout = 20;
        ctmoNew.ReadTotalTimeoutConstant = 10;
        ctmoNew.ReadTotalTimeoutMultiplier = 100;
        ctmoNew.WriteTotalTimeoutMultiplier = 10;
        ctmoNew.WriteTotalTimeoutConstant = 100;
        SetCommTimeouts(hComm, &ctmoNew);


  StatusBar1->Panels->Items[1]->Text="Device status: initialized";
  }
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
   unsigned long int n;
   int a;
   AnsiString f1;
   int f;
   char f2[7];

    f=StrToInt(Edit3->Text);
    f=16*f+534;
    f1=IntToHex(f,4);


     char l;
        l=0x0d;
     f2[0] ='*';
     f2[1] ='F';
     f2[2]=f1[1];
     f2[3]=f1[2];
     f2[4]=f1[3];
     f2[5]=f1[4];
     f2[6]=0x0D;
    //kmit2=IntToHex(kmit,5);

   if (!WriteFile(hComm,f2,8,&n,0))        // vyslani povelu "TAKT1"
        {
          StatusBar1->Panels->Items[1]->Text=("Device status: Inicializujte seriový port");
          CloseHandle(hComm);
          hComm = INVALID_HANDLE_VALUE;

        }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
	float mmpoloha;


   short int poloha = 0;
   int i;
   AnsiString out = ' ';
   unsigned long int n;
   char napeti[5];
   napeti[0] ='*';
   napeti[1] ='?';
   napeti[2]='A';
   napeti[3]='8';
   napeti[4]=0x0D;

   if (nn < 2)
	{
      while (n > 0)                               // vycteni bufferu
       {
		ReadFile(hComm, &qq, 1, &n, NULL);
	   }
	}

 if (!WriteFile(hComm,napeti,5,&n,0))        // vyslani povelu "zjisti napeti"
	 {
		StatusBar1->Panels->Items[1]->Text="Device status: Inicializujte seriový port";
		CloseHandle(hComm);
		hComm = INVALID_HANDLE_VALUE;
		return;
	 }
  
  i=1;
  qq=' ';
  n=1;
  while ((i<40)&&(n>0))                   // cteni odpovedi
	 {
        ReadFile(hComm, &qq, 1, &n, NULL);
        out=out+qq;
        i++;
     }
 //-----------------------
	poloha = komunikace();
	mmpoloha = (koeficient * poloha);
	CelkovaPoloha += mmpoloha;
 //-----------------------

  float vysledek;
  for (int mm=1; mm < i; mm++)
	{
	if (out[mm] == 'A')
      {
		if (out [mm+1] == ' ')
          {
		   if (out [mm+2]!= ' ')
             {
			 vysledek=(out[mm+2]-48)*100+(out[mm+3]-48)*10+(out[mm+5]-48);
             }
           else
			 {
			 vysledek = (out[mm+3]-48)*100+(out[mm+4]-48)*10;
             }

		   SG1->Cells[1][nn] = vysledek/10;
		   SG1->Cells[0][nn] = CelkovaPoloha;
		   nn++;
		  }

		if ((out [mm+1] == '<')||(out [mm+1] == '>') )
		  {
		   if (out [mm+2]!=' ')
			 {
			  vysledek=(out[mm+2]-48)*100+(out[mm+3]-48)*10; //+(out[m+5]-48); //+out[m+4];
			 }
		   else
			 {
			  vysledek=(out[mm+4]-48)*100+(out[mm+5]-48)*10; //+(out[m+6]-48); //+out[m+5];
			 }
		   SG1->Cells[1][nn] = vysledek/10;
		   SG1->Cells[0][nn] = CelkovaPoloha;
		   nn++;
		 }
	  }
	}
  if (nn > 2000)  //počet hodnot měření
  {
  Timer1->Enabled = false;
  }

 
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button4Click(TObject *Sender)
{
	data = 0x11;   //reset
	ftStatus = FT_Write(ftHandle, &data, 1, &pocet);
	data = 0x13;
	ftStatus = FT_Write(ftHandle, &data, 1, &pocet);

	Timer1->Enabled = true;
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Nati1Click(TObject *Sender)
{
Form1 -> Memo1 -> Lines -> Clear();
OpenDialog1->Filter = "Textovy soubor (*.txt)|*.txt";                 // otevře se matice A , pouze soubory .txt
if (OpenDialog1->Execute())
   {
   Memo1 -> Lines  -> LoadFromFile(OpenDialog1->FileName);             //matice se zobrazi do mema
   }

   spravne_zapsano1();

   // načtení hodnot z mema do matice
  int i,j,cisel,xa;
  AnsiString M;
  int n = 0;
  String znakA="";
  xa = StrToInt(Memo1 -> Lines -> Count);       //pocet radku
  float Matice[2000][2];

   for ( i=1 ; i < xa ; i++)    //nacti od druheho radku z .txt
    {
	 n = 0;
     M = Memo1 -> Lines -> Strings [i];                   // Do M řádky z Mema
     cisel = Memo1 -> Lines -> Strings [i].Length();     // Počet míst na řádku v Memu
       for ( j=2 ; j <=(cisel) ; j++)
         {
          if ( M[j]!=' ')
			 znakA = znakA + M[j];                           // Císlo + číslo dukud M se nerovná mezeře
		  if ( M[j] == ' ')                                  // Pokud M se rovna mezeře
           {
             Matice[i][n] = (float)StrToFloat(znakA);        // Do GlPromMatA se uklada cislo
             n++;
             znakA = "";                                     // nuluj znak
		   }
         }
     }
int x, y;
for( x=0; x < 2; x++)
    {
    for( y=1; y < xa; y++)
        {
         Form1->SG1->Cells[x][y] = FloatToStrF(Matice[y][x], ffGeneral, 4, 6);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ulo1Click(TObject *Sender)
{
int i,j,cisel,xa;
AnsiString M;
String znakA="";
xa = StrToInt(Memo1 -> Lines -> Count);       //pocet radku
float Matice[2000][2];
int x, y;
Memo1 -> Lines -> Clear();
for (i=0; i<xa; i++)
  {
   Memo1 -> Lines -> Add(" "+(Form1->SG1->Cells[0][i])+" "+(Form1->SG1->Cells[1][i])+" ");
  }

SaveDialog1->Filter = "Textovy soubor (*.txt)|*.txt|All files (*.*)|*.*";
SaveDialog1->FilterIndex = 1;

if (SaveDialog1->Execute())
  {
   Memo1 -> Lines -> SaveToFile( SaveDialog1->FileName + ".txt");             // uloží data do souboru
  }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Konec1Click(TObject *Sender)
{
ftStatus = FT_Close(ftHandle);
Close();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender)
{
   // nacti hodnoty z SG1 do Mema1
   String znakA1="";
   int xa1=0;
   for( int n1=0; n1 < 2000; n1++)
    {
    if (Form1->SG1->Cells[0][n1] != znakA1)
    xa1++;
    }

   Form1 -> Memo1 -> Lines -> Clear();
   for (int ii=1; ii<xa1; ii++)
     {
       Form1 -> Memo1 -> Lines -> Add(" "+(Form1->SG1->Cells[0][ii])+" "+(Form1->SG1->Cells[1][ii])+" ");
     }


  int SP = StrToInt(Form1 -> PaintBox2-> Height);
  int DP = StrToInt(Form1 -> PaintBox2-> Width);

  Form1 -> PaintBox2->Canvas->Pen->Color=clBlack;
      // osy
  Form1 -> PaintBox2->Canvas->MoveTo(20,SP-10);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-10);
  Form1 -> PaintBox2->Canvas->MoveTo(20,SP-10);
  Form1 -> PaintBox2->Canvas->LineTo(20,10);
    // sipky
  Form1 -> PaintBox2->Canvas->MoveTo(20,10);
  Form1 -> PaintBox2->Canvas->LineTo(17,20);
  Form1 -> PaintBox2->Canvas->MoveTo(20,10);
  Form1 -> PaintBox2->Canvas->LineTo(23,20);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-10,SP-10);
  Form1 -> PaintBox2->Canvas->LineTo(DP-20,SP-13);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-10,SP-10);
  Form1 -> PaintBox2->Canvas->LineTo(DP-20,SP-7);
    // carky  vodorovne
  Form1 -> PaintBox2->Canvas->MoveTo(DP-685,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-685,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-625,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-625,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-565,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-565,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-505,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-505,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-445,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-445,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-385,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-385,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-325,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-325,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-265,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-265,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-205,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-205,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-145,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-145,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-85,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-85,SP-5);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-25,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-25,SP-5);
   // carky  svisle
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-10);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-70);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-70);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-130);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-130);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-190);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-190);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-250);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-250);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-310);
  Form1 -> PaintBox2->Canvas->LineTo(DP-690,SP-310);
  // mrizkovani
  PaintBox2 ->Canvas->Pen->Color=clLtGray;
  PaintBox2 ->Canvas->Pen->Style = psDash;
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-70);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-70);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-130);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-130);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-190);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-190);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-250);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-250);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-680,SP-310);
  Form1 -> PaintBox2->Canvas->LineTo(DP-10,SP-310);
   // carky  svisle
  Form1 -> PaintBox2->Canvas->MoveTo(DP-625,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-625,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-565,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-565,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-505,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-505,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-445,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-445,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-385,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-385,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-325,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-325,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-265,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-265,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-205,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-205,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-145,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-145,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-85,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-85,10);
  Form1 -> PaintBox2->Canvas->MoveTo(DP-25,SP-15);
  Form1 -> PaintBox2->Canvas->LineTo(DP-25,10);
	// styl
  Form1 -> PaintBox2->Canvas->Pen->Color=clBlack;
  Form1 -> PaintBox2->Canvas->Pen->Style = psSolid;


int i,j,cisel,xa;
AnsiString M;
String znakA="";
xa = StrToInt( Form1 -> Memo1 -> Lines -> Count);       //pocet radku
float Matice[2000][2];
int x, y;

  // načtení hodnot z mema do matice

  int n = 0;
   for ( i=0 ; i < xa ; i++)
    {
     n = 0;
     M = Form1 -> Memo1 -> Lines -> Strings [i];                   // Do M řádky z Mema
     cisel = Form1 -> Memo1 -> Lines -> Strings [i].Length();     // Počet míst na řádku v Memu
       for ( j=2 ; j <=(cisel) ; j++)
         {
          if ( M[j]!=' ')
			 znakA = znakA + M[j];                           // Císlo + číslo dukud M se nerovna mezeře
		  if ( M[j] == ' ')                                  // Pokud M se rovna mezeře
           {
             Matice[i][n] = (float)StrToFloat(znakA);        // Do GlPromMatA se uklada cislo
             n++;
             znakA = "";                                     // nuluj znak
           }
         }
     }
 // zjisteni maximalni a minimalni frekvence
   int m1,m2;
   float maxf,minf;

     float konst1=0.000001;
     for (m1=0 ; m1<xa ; m1++)
       if (Matice[m1][0]>konst1)
       {
       maxf = Matice[m1][0];
       konst1=Matice[m1][0];
       }
	 float konst2=1000000;
     for (m2=0 ; m2<xa ; m2++)
       if (Matice[m2][0]<konst2)
       {
       minf = Matice[m2][0];
       konst2=Matice[m2][0];
       }
// zjisteni maximalni a minimalni hodnoty odporu
   int m3,m4;
   float maxA,minA;
   float konst3=0.000001;

   for (m3=0 ; m3<xa ; m3++)
       if (Matice[m3][1]>konst3)
       {
       maxA = Matice[m3][1];
       konst3=Matice[m3][1];
       }

   float konst4=1000000;
   for (m4=0 ; m4<xa ; m4++)
       if (Matice[m4][1]<konst4)
	   {
       minA = Matice[m4][1];
       konst4=Matice[m4][1];
       }

   Form1 -> Label13 -> Caption = FloatToStrF(minf,ffFixed,10,0);
   Form1 -> Label18 -> Caption = FloatToStrF(maxf,ffFixed,10,0);
   Form1 -> Label7 -> Caption = FloatToStrF(minf+(maxf-minf)/11,ffFixed,10,0);
   Form1 -> Label6 -> Caption = FloatToStrF(minf+(maxf-minf)*2/11,ffFixed,10,0);
   Form1 -> Label5 -> Caption = FloatToStrF(minf+(maxf-minf)*3/11,ffFixed,10,0);
   Form1 -> Label12 -> Caption = FloatToStrF(minf+(maxf-minf)*4/11,ffFixed,10,0);
   Form1 -> Label4 -> Caption = FloatToStrF(minf+(maxf-minf)*5/11,ffFixed,10,0);
   Form1 -> Label3 -> Caption = FloatToStrF(minf+(maxf-minf)*6/11,ffFixed,10,0);
   Form1 -> Label2 -> Caption = FloatToStrF(minf+(maxf-minf)*7/11,ffFixed,10,0);
   Form1 -> Label1 -> Caption = FloatToStrF(minf+(maxf-minf)*8/11,ffFixed,10,0);
   Form1 -> Label11 -> Caption = FloatToStrF(minf+(maxf-minf)*9/11,ffFixed,10,0);
   Form1 -> Label17 -> Caption = FloatToStrF(minf+(maxf-minf)*10/11,ffFixed,10,0);

   Form1 -> Label8 -> Caption = FloatToStrF(minA,ffFixed,10,1);
   Form1 -> Label21 -> Caption = FloatToStrF(maxA,ffFixed,10,1);
   Form1 -> Label9 -> Caption = FloatToStrF(minA+(maxA-minA)/5,ffFixed,10,1);
   Form1 -> Label19 -> Caption = FloatToStrF(minA+(maxA-minA)*2/5,ffFixed,10,1);
   Form1 -> Label20 -> Caption = FloatToStrF(minA+(maxA-minA)*3/5,ffFixed,10,1);
   Form1 -> Label10 -> Caption = FloatToStrF(minA+(maxA-minA)*4/5,ffFixed,10,1);

   float osax,osay,x1,y1;
   float k = Matice[0][0];
   float l = Matice[0][1];
   osay = abs((l-minA)*300/(maxA-minA)-327) ;
   osax = (k-minf)*660/(maxf-minf)+20  ;
   Form1 -> PaintBox2->Canvas->Pen->Style = psSolid;
   Form1 -> PaintBox2 -> Canvas ->MoveTo(osax,osay);

   for (int o=1 ; o<(xa) ; o++)
     {
     float p = Matice[o][0];
     float q = Matice[o][1];

      y1 = abs((q-minA)*300/(maxA-minA)-327) ;
      x1 = (p-minf)*660/(maxf-minf)+20  ;

      Form1 -> PaintBox2 -> Canvas ->LineTo(x1,y1);
     }
     Form1 -> PaintBox2->Canvas->Pen->Style = psSolid;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button7Click(TObject *Sender)
{
	for (int k=1; k<3; k++)
    {
	 for (int l=1; l<301; l++)
         {
		 SG1->Cells[k-1][l] = "";
         }
	}
	nn=1;
	CelkovaPoloha = 0;
}
//---------------------------------------------------------------------------



void __fastcall TForm1::Button5Click(TObject *Sender)
{
int i,j,cisel,xa;
AnsiString M;
String znakA="";
xa = StrToInt( Form1 -> Memo1 -> Lines -> Count);       //pocet radku
float data[2000][2];
int x, y;
  // načtení hodnot z mema do matice
  int n = 0;
   for ( i=0 ; i < xa ; i++)
    {
     n = 0;
     M = Form1 -> Memo1 -> Lines -> Strings [i];                   // Do M řádky z Mema
     cisel = Form1 -> Memo1 -> Lines -> Strings [i].Length();     // Počet míst na řádku v Memu
       for ( j=2 ; j <=(cisel) ; j++)
         {
          if ( M[j]!=' ')
			 znakA = znakA + M[j];                           // Císlo + číslo dukud M se nerovna mezeře
          if ( M[j] == ' ')                                  // Pokud M se rovna mezeře
           {
             data[i][n] = (float)StrToFloat(znakA);        // Do GlPromMatA se uklada cislo
             n++;
             znakA = "";                                     // nuluj znak
           }
         }
     }

  // zjisteni polohy minim
   int m1,m2;
   float min1,max1;
   float valmin1,valmax1;
   float konst1=1000000;
   for (m1=0 ; m1<xa ; m1++)
      if (data[m1][1] < konst1)
       {
       min1 = data[m1][0];
       valmin1 = data[m1][1];
       konst1=data[m1][1];
       }

   float konst2=0.000001;
   for (m1=0 ; m1<xa ; m1++)
       if (data[m1][1]>konst2)
       {
       max1 = data[m1][0];
       valmax1 = data[m1][1];
       konst2=data[m1][1];
       }
       Form1 -> Label24 -> Caption = FloatToStrF(min1,ffFixed,5,1);
       Form1 -> Label23 -> Caption = FloatToStrF(max1,ffFixed,5,1);
       Form1 -> Label26 -> Caption = FloatToStrF(valmax1,ffFixed,5,1);
       Form1 -> Label28 -> Caption = FloatToStrF(valmin1,ffFixed,5,1);

       float PSV;
       PSV =  ( pow(10,(valmax1/20))/pow(10,(valmin1/20)) );
       Form1 -> Label30 -> Caption = FloatToStrF(PSV,ffFixed,10,2);

       float frekv = StrToFloat(Edit3->Text);
       float lambda = 300/frekv;
       float lam2 = lambda/2;
       float kalibr = StrToFloat(Edit1->Text);
       float z0 = (0.6-lam2+kalibr);
       float dz=0;
       //float dz1=0;
       float minimum = min1/100;

       if (z0 < minimum)
          {
           dz = (minimum-z0);
          }
       if (z0 >= minimum)
          {
           dz = (minimum-z0)-lam2;
          }


       float modRoz = (PSV-1)/(PSV+1);
       float fazeRoz = M_PI + (4*M_PI*dz)/lambda;
       Form1 -> Label35 -> Caption = FloatToStrF(modRoz,ffFixed,5,2);
       Form1 -> Label38 -> Caption = FloatToStrF(fazeRoz,ffFixed,5,2);


               
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button8Click(TObject *Sender)
{
// otevření zařízení
ftStatus = FT_OpenEx("FT232R USB UART",FT_OPEN_BY_DESCRIPTION,&ftHandle);
if (ftStatus == FT_OK) {
Edit4->Text = "OPENED";
}
else { Edit4->Text = "Don't OPENED!";}

// nastavení BitMode
ftStatus=FT_SetBitMode(ftHandle,0x17,0x01);	//u-mask, typ bitbang modu

// nastaveni baudrate
ftStatus = FT_SetBaudRate(ftHandle,115200);	//115200 symb/s
if (ftStatus != FT_OK) {
	Edit4->Text = "RECONNECT!";
};


data = 0x17;
ftStatus = FT_Write(ftHandle, &data, 1, &pocet);

}
//---------------------------------------------------------------------------

void __fastcall TForm1::StopMerClick(TObject *Sender)
{
Timer1->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormCreate(TObject *Sender)
{
	SG1->Cells[0][0] = "Vzdálenost [mm]";
	SG1->Cells[1][0] = "Napětí [dBuV]";
}
//---------------------------------------------------------------------------



void __fastcall TForm1::StartKalClick(TObject *Sender)
{
	DelkaLinky = StrToInt(Edit5->Text);
	DelkaTimer1->Enabled = false;
	data = 0x11;   //reset
	ftStatus = FT_Write(ftHandle, &data, 1, &pocet);
	data = 0x13;
	ftStatus = FT_Write(ftHandle, &data, 1, &pocet);
	KCelkovaPoloha = 0;
	Timer2->Enabled = true;
	
	}
//---------------------------------------------------------------------------

void __fastcall TForm1::StopKalClick(TObject *Sender)
{
Timer2->Enabled = false;
koeficient = (DelkaLinky/KCelkovaPoloha)*koeficient;
Hodnota->Text = koeficient;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
	short int kpoloha;
	float mmkpoloha;

	koeficient = -0.0508;
	kpoloha = komunikace();
	mmkpoloha = (koeficient * kpoloha);
	KCelkovaPoloha += mmkpoloha;
	Hodnota->Text = KCelkovaPoloha;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DelkaTimer(TObject *Sender)
{
DelkaTimer1->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DelkaTimer1Timer(TObject *Sender)
{
	if (CheckBox1->Checked) {
		Edit5->Enabled = true;
	}
	else Edit5->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Oaplikaci2Click(TObject *Sender)
{
Form2->ShowModal();
}
//---------------------------------------------------------------------------


