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

#include <vcl.h>
#include <stdio.h>
#include <string.h>
#include "Serial.h"
#pragma hdrstop

#include "RFIDpanel.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

unsigned char data_in[50];
unsigned char data_out[50];
unsigned char pocet_in=0;
bool data_valid = 0;
bool cas_valid = 0;
int count = 0;

typedef struct tcas
{
  unsigned char prichod[2];
  unsigned char odchod[2];
}cas;

typedef struct tuser
{
  char jmeno[20];
  char prijmeni[20];
  unsigned char IDN[5];
  tcas po;
  tcas ut;
  tcas st;
  tcas ct;
  tcas pa;
  tcas so;
  tcas ne;
}user;

tuser pole_usr[128];
unsigned char pocet_usr=0;

//---------------------------------------------------------------------------
void __fastcall TForm1:: zobraz(void)   //zobrazeni dat uzivatelu do pole
{
  int i,j;
  char pom_IDN[6];

  for(i=0;i<pocet_usr;i++)
    {
    StringGrid1->Cells[0][i+1]=i+1;     //zobrazeni poradi

    StringGrid1->Cells[1][i+1] ="";     //zobrazeni jmena
    j=1;
    while(pole_usr[i].jmeno[j]!='\0')//for (j=0;j<10;j++)
      {
      StringGrid1->Cells[1][i+1] = StringGrid1->Cells[1][i+1] + (pole_usr[i].jmeno[j]);
      j++;
      }
    StringGrid1->Cells[2][i+1] ="";     //zobrazeni prijmeni
    j=1;
    while(pole_usr[i].prijmeni[j]!='\0')//for (j=0;j<10;j++)
      {
      StringGrid1->Cells[2][i+1] = StringGrid1->Cells[2][i+1] + pole_usr[i].prijmeni[j];
      j++;
      }
//    StringGrid1->Cells[1][i+1]=pole_usr[i].jmeno;
//    StringGrid1->Cells[2][i+1]=pole_usr[i].prijmeni;

    StringGrid1->Cells[3][i+1] ="";
    for (j=0;j<5;j++)
      StringGrid1->Cells[3][i+1] = StringGrid1->Cells[3][i+1] + IntToHex((pole_usr[i].IDN[j]&0xFF),2);//IntToHex(pom_IDN[4],1)+IntToHex((int)*((int*)(&pom_IDN[0])),8); //vypis pristupu

//    StringGrid1->Cells[4][i+1]=pole_usr[i].po.prichod[0]+':'+pole_usr[i].po.prichod[1];//
//    StringGrid1->Cells[5][i+1]=pole_usr[i].po.odchod[0]+':'+pole_usr[i].po.odchod[1];

    StringGrid1->Cells[4][i+1]=IntToHex(pole_usr[i].po.prichod[0],2)+':'+IntToHex(pole_usr[i].po.prichod[1],2);//
    StringGrid1->Cells[5][i+1]=IntToHex(pole_usr[i].po.odchod[0],2)+':'+IntToHex(pole_usr[i].po.odchod[1],2);

    StringGrid1->Cells[6][i+1]=IntToHex(pole_usr[i].ut.prichod[0],2)+':'+IntToHex(pole_usr[i].ut.prichod[1],2);//
    StringGrid1->Cells[7][i+1]=IntToHex(pole_usr[i].ut.odchod[0],2)+':'+IntToHex(pole_usr[i].ut.odchod[1],2);

    StringGrid1->Cells[8][i+1]=IntToHex(pole_usr[i].st.prichod[0],2)+':'+IntToHex(pole_usr[i].st.prichod[1],2);//
    StringGrid1->Cells[9][i+1]=IntToHex(pole_usr[i].st.odchod[0],2)+':'+IntToHex(pole_usr[i].st.odchod[1],2);

    StringGrid1->Cells[10][i+1]=IntToHex(pole_usr[i].ct.prichod[0],2)+':'+IntToHex(pole_usr[i].ct.prichod[1],2);//
    StringGrid1->Cells[11][i+1]=IntToHex(pole_usr[i].ct.odchod[0],2)+':'+IntToHex(pole_usr[i].ct.odchod[1],2);

    StringGrid1->Cells[12][i+1]=IntToHex(pole_usr[i].pa.prichod[0],2)+':'+IntToHex(pole_usr[i].pa.prichod[1],2);//
    StringGrid1->Cells[13][i+1]=IntToHex(pole_usr[i].pa.odchod[0],2)+':'+IntToHex(pole_usr[i].pa.odchod[1],2);

    StringGrid1->Cells[14][i+1]=IntToHex(pole_usr[i].so.prichod[0],2)+':'+IntToHex(pole_usr[i].so.prichod[1],2);//
    StringGrid1->Cells[15][i+1]=IntToHex(pole_usr[i].so.odchod[0],2)+':'+IntToHex(pole_usr[i].so.odchod[1],2);

    StringGrid1->Cells[16][i+1]=IntToHex(pole_usr[i].ne.prichod[0],2)+':'+IntToHex(pole_usr[i].ne.prichod[1],2);//
    StringGrid1->Cells[17][i+1]=IntToHex(pole_usr[i].ne.odchod[0],2)+':'+IntToHex(pole_usr[i].ne.odchod[1],2);

    }
    i++;
  for(i;i<128;i++)
    {
    for(j=0;j<18;j++)
      StringGrid1->Cells[j][i]="";
    }


    if(pocet_usr < 1)
      ButtonOdstran->Enabled=False;
    if(pocet_usr < 128)
      ButtonUloz->Enabled=True;
    if(pocet_usr > 0)
      ButtonOdstran->Enabled=True;
    if(pocet_usr > 127)
      ButtonUloz->Enabled=False;

}

