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

#include <vcl.h>
#include "Tserial_event.h"
#pragma hdrstop

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

void OnDataArrival(int size, char *buffer)
{
    if ((size>0) && (buffer!=0))
    {
        buffer[size] = 0;
        //printf("OnDataArrival: %s ",buffer);
    }
}

/* ======================================================== */
/* ===============  OnCharArrival     ===================== */
/* ======================================================== */
void SerialEventManager(uint32 object, uint32 event)
{
    char *buffer;
    int   size;
    Tserial_event *com;
    unsigned char txt[512];

    com = (Tserial_event *) object;
    if (com!=0)
    {
        switch(event)
        {
            case  SERIAL_CONNECTED  :

                                        Form1->text1->Caption= "Připojeno !";  //pro vypis textu v okne, promenna je text1
                                        break;
            case  SERIAL_DISCONNECTED  :
                                        Form1->text1->Caption="Odpojeno !";
                                        break;
            case  SERIAL_DATA_SENT  :
                                        Form1->text1->Caption="Data odeslána !";
                                        break;
            case  SERIAL_RING       :
                                        Form1->text1->Caption="DRING !";
                                        break;
            case  SERIAL_CD_ON      :
                                        Form1->text1->Caption="Carrier Detected !";
                                        break;
            case  SERIAL_CD_OFF     :
                                        Form1->text1->Caption="No more carrier !";
                                        break;
            case  SERIAL_DATA_ARRIVAL  :
                                        size   = com->getDataInSize();
                                        buffer = com->getDataInBuffer();
                                        if(size>511)size=511;

                                        memcpy(txt,buffer, size);
                                        txt[size]=0;

                                        Form1->Prijato->Caption="Přijato: "+IntToStr(txt[0]);
                                        OnDataArrival(size, buffer);
                                        com->dataHasBeenRead();
                                        break;
        }
    }
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)   //nekonecna smycka, ekviv. while
        : TForm(Owner)
{
        serial_port.setManager(SerialEventManager);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        serial_port.connect("COM1", 4800, SERIAL_PARITY_NONE, 8, false);
}
//---------------------------------------------------------------------------

/*void __fastcall TForm1::SentClick(TObject *Sender)
{
        unsigned char znak[1];
        znak[0]=7;
        serial_port.sendData(znak,1);    //odelse znak na RS232  
} */
//---------------------------------------------------------------------------
void __fastcall TForm1::NastavClick(TObject *Sender)
{
     unsigned char znak[1];
     znak[0]=StrToInt(Form1->Edit1->Text);
     serial_port.sendData(znak,1);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::SinusClick(TObject *Sender)  //Zmena na harmon. prubeh
{
        unsigned char znak[1];
        znak[0]=253;
        serial_port.sendData(znak,1);    //odelse znak na RS232
}
//---------------------------------------------------------------------------

void __fastcall TForm1::vystupClick(TObject *Sender)   //Sepnuti vystupu
{
        unsigned char znak[1];
        znak[0]=255;
        serial_port.sendData(znak,1);    //odelse znak na RS232
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioButton1Click(TObject *Sender)      //Zmena na trojuhelnikovy probeh
{
        unsigned char znak[1];
        znak[0]=254;
        serial_port.sendData(znak,1);
}
//---------------------------------------------------------------------------





