找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3362|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

Embarcadero RAD Studio 10 Seattle win32串口通信例程

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:113276 發(fā)表于 2016-4-10 17:59 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
// ---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
// ---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include "CPort.hpp"
#include "CPortCtl.hpp"
// ---------------------------------------------------------------------------
class TForm1 : public TForm {
__published: // IDE-managed Components

    TComPort *ComPort1;
    TComPort *ComPort2;
    TComLed *ComLed1;
    TComLed *ComLed2;
    TComComboBox *ComComboBox1;
    TComComboBox *ComComboBox2;
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    TButton *Button4;
    TButton *Button5;
    TMemo *Memo1;
    TMemo *Memo2;
    TButton *Button6;

    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button4Click(TObject *Sender);
    void __fastcall Button5Click(TObject *Sender);
    void __fastcall Button6Click(TObject *Sender);
    void __fastcall ComPort2RxChar(TObject *Sender, int Count);
    void __fastcall ComPort1RxChar(TObject *Sender, int Count);

private: // User declarations
        public : // User declarations
    __fastcall TForm1(TComponent* Owner);
};

// ---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
// ---------------------------------------------------------------------------
#endif

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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CPort"
#pragma link "CPortCtl"
#pragma resource "*.dfm"
TForm1 *Form1;

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

// ---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) {
    ComPort1->Connected = true;
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender) {
    ComPort2->Connected = true;
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Button3Click(TObject *Sender) {
    ComPort1->Connected = false;
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Button4Click(TObject *Sender) {
    ComPort2->Connected = false;
}
// ---------------------------------------------------------------------------
unsigned char buf1[128];
unsigned char buf2[128];

void __fastcall TForm1::Button5Click(TObject *Sender) {
    buf1[0] = 0x30;
    buf1[1] = 0x31;
    buf1[2] = 0x32;
    buf1[3] = 0x33;
    buf1[4] = 0x34;
    buf1[5] = 0x35;
    buf1[6] = 0x36;
    buf1[7] = 0x37;
    buf1[8] = 0x38;
    buf1[9] = 0x39;
    buf1[10] = 0x00;
    ComPort1->Write(buf1, 10);
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::Button6Click(TObject *Sender) {
    buf2[0] = 'a';
    buf2[1] = 'b';
    buf2[2] = 'c';
    buf2[3] = 'd';
    buf2[4] = 'e';
    buf2[5] = 'f';
    buf2[6] = 'g';
    buf2[7] = 'h';
    buf2[8] = 'i';
    buf2[9] = 'j';
    buf2[10] = 0x00;
    ComPort2->Write(buf2, 10);
}
// ---------------------------------------------------------------------------

void __fastcall TForm1::ComPort2RxChar(TObject *Sender, int Count) {
    AnsiString Str;
    Str.SetLength(Count);
    ComPort2->Read(&Str[1], Count);
    Memo2->Lines->Add(Str);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::ComPort1RxChar(TObject *Sender, int Count)
{
    AnsiString Str;
    Str.SetLength(Count);
    ComPort1->Read(&Str[1], Count);
    Memo1->Lines->Add(Str);
}
//---------------------------------------------------------------------------




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表