//---------------------------------------------------------------------------
int verify_check(void)          //overeni paketu
{
int i;
unsigned char check;

  check=0;
  for(i=0;i<(data_in[1]-1);i++)
       check=check ^ (data_in[i] & 0xFF);

  if (check == 0) {// pokud vypocteny check v in_bufferu=0 - data jsou v poradku
      if (data_in[data_in[1]-1] == 0xF0)   // kontrola jestli je na konci paketu ukoncovaci byte
         return 1;
      else
         return 0;
      }
  else
      return 0;
}

//---------------------------------------------------------------------------
void __fastcall TForm1:: posli_paket(int pocet)         //odeslani paketu
{
  int i;
  int check;

Serial->WriteByte(170);        //start byte
  check=170;
Serial->WriteByte(pocet+4);
  check=check ^ (pocet+4);

	for(i=0;i<pocet;i++)
	{
		Serial->WriteByte(data_out[i]);                // odeslani dat
		check=check ^ data_out[i];
	}

Serial->WriteByte(check);       //stop byte
Serial->WriteByte(0xF0);
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
        f_ini = fopen("serial.ini", "r");
        if(f_ini != NULL){
          fscanf(f_ini, "PORT = [%d]", &Port);
          fclose(f_ini);
        }

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

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  int i;

  if(i>3)
  {
  if(stav == 0)
    Labelstav->Caption = "OK";
  if(stav == 1)
    Labelstav->Caption = "odesilam data";
  if(stav == 2)
    Labelstav->Caption = "prijimam data";
  if(stav == 4)
    {
    zobraz();
    stav = 0;
    }
   i=1;
   }
}

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


