您的位置:首页 > 编程语言 > C语言/C++

今天又用c++builer写了个风机模拟软件,贴出来备忘,协议我的上传资源里有

2011-11-17 18:04 483 查看
*.cpp

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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "crc.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma link "AniGIFCtrl_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::fan(bool b,int i)
{
switch(i)
{
case 0:
AniGIF2->Playing=b;
AniGIF4->Playing=b;
AniGIF6->Playing=b;
AniGIF8->Playing=b;
AniGIF10->Playing=b;
AniGIF12->Playing=b;
AniGIF14->Playing=b;
break;
case 1:
AniGIF1->Playing=b;
AniGIF3->Playing=b;
AniGIF5->Playing=b;
AniGIF7->Playing=b;
AniGIF9->Playing=b;
AniGIF11->Playing=b;
AniGIF13->Playing=b;
break;
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
MSComm1->Settings="9600,n,8,1";
MSComm1->InputLen=0;
MSComm1->InputMode=1;
MSComm1->RThreshold=1;

AniGIF1->Playing=false;
AniGIF2->Playing=false;
AniGIF3->Playing=false;
AniGIF4->Playing=false;
AniGIF5->Playing=false;
AniGIF6->Playing=false;
AniGIF7->Playing=false;
AniGIF8->Playing=false;
AniGIF9->Playing=false;
AniGIF10->Playing=false;
AniGIF11->Playing=false;
AniGIF12->Playing=false;
AniGIF13->Playing=false;
AniGIF14->Playing=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(MSComm1->PortOpen)
{ShowMessage("port already open");
return;
}
MSComm1->CommPort=RadioGroup1->ItemIndex+1;
MSComm1->PortOpen=true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MSComm1Comm(TObject *Sender)
{

OleVariant variant_inp;
AnsiString Msg,Msg2;
unsigned short len;
unsigned char rxdata[2048];
if(MSComm1->CommEvent==comEvReceive)
{
variant_inp=MSComm1->Input;
len=variant_inp.ArrayHighBound(1);
for(int i=0;i<len+1;i++)
rxdata[i]=variant_inp.GetElement(i);
for(int i=0;i<len+1;i++)
Msg+=IntToHex(rxdata[i],2)+" ";
unsigned short c;
c=CRC16(rxdata,len-1);
rxdata[len+1]=c>>8;
rxdata[len+2]=c&0xff;
if(rxdata[len-1]==rxdata[len+1]&&rxdata[len]==rxdata[len+2])
{
if(rxdata[0]==0x01)
{
switch(rxdata[1])
{
case 0x10:
{
switch(rxdata[8])
{
case 0x05:
{
fan(false,0);
fan(false,1);
}break;
case 0x06:
{
fan(false,0);
fan(true,1);
}break;
case 0x09:
{
fan(true,0);
fan(false,1);
}break;
case 0x0a:
{
fan(true,0);
fan(true,1);
}break;
}
c=CRC16(rxdata,6);
rxdata[6]=c>>8;
rxdata[7]=c&0xff;
variant_inp=VarArrayCreate(OPENARRAY(int,(0,7)),varByte);
for(int i=0;i<8;i++)
{
variant_inp.PutElement(rxdata[i],i);
Msg2+=IntToHex(rxdata[i],2)+" ";

}
MSComm1->Output=variant_inp;
ListBox1->Items->Add("接收:"+Msg);
ListBox1->Items->Add("发送:"+Msg2);
ListBox1->ItemIndex=ListBox1->Items->Count-1;
ListBox1->SetFocus();
} break;
case 0x03:
{
rxdata[2]=0x06;
rxdata[3]=((CheckBox1->Checked?1:0)<<7)|
((CheckBox2->Checked?1:0)<<6)|
((CheckBox3->Checked?1:0)<<5)|
((CheckBox4->Checked?1:0)<<4)|
((CheckBox5->Checked?1:0)<<3)|
((CheckBox6->Checked?1:0)<<2)|
((CheckBox7->Checked?1:0)<<1)|
(CheckBox8->Checked?1:0);
rxdata[4]=((CheckBox9->Checked?1:0)<<7)|
((CheckBox10->Checked?1:0)<<6)|
((CheckBox11->Checked?1:0)<<5)|
((CheckBox12->Checked?1:0)<<4)|
((CheckBox13->Checked?1:0)<<3)|
((CheckBox14->Checked?1:0)<<2)|
((CheckBox15->Checked?1:0)<<1)|
(CheckBox16->Checked?1:0);
rxdata[5]=((CheckBox17->Checked?1:0)<<7)|
((CheckBox18->Checked?1:0)<<6)|
((CheckBox19->Checked?1:0)<<5)|
((CheckBox20->Checked?1:0)<<4)|
((CheckBox21->Checked?1:0)<<3)|
((CheckBox22->Checked?1:0)<<2)|
((CheckBox23->Checked?1:0)<<1)|
(CheckBox24->Checked?1:0);
rxdata[6]=((CheckBox25->Checked?1:0)<<7)|
((CheckBox26->Checked?1:0)<<6)|
((CheckBox27->Checked?1:0)<<5)|
((CheckBox28->Checked?1:0)<<4)|
((CheckBox29->Checked?1:0)<<3)|
((CheckBox30->Checked?1:0)<<2)|
((CheckBox31->Checked?1:0)<<1)|
(CheckBox32->Checked?1:0);
rxdata[7]=((CheckBox33->Checked?1:0)<<7)|
((CheckBox34->Checked?1:0)<<6)|
((CheckBox35->Checked?1:0)<<5)|
((CheckBox36->Checked?1:0)<<4)|
((CheckBox37->Checked?1:0)<<3)|
((CheckBox38->Checked?1:0)<<2)|
((CheckBox39->Checked?1:0)<<1)|
(CheckBox40->Checked?1:0);
rxdata[8]=((CheckBox41->Checked?1:0)<<7)|
((CheckBox42->Checked?1:0)<<6)|
((CheckBox43->Checked?1:0)<<5)|
((CheckBox44->Checked?1:0)<<4)|
((CheckBox45->Checked?1:0)<<3)|
((CheckBox46->Checked?1:0)<<2)|
((CheckBox47->Checked?1:0)<<1)|
(CheckBox48->Checked?1:0);
c=CRC16(rxdata,9);
rxdata[9]=c>>8;
rxdata[10]=c&0xff;
variant_inp=VarArrayCreate(OPENARRAY(int,(0,10)),varByte);
for(int i=0;i<11;i++)
{
variant_inp.PutElement(rxdata[i],i);
Msg2+=IntToHex(rxdata[i],2)+" ";

}
MSComm1->Output=variant_inp;
ListBox1->Items->Add("接收:"+Msg);
ListBox1->Items->Add("发送:"+Msg2);
ListBox1->ItemIndex=ListBox1->Items->Count-1;
ListBox1->SetFocus();
}
break;
}
}
}

}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox25Click(TObject *Sender)
{ if(CheckBox25->Checked)
AniGIF1->Play();
else
AniGIF1->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox24Click(TObject *Sender)
{
if(CheckBox24->Checked)
AniGIF2->Play();
else
AniGIF2->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox23Click(TObject *Sender)
{
if(CheckBox23->Checked)
AniGIF3->Play();
else
AniGIF3->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox22Click(TObject *Sender)
{
if(CheckBox22->Checked)
AniGIF4->Play();
else
AniGIF4->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox21Click(TObject *Sender)
{
if(CheckBox21->Checked)
AniGIF5->Play();
else
AniGIF5->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox20Click(TObject *Sender)
{
if(CheckBox20->Checked)
AniGIF6->Play();
else
AniGIF6->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox19Click(TObject *Sender)
{
if(CheckBox19->Checked)
AniGIF7->Play();
else
AniGIF7->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox18Click(TObject *Sender)
{
if(CheckBox18->Checked)
AniGIF8->Play();
else
AniGIF8->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox17Click(TObject *Sender)
{
if(CheckBox17->Checked)
AniGIF9->Play();
else
AniGIF9->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox48Click(TObject *Sender)
{
if(CheckBox48->Checked)
AniGIF10->Play();
else
AniGIF10->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox47Click(TObject *Sender)
{
if(CheckBox47->Checked)
AniGIF11->Play();
else
AniGIF11->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox46Click(TObject *Sender)
{
if(CheckBox46->Checked)
AniGIF12->Play();
else
AniGIF12->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox45Click(TObject *Sender)
{
if(CheckBox45->Checked)
AniGIF13->Play();
else
AniGIF13->Stop();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox44Click(TObject *Sender)
{
if(CheckBox44->Checked)
AniGIF14->Play();
else
AniGIF14->Stop();
}
//---------------------------------------------------------------------------


*.h

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "MSCommLib_OCX.h"
#include <ExtCtrls.hpp>
#include <OleCtrls.hpp>
#include "AniGIFCtrl_OCX.h"
#include <DBOleCtl.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
TButton *Button1;
TMSComm *MSComm1;
TRadioGroup *RadioGroup1;
TAniGIF *AniGIF1;
TAniGIF *AniGIF2;
TAniGIF *AniGIF3;
TAniGIF *AniGIF4;
TAniGIF *AniGIF5;
TAniGIF *AniGIF6;
TAniGIF *AniGIF7;
TAniGIF *AniGIF8;
TAniGIF *AniGIF9;
TAniGIF *AniGIF10;
TAniGIF *AniGIF11;
TAniGIF *AniGIF12;
TAniGIF *AniGIF13;
TAniGIF *AniGIF14;
TCheckBox *CheckBox1;
TCheckBox *CheckBox2;
TCheckBox *CheckBox3;
TCheckBox *CheckBox4;
TCheckBox *CheckBox5;
TCheckBox *CheckBox6;
TCheckBox *CheckBox7;
TCheckBox *CheckBox8;
TCheckBox *CheckBox9;
TCheckBox *CheckBox10;
TCheckBox *CheckBox11;
TCheckBox *CheckBox12;
TCheckBox *CheckBox13;
TCheckBox *CheckBox14;
TCheckBox *CheckBox15;
TCheckBox *CheckBox16;
TCheckBox *CheckBox17;
TCheckBox *CheckBox18;
TCheckBox *CheckBox19;
TCheckBox *CheckBox20;
TCheckBox *CheckBox21;
TCheckBox *CheckBox22;
TCheckBox *CheckBox23;
TCheckBox *CheckBox24;
TCheckBox *CheckBox25;
TCheckBox *CheckBox26;
TCheckBox *CheckBox27;
TCheckBox *CheckBox28;
TCheckBox *CheckBox29;
TCheckBox *CheckBox30;
TCheckBox *CheckBox31;
TCheckBox *CheckBox32;
TCheckBox *CheckBox33;
TCheckBox *CheckBox34;
TCheckBox *CheckBox35;
TCheckBox *CheckBox36;
TCheckBox *CheckBox37;
TCheckBox *CheckBox38;
TCheckBox *CheckBox39;
TCheckBox *CheckBox40;
TCheckBox *CheckBox41;
TCheckBox *CheckBox42;
TCheckBox *CheckBox43;
TCheckBox *CheckBox44;
TCheckBox *CheckBox45;
TCheckBox *CheckBox46;
TCheckBox *CheckBox47;
TCheckBox *CheckBox48;
TListBox *ListBox1;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
TLabel *Label6;
TLabel *Label7;
TLabel *Label8;
TLabel *Label9;
TLabel *Label10;
TLabel *Label11;
TLabel *Label12;
TLabel *Label13;
TLabel *Label14;
TLabel *Label15;
TLabel *Label16;
TLabel *Label17;
TLabel *Label18;
TLabel *Label19;
TLabel *Label20;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall MSComm1Comm(TObject *Sender);
void __fastcall CheckBox25Click(TObject *Sender);
void __fastcall CheckBox24Click(TObject *Sender);
void __fastcall CheckBox23Click(TObject *Sender);
void __fastcall CheckBox22Click(TObject *Sender);
void __fastcall CheckBox21Click(TObject *Sender);
void __fastcall CheckBox20Click(TObject *Sender);
void __fastcall CheckBox19Click(TObject *Sender);
void __fastcall CheckBox18Click(TObject *Sender);
void __fastcall CheckBox17Click(TObject *Sender);
void __fastcall CheckBox48Click(TObject *Sender);
void __fastcall CheckBox47Click(TObject *Sender);
void __fastcall CheckBox46Click(TObject *Sender);
void __fastcall CheckBox45Click(TObject *Sender);
void __fastcall CheckBox44Click(TObject *Sender);

private:	// User declarations
public:		// User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TForm1::fan(bool b,int i);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

用到的控件有mscomm,anigif等
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: