//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "main.h"
#include "MyThread.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFMain *FMain;

extern unsigned char rdbuff[];
extern int rdindex;
extern int errors;

unsigned char rtbuff[1000];
int rtindex=0;

HANDLE m_hCommPort;

MyThread *threadReception;

//---------------------------------------------------------------------------
__fastcall TFMain::TFMain(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnOpenCOMClick(TObject *Sender)
{  DCB dcbSerialParams ;
        m_hCommPort = CreateFile(cbSelectCOM->Text.c_str(),
            GENERIC_WRITE|GENERIC_READ,//access ( read and write)
            0,    //(share) 0:cannot share the COM port
            0,    //security  (None)
            OPEN_EXISTING,// creation : open_existing
            FILE_FLAG_OVERLAPPED,// we want overlapped operation
            0// no templates file for COM port...
            );

      if(m_hCommPort==NULL){
        ShowMessage("can not open port");
        return;
      }
      if(m_hCommPort==INVALID_HANDLE_VALUE) {
        ShowMessage("invalid handle value");
        return;
      }

      if (!GetCommState(m_hCommPort, &dcbSerialParams))
      {
           ShowMessage("error getting serial port state");
      }

      dcbSerialParams.DCBlength = sizeof(dcbSerialParams);

      dcbSerialParams.BaudRate = CBR_19200;
      dcbSerialParams.ByteSize = 8;
      dcbSerialParams.StopBits = ONESTOPBIT;
      dcbSerialParams.Parity   = NOPARITY;

      dcbSerialParams.fBinary  = TRUE;

      if (!SetCommState(m_hCommPort, &dcbSerialParams))
      {
            ShowMessage(" error setting serial port state");
      }

      lHCom->Caption = (int)m_hCommPort;

      if(threadReception == NULL)
      	threadReception = new MyThread(false);
      else
	threadReception->Resume();
      btnOpenCOM->Enabled = false;
      btnCloseCOM->Enabled = true;
      eValueA->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnShowErrClick(TObject *Sender)
{
        lErrors->Caption = errors;
}
//---------------------------------------------------------------------------

void __fastcall TFMain::FormClose(TObject *Sender, TCloseAction &Action)
{
        CloseHandle(m_hCommPort);
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnSendTextClick(TObject *Sender)
{
DWORD nr=0;
OVERLAPPED ovlw;

          ovlw.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
          if(!WriteFile(m_hCommPort, rtbuff, rtindex, &nr, &ovlw))
              if(GetLastError()!= ERROR_IO_PENDING)
                ShowMessage("WriteText: "+IntToStr(GetLastError()));
          eValueA->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnCloseCOMClick(TObject *Sender)
{
        CloseHandle(m_hCommPort);
        m_hCommPort = (void*)-1;
	threadReception->Suspend();
        lHCom->Caption = "closed";
        btnOpenCOM->Enabled = true;
        btnCloseCOM->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnDeleteRClick(TObject *Sender)
{
        eReception->Text    = "";
        eHexReception->Text = "";
        rdindex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnDeleteTClick(TObject *Sender)
{
        eSendA->Text = "";
        eSendH->Text = "";
        rtindex = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnAddAClick(TObject *Sender)
{
     for(int k=1;k <= eValueA->Text.Length();k++)
        {
        rtbuff[rtindex] = eValueA->Text[k];
        FMain->eSendA->Text = FMain->eSendA->Text +    (char)rtbuff[rtindex];
        FMain->eSendH->Text = FMain->eSendH->Text + IntToHex(rtbuff[rtindex],2)+" ";
        rtindex++;
        }
}
//---------------------------------------------------------------------------

void __fastcall TFMain::btnAddHClick(TObject *Sender)
{
int x;
char v;
        eValueH->Text.LowerCase();
        if( (x = HexToBin(eValueH->Text.c_str(), &v, 1))!=1)
                                {ShowMessage("conversion error" + IntToStr(x)); return;}
        rtbuff[rtindex] = v;
        FMain->eSendA->Text = FMain->eSendA->Text +    (char)rtbuff[rtindex];
        FMain->eSendH->Text = FMain->eSendH->Text + IntToHex(rtbuff[rtindex],2)+" ";
        rtindex++;
}
//---------------------------------------------------------------------------