void __fastcall TForm1::ButtonIDClick(TObject *Sender)  //nacteni IDN z modulu
{
  int i;
  char znaky[5];
  char pom_IDN[5];

  data_out[0] = 0x10;   //odeslani prikazu
  posli_paket(1);
  data_valid = 0;
  pocet_in=0;
  for(i=0;i<50;i++)
        data_in[i]=0;

  while(data_valid == 0)        //cteni dat
    {
      Serial->ReadChar(&data_in[pocet_in]);
        pocet_in++;
          if((data_in[0]) == (170))
            {
            if(data_in[1] == pocet_in)
              {
               if(verify_check)
                 {
                   pocet_in=pocet_in-4;	//orezani kontrolnich bytu prenosu
                   for(i=0;i<(pocet_in);i++)
                     data_in[i]=data_in[i+2];

                   if(data_in[i]==0xE8)
                     Labelstav->Caption = "Chyba prenosu";
                   data_valid=1;
                 }
               else
                 MessageBox(Handle,"Cteni selhalo","Komunikace",MB_ICONHAND);

              }

            }
          else
            {
            pocet_in=0;
            }

    }


  LabelIDN->Caption ="";
  for(i=0;i<5;i++)
    LabelIDN->Caption = LabelIDN->Caption + IntToHex((data_in[i]&0xFF),2);//data_in[i];

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

void __fastcall TForm1::ButtonUlozClick(TObject *Sender)  //ulozeni uzivatele
{
  int i;
  unsigned char IDN[5];
  AnsiString jm, prijm;

  jm = EditJm->Text;
  prijm = EditPrijm->Text;

  for(i=1;i<=jm.Length();i++)                    //ulozeni jmena a prjmeni z editu
    pole_usr[pocet_usr].jmeno[i] = jm[i] ;
  for(i=1;i<=prijm.Length();i++)
    pole_usr[pocet_usr].prijmeni[i] = prijm[i];

  for(i=0;i<5;i++)                                // cteni IDN
    pole_usr[pocet_usr].IDN[i]=data_in[i];

  LabelIDN->Caption ="0";
  EditJm->Text="";
  EditPrijm->Text="";
  pocet_usr++;
  zobraz();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
   StringGrid1->Cells[0][0]="Číslo";
   StringGrid1->Cells[1][0]="Jméno";    //prednastaveni pole
   StringGrid1->Cells[2][0]="Příjmení";
   StringGrid1->Cells[3][0]="ID čipu";
   StringGrid1->Cells[4][0]="Po  příchod";
   StringGrid1->Cells[5][0]="Po  odchod";
   StringGrid1->Cells[6][0]="Út  příchod";
   StringGrid1->Cells[7][0]="Út  odchod";
   StringGrid1->Cells[8][0]="St  příchod";
   StringGrid1->Cells[9][0]="St  odchod";
   StringGrid1->Cells[10][0]="Čt  příchod";
   StringGrid1->Cells[11][0]="Čt  odchod";
   StringGrid1->Cells[12][0]="Pá  příchod";
   StringGrid1->Cells[13][0]="Pá  odchod";
   StringGrid1->Cells[14][0]="So  příchod";
   StringGrid1->Cells[15][0]="So  odchod";
   StringGrid1->Cells[16][0]="Ne  příchod";
   StringGrid1->Cells[17][0]="Ne  odchod";

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



void __fastcall TForm1::ButtonOdstranClick(TObject *Sender)  //odstraneni uzivatele
{
  int i;
  int pom,pocet_cyklu;

  pom=(StrToInt(EditOdstran->Text)-1);  //prepsani odstraneneho uzivatele
  if((pom>=0) && (pom<129))
    {
    if(pom<pocet_usr)
      {
      if(pocet_usr<127)
        {
        for(i=pom;i<pocet_usr;i++)
          pole_usr[i]=pole_usr[i+1];
        }
      pocet_usr--;
        if(pocet_usr < 1)
          ButtonOdstran->Enabled=False;
        if(pocet_usr < 128)
          ButtonUloz->Enabled=True;
      }
     }
   else
     MessageBox(Handle,"Císlo musí být 1 až 128","Editace",MB_ICONHAND);
  zobraz();

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


void __fastcall TForm1::Odelipoledomodulu1Click(TObject *Sender)  //odeslani dat do pameti modulu
{
  int i,j=0;
  char znaky[5];

//  Label6->Caption="odesilam";

/*
  data_out[0] = 0x50; //prikaz mazani pameti
  posli_paket(1);
  data_in[0]=0;
        while(data_in[0]!=0xCC)         //cekani na odpoved
          {
            Serial->ReadChar(&data_in[0]);
          }
*/

  data_out[0] = 0;   //nulovani poctu uzivatelu v pameti
  data_out[1] = 0x40;

  posli_paket(2);
  data_in[0]=0;
    while(data_in[0]!=0xCC)         //cekani na odpoved
      {
        Serial->ReadChar(&data_in[0]);
      }


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

       for(j=0;j<5;j++)                  //odeslani jednoho uzivatele
        data_out[j]=pole_usr[i].IDN[j];

      data_out[j] = pole_usr[i].po.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].po.prichod[1];   //# PONDELI
      j++;
      data_out[j] = pole_usr[i].po.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].po.odchod[1];    //#

      j++;
      data_out[j] = pole_usr[i].ut.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].ut.prichod[1];   //# UTERY
      j++;
      data_out[j] = pole_usr[i].ut.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].ut.odchod[1];    //#

      j++;
      data_out[j] = pole_usr[i].st.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].st.prichod[1];   //# STREDA
      j++;
      data_out[j] = pole_usr[i].st.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].st.odchod[1];    //#

      j++;
      data_out[j] = pole_usr[i].ct.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].ct.prichod[1];   //# CTVRTEK
      j++;
      data_out[j] = pole_usr[i].ct.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].ct.odchod[1];    //#

      j++;
      data_out[j] = pole_usr[i].pa.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].pa.prichod[1];   //# PATEK
      j++;
      data_out[j] = pole_usr[i].pa.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].pa.odchod[1];    //#

      j++;
      data_out[j] = pole_usr[i].so.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].so.prichod[1];   //# SOBOTA
      j++;
      data_out[j] = pole_usr[i].so.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].so.odchod[1];    //#
      

      j++;
      data_out[j] = pole_usr[i].ne.prichod[0];   //# odeslani casu jednoho dne
      j++;
      data_out[j] = pole_usr[i].ne.prichod[1];   //# NEDELE
      j++;
      data_out[j] = pole_usr[i].ne.odchod[0];    //#
      j++;
      data_out[j] = pole_usr[i].ne.odchod[1];    //#
      j++;


      data_out[j] = 0x20;               //prikaz, na konec dat


      posli_paket(j+1);                 //konec odesl jednoho uziv
      data_in[0]=0;
        while(data_in[0]!=0xCC)         //cekani na odpoved
          {
            Serial->ReadChar(&data_in[0]);
          }
     }
  Labelstav->Caption = "Odeslano";
