本RS232的元件是由書本[c++ Builder與RS232串列通訊控制]內ch4所附的元件. 安裝方式:
step1:把TComm資料夾copy到C:\Program Files\Borland\CBuilder5 step2:開啟BCB5 step3:進入Compoment--->Install Component--->在Into existing package中的Unit file name中按
Browse按鈕,設路徑為C:\Program Files\Borland\CBuilder5\TComm
選Comm.cpp step4: 安裝完,關閉BCB5,並儲存檔案. step5:開啟BCB5,可以在上面一排元件中的system中看到TComm元件(一個9pin的Rs232端子,下方數字1010) setp6:安裝完成. [運用] 開啟: Comm1->PortOpen = true; 傳送: Comm1->OutputByte(ByteSend); 接收: ByteReceive=Comm1->ReadInputByte(); 關閉: Comm1->PortOpen=false; 全域變數的宣告:
DynamicArray ByteSend,ByteReceive;
String rdata[255]; [範例]16進制傳送和接收 功能說明:
Button1:傳送資料
Timer1:自動接收資料 //---------------------------------------------------------------------------
#include
#pragma hdrstop
#include
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Comm"
#pragma resource "*.dfm"
TForm1 *Form1;
DynamicArray ByteSend,ByteReceive;
String rdata[255];
long i;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Comm1->PortOpen = true;
ShowMessage("Comm1已開啟");
Timer1->Enabled=true; //自動接收
}
//---------------------------------------------------------------------------
void Delay(DWORD DT)
{
long tt;
tt=GetTickCount();
while (GetTickCount()-ttProcessMessages();
if ((GetTickCount()-tt)<=0)
tt=GetTickCount();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ByteSend.Length =4;
ByteSend[0] = 0x01;
ByteSend[1] = 0x03;
ByteSend[2] = 0x0c;
ByteSend[3] = 0x00;
Comm1->OutputByte(ByteSend);//傳送出去
Delay(1000); //延遲100毫秒
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{ Delay(100); //延遲100毫秒
ByteReceive=Comm1->ReadInputByte();
for(i=0;i