1. Jalankan C++ Builder dan mulailah dengan Start New Project
2. Tambahkan komponen Memo pada Form1 yang tersedia. Setting properties dari Memo1 sesuai dengan keinginan anda.
3. Pada menu File->New, tambahkan object Thread. Gunakan nama class TRead.
4. Jika langkah yang anda lakukan benar dapat anda lihat 2 buah file unit, yaitu: Unit1.cpp untuk Form1 dan Unit2.cpp untuk Thread.
5. Pada event handlers lakukan setting: Form1->OnCreate; Form1->OnClose; Memo1->OnKeyPress
Pada Main Form, edit listing program menjadi seperti berikut :
//---------------------------------------------------------------------------
'#include
'#pragma hdrstop
'#include "Unit1.h"
'#include "Unit2.h"
HANDLE hComm = NULL;
TRead ReadThread;
COMMTIMEOUTS ctmoNew = {0}, ctmoOld;
//---------------------------------------------------------------------------
'#pragma resource ".dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DCB dcbCommPort;
hComm = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if(hComm == INVALID_HANDLE_VALUE)
Application->Terminate();
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);
ReadThread = new TRead(false);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
ReadThread->Terminate();
Sleep(250);
PurgeComm(hComm, PURGE_RXABORT);
SetCommTimeouts(hComm, &ctmoOld);
CloseHandle(hComm);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
TransmitCommChar(hComm, Key);
if(Key != 13 && (Key < ' ' || Key > 'z')) Key = 0;
}
//---------------------------------------------------------------------------
Pada Thread, edit listing program menjadi seperti berikut:
//---------------------------------------------------------------------------
'#include
'#pragma hdrstop
'#include "Unit1.h"
'#include "Unit2.h"
extern HANDLE hComm;
char InBuff[100];
//---------------------------------------------------------------------------
__fastcall TRead::TRead(bool CreateSuspended) :TThread(CreateSuspended)
{
}
//---------------------------------------------------------------------------
void __fastcall TRead::DisplayIt()
{
Form1->Memo1->SetSelTextBuf(InBuff);
}
//---------------------------------------------------------------------------
void __fastcall TRead::Execute()
{
DWORD dwBytesRead;
FreeOnTerminate = true;
while(1)
{
ReadFile(hComm, InBuff, 50, &dwBytesRead, NULL);
if(dwBytesRead)
{
InBuff[dwBytesRead] = 0;
Synchronize(DisplayIt);
}
}
}
//---------------------------------------------------------------------------
Yang berikutnya adalah melakukan sinkronisasi fungsi DisplayIt() pada Thread. Buka file unit2.h dan tambahkan baris DisplayIt() seperti berikut
//---------------------------------------------------------------------------
class TRead : public TThread
{
private:
protected:
void __fastcall DisplayIt(void);
void __fastcall Execute();
public:
__fastcall TRead(bool CreateSuspended);
};
//---------------------------------------------------------------------------
Selesai.from : ARIEF SHAFF