//  Timer1->Enabled = True;
//  Label8->Caption="OK";
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Natipolezmodulu1Click(TObject *Sender)//cteni dat z pam. modulu
{
  int i,j,k;
  bool cteni,hledej,valid;

  i=0;
  data_out[0] = 0x30;   //odeslani prikazu-cti pamet
  posli_paket(1);

  cteni = 1;
  while(cteni)
    {
      pocet_in=0;
      data_valid=0;

      while(data_valid == 0)        //cteni odpovedi
        {
          Serial->ReadChar(&data_in[pocet_in]);
          pocet_in++;
            if((data_in[0]) == (170))
              {
              if(data_in[1] == pocet_in)
                {
                 if(verify_check)
                   {
                     pocet_in=pocet_in-4;	//orezani kontrolnich bytu prenosu
                     for(j=0;j<(pocet_in);j++)
                       data_in[j]=data_in[j+2];

                       if(data_in[i]==0xE8)
                         Labelstav->Caption = "Chyba prenosu ";
                     data_valid=1;
                   }
                 else
                   MessageBox(Handle,"Cteni selhalo","Komunikace",MB_ICONHAND);

                }
              }
            else
              {
              pocet_in=0;
              }

        }
      if((data_in[0]==0xFA)&&(pocet_in==1))
        cteni=0;
      else
        {
         hledej=1;
         k=0;
         while(hledej)
           {
             valid=1;
             for(j=0;j<5;j++)                  //porovnani IDN
               {
               if(pole_usr[i+k].IDN[j] != data_in[j])
                 valid &= 0;
               }
             if(valid)                         //pokud shodna, uloz cas
               {
                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].po.prichod[j] = data_in[j+5];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].po.odchod[j] = data_in[j+7];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].ut.prichod[j] = data_in[j+9];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].ut.odchod[j] = data_in[j+11];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].st.prichod[j] = data_in[j+13];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].st.odchod[j] = data_in[j+15];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].ct.prichod[j] = data_in[j+17];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].ct.odchod[j] = data_in[j+19];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].pa.prichod[j] = data_in[j+21];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].pa.odchod[j] = data_in[j+23];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].so.prichod[j] = data_in[j+25];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].so.odchod[j] = data_in[j+27];

                 for(j=0;j<2;j++)                              //ulozeni casu
                   pole_usr[i+k].ne.prichod[j] = data_in[j+29];
                 for(j=0;j<2;j++)
                   pole_usr[i+k].ne.odchod[j] = data_in[j+31];


                 hledej=0;
               }
             k++;
               if((i+k) > (pocet_usr-1)) //err!!!
                 {
                 hledej=0;
                 Labelstav->Caption = "Nenalezeno IDN";
                 }
             //pole_usr[i+k].prava = data_in[5];       //ulozeni prav    !!!
           }
          data_out[0] = 0xC7;   //odeslani prikazu-cti pamet dale
          posli_paket(1);

          i++;
        }

    }
  Labelstav->Caption = "Nacteno";
