📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "YbCommDevice"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
try
{
YbCommDevice1->Active = true;
}
catch(Exception &e)
{
ShowMessage("YbCommDevice1: "+e.Message);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSet1Click(TObject *Sender)
{
YbCommDevice1->SettingsDialog(this,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSend1Click(TObject *Sender)
{
int nBytes = 0;
char Buffer[1000], *EndPtr;
AnsiString t,s = Edit1->Text.Trim();
while(s.Length()>0)
{
int p = s.Pos(' '); //空格
if(p>0)
{
t = s.SubString(1,p-1);
s = s.SubString(p+1,s.Length()).Trim();
Buffer[nBytes++] = strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
else //还剩下最后一个字节
{
t = s;
s = "";
Buffer[nBytes++] = strtol(t.c_str(), &EndPtr, 16); //十六进制字符串转成字节
}
}
YbCommDevice1->Write(Buffer,nBytes);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
unsigned char Buf[8192]; //收到的字节数不会超过串口缓存的容量, 所以分配一个缓存容量相同的Buf
int n = YbCommDevice1->Read(Buf,8192); //收到 n 个字节
AnsiString s;
for(int i=0; i<n; i++)
s += IntToHex(Buf[i],2) + " ";
s = s.Trim();
if(!s.IsEmpty())
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -