如何在c++builder中 用windowsAPI编写串口通信
2016-02-08 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
main.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
int WINAPI ReadBuff(HANDLE hwnd,BYTE *pBuf,WORD wBufLen)
{
//
//HDC hdc;
String str = "";
String szTemp;
str.sprintf("Receive %02d Size:",wBufLen);
for(int i = 0;i < wBufLen;i++)
{
szTemp = "";
szTemp.sprintf(" %02X ",pBuf[i]);
str += szTemp;
}
Form1->DisplayInMemo(str);
return 1;
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//
pComm = new TComm(Handle,2);
int iReturn = pComm->Open();
if(iReturn < 0)
{
ShowMessage(pComm->GetErrorString());
}
pComm->SetReadEvent(ReadBuff);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete pComm;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
int iReturn = pComm->DialogSetupComm();
if(iReturn < 0)
{
Canvas->TextOutA(0,40,pComm->GetErrorString());
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DisplayInMemo(String str)
{
if(Memo1->Lines->Count > 100)
{
Memo1->Lines->Clear();
}
Memo1->Text = str + char(13) + char(10) + Memo1->Text ;
}
main.h
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "TComm.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TMemo *Memo1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
TComm *pComm;
__fastcall TForm1(TComponent* Owner);
void __fastcall DisplayInMemo(String str);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
int WINAPI ReadBuff(HANDLE hwnd,BYTE *pBuf,WORD wBufLen)
{
//
//HDC hdc;
String str = "";
String szTemp;
str.sprintf("Receive %02d Size:",wBufLen);
for(int i = 0;i < wBufLen;i++)
{
szTemp = "";
szTemp.sprintf(" %02X ",pBuf[i]);
str += szTemp;
}
Form1->DisplayInMemo(str);
return 1;
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//
pComm = new TComm(Handle,2);
int iReturn = pComm->Open();
if(iReturn < 0)
{
ShowMessage(pComm->GetErrorString());
}
pComm->SetReadEvent(ReadBuff);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
delete pComm;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//
int iReturn = pComm->DialogSetupComm();
if(iReturn < 0)
{
Canvas->TextOutA(0,40,pComm->GetErrorString());
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DisplayInMemo(String str)
{
if(Memo1->Lines->Count > 100)
{
Memo1->Lines->Clear();
}
Memo1->Text = str + char(13) + char(10) + Memo1->Text ;
}
main.h
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "TComm.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TMemo *Memo1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall FormDestroy(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
TComm *pComm;
__fastcall TForm1(TComponent* Owner);
void __fastcall DisplayInMemo(String str);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询