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

#include <vcl.h>
#pragma hdrstop

#include "MyThread.h"
#include "main.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall Unit2::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

extern HANDLE m_hCommPort;
unsigned char rdbuff[10000];
int rdindex=0;
int errors=0;

__fastcall MyThread::MyThread(bool CreateSuspended)
        : TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall MyThread::Execute()
{
unsigned long nrbytesread=0;
unsigned char nextcar;
OVERLAPPED ovlr;

        //---- Place thread code here ----

        rdindex=0;
        ovlr.hEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
        while(1)
        {
                nrbytesread=0;

                if(ReadFile(m_hCommPort, &nextcar, 1, &nrbytesread, &ovlr) == 0) // fails
                     { // fails
                     if( GetLastError() == ERROR_IO_PENDING)
                         GetOverlappedResult(m_hCommPort, &ovlr, &nrbytesread, TRUE);
                     else {errors++;continue;} //errors
                     }

                rdbuff[rdindex] = nextcar;
                rdindex += nrbytesread;
                Synchronize(UpdateCaption);
        }// while
}
//---------------------------------------------------------------------------

void __fastcall MyThread::UpdateCaption()
{
   FMain->lReceptioned->Caption = rdindex;

   FMain->eHexReception->Text = "";
   FMain->eReception->Text    = "";
   for(int k=0;k<rdindex ;k++)
      {
      FMain->eReception->Text    = FMain->eReception->Text    +    (char)rdbuff[k];
      FMain->eHexReception->Text = FMain->eHexReception->Text + IntToHex(rdbuff[k],2)+" ";
      }
}