//  Timer1->Enabled = True;
  //pocet_usr=i;
  //pocet_usr++;
  zobraz();



}
//---------------------------------------------------------------------------
void __fastcall TForm1::Synchronizujhodiny1Click(TObject *Sender)  //odeslani casu do modulu
{
  int i;
  unsigned char div,jednsec,jednmin,jednhod,jednden,jednmes;
  unsigned char pom[10];
  TDateTime Cas1,Datum,DenVtydnu;
  unsigned short Hodina,Minuta,Sekunda,MSekunda,Rok,Den,Mesic,Dvt;

  Datum=Cas1. CurrentDate();    //nacteni systemoveho data
  Cas1=Cas1. CurrentTime();     //nacteni systemoveho casu
  DenVtydnu=DayOfWeek(Cas1 );   //nacteni dne v tydnu
  DecodeDateFully(Datum,Rok,Mesic,Den,Dvt);     //dekodovani na jednotlive hodnoty
  DecodeTime(Cas1,Hodina,Minuta,Sekunda,MSekunda);
  stav=1;

  div = Sekunda / 10;
  jednsec = Sekunda - div * 10;
  pom[0] = ((div<<4)&0xF0)|(jednsec&0x0F);      //sekundy do bcd

  div = Minuta / 10;
  jednmin = Minuta - div * 10;
  pom[1] = ((div<<4)&0xF0)|(jednmin&0x0F);      //minuty do bcd

  div = Hodina / 10;
  jednhod = Hodina - div * 10;
  pom[2] = ((div<<4)&0x30)|(jednhod&0x0F);      //hodiny do bcd

  div = Den / 10;
  jednden = Den - div * 10;
  pom[3] = ((div<<4)&0x30)|(jednden&0x0F);      //den do bcd

  div = Mesic / 10;
  jednmes = Mesic - div * 10;
  pom[4] = ((div<<4)&0x10)|(jednmes&0x0F);      //den v tydnu a mesic do bcd

    if(Dvt == 1)
      {              //prevod na po = 0 az ne = 6
      Dvt = 6;
      }
    else
      {
      Dvt = Dvt-2;
      }
  pom[4] = pom[4] | (((Dvt)<<5)&0xE0);

  pom[5] = 0x60; //prikaz

  for(i=0;i<6;i++)
    data_out[i]=pom[i];

  posli_paket(6); //odeslani
  data_in[0] = 0;
        while(data_in[0]!=0xCC)         //cekani na odpoved
          {
            Serial->ReadChar(&data_in[0]);
          }
  stav=0;
  Labelstav->Caption = "Nastaveno";

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

void __fastcall TForm1::FormActivate(TObject *Sender)
{
        f_ini = fopen("serial.ini", "r");       //nacteni c. portu z ini souboru
        if(f_ini != NULL){
          fscanf(f_ini, "PORT = [%d]", &Port);
          fclose(f_ini);
        }
        if(Port == 1)
          COM11->Checked = true;
        if(Port == 2)
          COM21->Checked = true;
        if(Port == 3)
          COM31->Checked = true;
        if(Port == 4)
          COM41->Checked = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        f_ini = fopen("serial.ini", "w");       //ulozeni c. portu do ini souboru
        fprintf(f_ini, "PORT = [%d]", Port);
        fclose(f_ini);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::COM11Click(TObject *Sender)
{
  if (Port != 0)                // nastaveni c. 1 po kliknuti na COM1
    {
    delete(Serial);
    Port=0;
    }
        Port = 1;
        COM11->Checked = true;
        COM21->Checked = false;
        COM31->Checked = false;
        COM41->Checked = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::COM21Click(TObject *Sender)
{
  if (Port != 0)                // nastaveni c. 2 po kliknuti na COM2
    {
    delete(Serial);
    Port=0;
    }
        Port = 2;
        COM11->Checked = false;
        COM21->Checked = true;
        COM31->Checked = false;
        COM41->Checked = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::COM31Click(TObject *Sender)
{
  if (Port != 0)                // nastaveni c. 3 po kliknuti na COM3
    {
    delete(Serial);
    Port=0;
    }
        Port = 3;
        COM11->Checked = false;
        COM21->Checked = false;
        COM31->Checked = true;
        COM41->Checked = false;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::COM41Click(TObject *Sender)
{
  if (Port != 0)                // nastaveni c. 4 po kliknuti na COM4 
    {
    delete(Serial);
    Port=0;
    }
        Port = 4;
        COM11->Checked = false;
        COM21->Checked = false;
        COM31->Checked = false;
        COM41->Checked = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Pipoj1Click(TObject *Sender)   //pripojeni seriove linky
{

    Serial = new TSerial(Port);
    Serial->BaudRate = br9600;
    Serial->Parity = pNo;
    Serial->StopBits = sb10;
    Serial->ByteSize = bs8;
    Serial->SetupComm(16000, 50);
    Serial->ReadIntervalTimeout = 0;
    Serial->ReadTotalTimeoutConstant = 800;
    Serial->ReadTotalTimeoutMultiplier = 0;
    Serial->WriteTotalTimeoutConstant = 800;
    Serial->WriteTotalTimeoutMultiplier = 2;
//    Serial->CTS= 1;

    LabelKom->Caption = "připojeno";
    Editace1->Enabled = True;
    ButtonID->Enabled = True;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Odpoj1Click(TObject *Sender)//ukonceni komunikace
{
  data_out[0] = 0xA0;   //odeslani prikazu konec komunikace
  posli_paket(1);


  if (Port != 0)
    {
    delete(Serial);
    Port=0;
    }


    LabelKom->Caption = "odpojeno";
    Editace1->Enabled = False;
    ButtonID->Enabled = False;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StringGrid1SetEditText(TObject *Sender, int ACol,
      int ARow, const AnsiString Value) //editace hodnot v poli
{
  unsigned char cisla[10];
  unsigned int pocet;
  AnsiString hodnota;

  hodnota = Value;
  pocet = hodnota.Length();     //zjisteni delky zapsaneho retezce

//  Label2->Caption = pocet;

  if((ACol>3) && (ARow>0) && (ARow<(pocet_usr+1)))      //pokud se nachazim v editaci casu
    {
     if(pocet > 4)                                      //a je zapsan dostatek znaku
       {
       cisla[0] = (Value[1]<<4)|(Value[2]&0x0F);        // prevedu na format casu
       cisla[1] = (Value[4]<<4)|(Value[5]&0x0F);

         if((Value[3]==':')&&(cisla[0]<0x24)&&(cisla[1]<0x60))  //kontrola rozsahu dat
           {
             if((ACol-4) == 0)
               {
               pole_usr[ARow-1].po.prichod[0] = cisla[0];
               pole_usr[ARow-1].po.prichod[1] = cisla[1];
               }
             if((ACol-4) == 1)
               {
               pole_usr[ARow-1].po.odchod[0] = cisla[0];
               pole_usr[ARow-1].po.odchod[1] = cisla[1];
               }
             if((ACol-4) == 2)
               {
               pole_usr[ARow-1].ut.prichod[0] = cisla[0];
               pole_usr[ARow-1].ut.prichod[1] = cisla[1];
               }
             if((ACol-4) == 3)
               {
               pole_usr[ARow-1].ut.odchod[0] = cisla[0];
               pole_usr[ARow-1].ut.odchod[1] = cisla[1];
               }
             if((ACol-4) == 4)
               {
               pole_usr[ARow-1].st.prichod[0] = cisla[0];
               pole_usr[ARow-1].st.prichod[1] = cisla[1];
               }
             if((ACol-4) == 5)
               {
               pole_usr[ARow-1].st.odchod[0] = cisla[0];
               pole_usr[ARow-1].st.odchod[1] = cisla[1];
               }
             if((ACol-4) == 6)
               {
               pole_usr[ARow-1].ct.prichod[0] = cisla[0];
               pole_usr[ARow-1].ct.prichod[1] = cisla[1];
               }
             if((ACol-4) == 7)
               {
               pole_usr[ARow-1].ct.odchod[0] = cisla[0];
               pole_usr[ARow-1].ct.odchod[1] = cisla[1];
               }
             if((ACol-4) == 8)
               {
               pole_usr[ARow-1].pa.prichod[0] = cisla[0];
               pole_usr[ARow-1].pa.prichod[1] = cisla[1];
               }
             if((ACol-4) == 9)
               {
               pole_usr[ARow-1].pa.odchod[0] = cisla[0];
               pole_usr[ARow-1].pa.odchod[1] = cisla[1];
               }
             if((ACol-4) == 10)
               {
               pole_usr[ARow-1].so.prichod[0] = cisla[0];
               pole_usr[ARow-1].so.prichod[1] = cisla[1];
               }
             if((ACol-4) == 11)
               {
               pole_usr[ARow-1].so.odchod[0] = cisla[0];
               pole_usr[ARow-1].so.odchod[1] = cisla[1];
               }
             if((ACol-4) == 12)
               {
               pole_usr[ARow-1].ne.prichod[0] = cisla[0];
               pole_usr[ARow-1].ne.prichod[1] = cisla[1];
               }
             if((ACol-4) == 13)
               {
               pole_usr[ARow-1].ne.odchod[0] = cisla[0];
               pole_usr[ARow-1].ne.odchod[1] = cisla[1];
               }

           }
         else
           {
           MessageBox(Handle,"Zadejte čas ve správném formátu!","Editace časů",MB_ICONHAND);
           }

        zobraz();
       }
     }
}

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

void __fastcall TForm1::StringGrid1GetEditText(TObject *Sender, int ACol,
      int ARow, AnsiString &Value)      //editace hodnot v poli
{
  unsigned char cisla[2];
  unsigned int den;

  if((ACol>3) && (ARow>0) && (ARow<(pocet_usr+1)))
    {

     if((Value[3]==':'))
       {
       cisla[0] = (Value[1]<<4)|(Value[2]&0x0F);
       cisla[1] = (Value[4]<<4)|(Value[5]&0x0F);

         if((cisla[0]<0x24)&&(cisla[1]<0x60))
           {
             //Label1->Caption=IntToHex(cisla[0],2);
             if((ACol-4) == 0)
               {
               pole_usr[ARow-1].po.prichod[0] = cisla[0];
               pole_usr[ARow-1].po.prichod[1] = cisla[1];
               }
             if((ACol-4) == 1)
               {
               pole_usr[ARow-1].po.odchod[0] = cisla[0];
               pole_usr[ARow-1].po.odchod[1] = cisla[1];
               }
             if((ACol-4) == 2)
               {
               pole_usr[ARow-1].ut.prichod[0] = cisla[0];
               pole_usr[ARow-1].ut.prichod[1] = cisla[1];
               }
             if((ACol-4) == 3)
               {
               pole_usr[ARow-1].ut.odchod[0] = cisla[0];
               pole_usr[ARow-1].ut.odchod[1] = cisla[1];
               }
             if((ACol-4) == 4)
               {
               pole_usr[ARow-1].st.prichod[0] = cisla[0];
               pole_usr[ARow-1].st.prichod[1] = cisla[1];
               }
             if((ACol-4) == 5)
               {
               pole_usr[ARow-1].st.odchod[0] = cisla[0];
               pole_usr[ARow-1].st.odchod[1] = cisla[1];
               }
             if((ACol-4) == 6)
               {
               pole_usr[ARow-1].ct.prichod[0] = cisla[0];
               pole_usr[ARow-1].ct.prichod[1] = cisla[1];
               }
             if((ACol-4) == 7)
               {
               pole_usr[ARow-1].ct.odchod[0] = cisla[0];
               pole_usr[ARow-1].ct.odchod[1] = cisla[1];
               }
             if((ACol-4) == 8)
               {
               pole_usr[ARow-1].pa.prichod[0] = cisla[0];
               pole_usr[ARow-1].pa.prichod[1] = cisla[1];
               }
             if((ACol-4) == 9)
               {
               pole_usr[ARow-1].pa.odchod[0] = cisla[0];
               pole_usr[ARow-1].pa.odchod[1] = cisla[1];
               }
             if((ACol-4) == 10)
               {
               pole_usr[ARow-1].so.prichod[0] = cisla[0];
               pole_usr[ARow-1].so.prichod[1] = cisla[1];
               }
             if((ACol-4) == 11)
               {
               pole_usr[ARow-1].so.odchod[0] = cisla[0];
               pole_usr[ARow-1].so.odchod[1] = cisla[1];
               }
             if((ACol-4) == 12)
               {
               pole_usr[ARow-1].ne.prichod[0] = cisla[0];
               pole_usr[ARow-1].ne.prichod[1] = cisla[1];
               }
             if((ACol-4) == 13)
               {
               pole_usr[ARow-1].ne.odchod[0] = cisla[0];
               pole_usr[ARow-1].ne.odchod[1] = cisla[1];
               }

            }
         else
           {
           MessageBox(Handle,"Zadejte čas ve správném formátu!","Editace časů",MB_ICONHAND);
           //Value = "00:00";
           }



       }
     }
//  strlength(Value);
//  Label1->Caption=Value;
//  Label2->Caption=Value[5];
// zobraz();
stav=4;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Ulo1Click(TObject *Sender)      //ulozeni do souboru
{
  int zapsane;

  if (SaveDialog1->Execute())
    {
    AnsiString cesta = SaveDialog1->FileName;

    FILE *soubor;
      if ((soubor = fopen(SaveDialog1->FileName.c_str(), "wb")) == NULL)        //otevreni souboru
        Labelstav->Caption = "soubor se nepodařilo otevřít";
      else
        {
        zapsane = fwrite(&pole_usr, sizeof(struct tuser), pocet_usr, soubor);   //zapis dat
          if (zapsane == pocet_usr)
            Labelstav->Caption = "OK";
          else
            Labelstav->Caption = "chybný zápis dat";

        fclose(soubor);
        }
    }
}

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

void __fastcall TForm1::Otevi1Click(TObject *Sender)    //otevreni dat ze souboru
{
  int zapsane,i=0;

  if (OpenDialog1->Execute())
    {
    AnsiString cesta = OpenDialog1->FileName;  //precteni nazvu souboru

    FILE *soubor;
      if ((soubor = fopen(OpenDialog1->FileName.c_str(), "rb")) == NULL)        //otevreni souboru
        Labelstav->Caption = "soubor se nepodařilo otevřít";
    else
      {
      do
        {
        zapsane = fread(&pole_usr[i], sizeof(struct tuser), 1, soubor);         //cteni dat ze souboru
          if (zapsane == sizeof(struct tuser))
            Labelstav->Caption = "OK";
          else
            if(feof(soubor))
              break;

        i++;
        } while (i<127);

      pocet_usr = i;
      }
    fclose(soubor);
    zobraz();
    }
}

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

void __fastcall TForm1::StringGrid1Click(TObject *Sender)       //aktualizace pole
{
  zobraz();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol,
      int ARow, bool &CanSelect)        //cteni cisla radku pro odstraneni uzivatele
{
  EditOdstran->Text = IntToStr(ARow);      
}
//---------------------------------------------------------------------------

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

