#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "YbCommDevice"
#pragma resource "*.dfm"
TForm1 *Form1;
int i;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
YbCommDevice1->SettingsDialog(this,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
try
{
YbCommDevice1->Active = true; //设置控件活动为真
}
catch(Exception &e) // 抓错
{
ShowMessage("YbCommDevice1: "+e.Message);
if(!YbCommDevice1->SettingsDialog(this,true))
Application->Terminate();
} YbCommDevice1->PackageSize = 4096; //最大可?送 4096 ?字?的?据包
YbCommDevice1->UsePackage = true; //???据包 (可以????和停止, 与 Active ?性??)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
char Buffer2[9]={0x53 ,0x54,0x41 ,0x54,0x55,0x53,0x3F,0x0D,0x0A} ;
YbCommDevice1->Write(Buffer2,9);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int nBytes = 0;
char Buffer[1000], buf[30],*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);
int n = YbCommDevice1->Read(buf,8192);
}
//---------------------------------------------------------------------------
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
s += IntToHex(Buf[i],2) + " ";
s = s.Trim();
if(!s.IsEmpty())
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------
不知道是哪里出了问题,发出去总是收不到任何反回的消息,用你们写的测试程序发同样的命令就可以,请问哪里出问题了?
--------------------next---------------------
阅读(1626) | 评论(0) | 转发(0) |