//---------------------------------------------------------------------------
#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);
if(!YbCommDevice1->SettingsDialog(this,true))
Application->Terminate();
}
YbCommDevice1->PackageSize = 4096; //最大可发送 4096 个字节的数据包
YbCommDevice1->PackageType = cptFrameTimeout; //用判断超时的方法接收数据包
YbCommDevice1->UsePackage = true; //启动数据包 (可以随时启动和停止, 与 Active 属性无关)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSetClick(TObject *Sender)
{
YbCommDevice1->SettingsDialog(this,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
long X=0,Y=0;
const BufSize = 4096;
unsigned char Buffer[BufSize];
int nBytes = 0;
int AD1=0,AD2=0,Temper,Voltage;
int count=0;
while((nBytes=YbCommDevice1->ReadPackage(Buffer,BufSize))>0)
{
AnsiString s;
for(int i=0; i<nBytes; i++)
s += IntToHex(Buffer[i],2) + " ";
s = s.Trim();
//AD1=0;AD2=0;
//==============================
for(char i=16;i<20;i++){
count=count<<8;
count|=Buffer[i];
}
//Label9->Caption=IntToHex(count,8);//将数据送入Label9中显示
//==============================
//==============================
AD1=0;
for(char i=0;i<4;i++){
AD1=AD1<<8;
AD1|=Buffer[i];
}
AD1=AD1+0x0000;
Label2->Caption=IntToHex(AD1,8);//将数据送入通道1中显示
// if((StrToInt(AD1)<130000)&(StrToInt(AD1)>1000)){
Y_CH1=Y=StrToInt(AD1);
// }else{
// Y=Y_CH1;
// }
X=StrToInt(count);
Series1->AddXY(X,Y);X=0;Y=0;
//==============================
//==============================
AD2=0;
for(char i=4;i<8;i++){
AD2=AD2<<8;
AD2|=Buffer[i];
}
AD2=AD2+0x0700;
Label4->Caption=IntToHex(AD2,8);//将数据送入通道2中显示
// if((StrToInt(AD2)<130000)&(StrToInt(AD2)>1000)){
Y_CH2=Y=StrToInt(AD2);
// }else{
// Y=Y_CH2;
// }
X=StrToInt(count);
Series2->AddXY(X,Y);X=0;Y=0;
//==============================
//==============================
Temper=0;
for(char i=8;i<12;i++){
Temper=Temper<<8;
Temper|=Buffer[i];
}
Temper=Temper-0x0300;
Label6->Caption=IntToHex(Temper,8);//将数据送入Label6中显示
// if((StrToInt(Temper)<130000)&(StrToInt(Temper)>1000)){
Y_CH3=Y=StrToInt(Temper);
// }else{
// Y=Y_CH3;
// }
X=StrToInt(count);
Series3->AddXY(X,Y);X=0;Y=0;
//==============================
//==============================
Voltage=0;
for(char i=12;i<16;i++){
Voltage=Voltage<<8;
Voltage|=Buffer[i];
}
Voltage=Voltage+0x0100;
Label8->Caption=IntToHex(Voltage,8);//将数据送入Label8中显示
// if((StrToInt(Voltage)<130000)&(StrToInt(Voltage)>1000)){
Y_CH4=Y=StrToInt(Voltage);
// }else{
// Y=Y_CH4;
// }
X=StrToInt(count);
Series4->AddXY(X,Y);X=0;Y=0;
//==============================
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Form1->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSendClick(TObject *Sender)
{
const BufSize = 4096;
char Buffer[BufSize];
int nBytes = 0;
char *EndPtr=0; //指示数据转换错误 if(EndPtr){if(*EndPtr){ 转换失败 }}
AnsiString t,s = Edit1->Text.Trim();
while((s.Length()>0) && (nBytes<BufSize))
{
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->WritePackage(Buffer,nBytes); //发送数据包
}
//---------------------------------------------------------------------------