📄 main.cpp
字号:
TimerRun->Enabled=true;
//RecordData();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::ActionStopExecute(TObject *Sender)
{
//Action Stop
A1->Enabled=true;
BitBtn24->Enabled=true;
BitBtn25->Enabled=true;
BitBtn26->Enabled=true;
BitBtn27->Enabled=true;
BitBtn28->Enabled=true;
BitBtn29->Enabled=true;
BitBtn30->Enabled=true;
BitBtn31->Enabled=true;
BitBtn32->Enabled=true;
BitBtn33->Enabled=true;
BitBtn16->Enabled=true; // manual run button
BitBtn13->Enabled=true;
BitBtn14->Enabled=true;
BitBtn20->Enabled=true;
BitBtn1->Enabled=true;
BitBtn2->Enabled=true;
BitBtn3->Enabled=true;
BitBtn5->Enabled=true;
BitBtn6->Enabled=true;
BitBtn7->Enabled=true;
BitBtn9->Enabled=true;
BitBtn11->Enabled=true;
S3->Enabled=true;
BitBtn39->Enabled=true;
BitBtn36->Enabled=true;
BitBtn38->Enabled=true;
TreeView1->Enabled=true;
autoread=0;
stopedread=1;
//N4->Enabled=true;
//N5->Enabled=false;
TimerRun->Enabled=false;
//BitBtn13->Enabled=true;
//BitBtn14->Enabled=false;
ActionRun->Enabled=true;
ActionStop->Enabled=false;
StatusBar1->Panels->Items[3]->Text="手动操作";
ActionAdd->Enabled=true;
ActionDel->Enabled=true;
ActionComm->Enabled=true;
ActionADSet->Enabled=true;
ToolButton3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N5Click(TObject *Sender)
{
PageControl1->ActivePageIndex=1;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N3Click(TObject *Sender)
{
Form3->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N4Click(TObject *Sender)
{
Form4->ShowModal();
}
//---------------------------------------------------------------------------
String sumStr;
void __fastcall TFormMain::Comm1ReceiveData(TObject *Sender)
{
//define array
DynamicArray<byte> Receiveb;
Receiveb.Length=Comm1->InputLen;
Receiveb=Comm1->ReadInputByte();
//identify data
if(Receiveb.Length>3) {timeout=0;
}
else {timeout=1;//ChangeAdrTest=1;
}
if(Receiveb.Length==6||Receiveb.Length==8||Receiveb.Length==14||Receiveb.Length==7||Receiveb.Length==15) ;
else {StatusBar1->Panels->Items[2]->Text="数据错误";
return;}
int i;
String tmpStr,subStr;
sumStr=AnsiString();
//Get and save data
//sumStr="";
for(i=0;i<Receiveb.Length;i++) //for start
{if(Receiveb.Length>10)
{rxdata[i]=Receiveb[i];}
else
{if(Receiveb.Length==8)
{rxdata[i]=Receiveb[i];}
else
{rxdata[i+8]=Receiveb[i];}
}
tmpStr=ByteToHexStr(Receiveb[i]);
sumStr=sumStr+tmpStr+" ";
} //for end
//Received display
Memo2->Lines->Add(sumStr);
//Check //if Receiveb.Length==14,15,6,7 start
if(Receiveb.Length==14||Receiveb.Length==6||Receiveb.Length==7||Receiveb.Length==15)
{ if(rxdata[3]==3) //the data is switch data
{GetAdd=0;GetAlg=0;GetDgt=1; ChangeAdrTest=0;
TimerDgtDisp->Enabled=true;}
else if(rxdata[3]==5) //the data is analog data
{GetAdd=0;GetAlg=1;GetDgt=0; GetAlg2=1; ChangeAdrTest=1;
TimerAlgDisp->Enabled=true;}
else if(rxdata[3]==7) //the data is information include address
{GETADR=rxdata[5];GetAdd=1;GetAlg=0;GetDgt=0; ChangeAdrTest=1;
TimerAddDisp->Enabled=true;}
RTN=rxdata[4];
switch(RTN)
{
case 1:StatusBar1->Panels->Items[2]->Text="正常";
break;
case 2:
StatusBar1->Panels->Items[2]->Text="空调机无回应";RTNError=1;timeout=1;
return;
case 3:
StatusBar1->Panels->Items[2]->Text="CHKSUM错误";RTNError=1; timeout=1;
return;
case 4:
StatusBar1->Panels->Items[2]->Text="命令格式错误";RTNError=1;timeout=1;
return;
case 5:
StatusBar1->Panels->Items[2]->Text="其它错误";RTNError=1;timeout=1;
return;
case 6:
StatusBar1->Panels->Items[2]->Text="CMD_TYPE无效";RTNError=1;timeout=1;
return;
case 7:
StatusBar1->Panels->Items[2]->Text="CMD_ID无效";RTNError=1;timeout=1;
return;
default:StatusBar1->Panels->Items[2]->Text="数据格式错误";timeout=1;return;
}//switch end
//if(dgtalg==2) readalg=1;
//else readalg=0;
for(int n=0;n<13;n++) rxdata[18]=rxdata[18]^rxdata[n];
if (rxdata[13]==rxdata[18])
{StatusBar1->Panels->Items[1]->Text="通信成功!!";CHKSUMError=0;}
else
{StatusBar1->Panels->Items[1]->Text="校验出错!";CHKSUMError=1; }
// Timerdisp->Enabled=true;
//Tx INFO display
Edit3->Text=ByteToHexStr(rxdata[5])+' '+ByteToHexStr(rxdata[6])+' '+ByteToHexStr(rxdata[7])+' '
+ByteToHexStr(rxdata[8])+' '+ByteToHexStr(rxdata[9])+' '+ByteToHexStr(rxdata[10])+' '+ByteToHexStr(rxdata[11])+' '
+ByteToHexStr(rxdata[12]);
}//if Receiveb.Length==14,15,6,7 end
}//Event end
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn5Click(TObject *Sender)
{ AnsiString chara="目前本系统数据库有设备 ";
AnsiString charb=" 台";
String charc;
charc=IntToStr(TableP->RecordCount);
if(TableP->RecordCount==0)
{ShowMessage("目前本管理系统数据库无任何设备记录!");
}
else
{ShowMessage(chara+charc+charb);}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn10Click(TObject *Sender)
{
Edit1->Clear(); //地址查找清空
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn12Click(TObject *Sender)
{ //时间段查找数据复位
DateTimePicker1->Date=Now();
DateTimePicker4->Date=Now();
DateTimePicker3->Time=Now();
DateTimePicker2->Time=0;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn8Click(TObject *Sender)
{
//显示所有
TableD->Filtered=false;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::A1Click(TObject *Sender)
{
TablePS->First();
if(TablePS->RecordCount==0||TablePS->FieldByName("PassWord")->AsString=="") goto L1000;
if(PasswordDlg->ShowModal()==1) ;
else return;
if(Edit16->Text=="0") return;
else Edit16->Text="0";
L1000:;
PageControl1->ActivePageIndex=3;
Form2->Show();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::F1Click(TObject *Sender)
{
PageControl1->ActivePageIndex=2;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn21Click(TObject *Sender)
{
Edit5->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn20Click(TObject *Sender)
{
if(!Comm1->PortOpen) {ShowMessage("串口未打开!!"); return;}
if(MessageBox(0,"确定要使用地址获取功能吗?","系统提示",MB_OKCANCEL)==IDOK) ;
else return;
if(MessageBox(0,"该功能只有在与一台设备通信的情况下才有效,否则会出现数据错误,确认吗?","系统提示",MB_OKCANCEL)==IDOK) ;
else return;
//Address get
ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x07;
CMD_ID=0x00;
Senddata();
// getadr=1;//////
Checktimeout=1;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn32Click(TObject *Sender)
{ if(!Comm1->PortOpen) {ShowMessage("串口未打开!!"); return;}
//Open and Close Aircondition Command
Checktimeout=0;
if(RadioGroup4->ItemIndex==0) //Open
{ ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x09;
CMD_ID=0x01;
Senddata();
}
else //Close
{ ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x09;
CMD_ID=0x02;
Senddata();
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn33Click(TObject *Sender)
{ if(!Comm1->PortOpen) {ShowMessage("串口未打开!!"); return;}
//Modify address
TablePS->First();
if(TablePS->RecordCount==0||TablePS->FieldByName("PassWord")->AsString=="") goto L1034;
if(PasswordDlg->ShowModal()==1) ;
else return;
if(Edit16->Text=="0") return;
else Edit16->Text="0";
L1034:;
TableP->First();
for(int i=0;i<TableP->RecordCount;i++)
{if(Edit22->Text.ToInt()==TableP->FieldByName("Addr")->AsString.ToInt())
{ShowMessage("该地址已经被其他设备启用,请另选其他地址!");return;}
TableP->Next();
}
int addre=0;
addre=Edit2->Text.ToInt();
Checktimeout=0;
ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x02;
CMD_ID=0x05;
INFO0=Edit22->Text.ToInt();
Senddata();
Cursor=crHourGlass;
GroupBox7->Cursor=crHourGlass;
GroupBox8->Cursor=crHourGlass;
BitBtn33->Cursor=crHourGlass;
GroupBox1->Cursor=crDefault;
GroupBox2->Cursor=crDefault;
GroupBox3->Cursor=crDefault;
GroupBox4->Cursor=crDefault;
GroupBox11->Cursor=crDefault;
Delay(800);
ADR=Edit22->Text.ToInt();
CMD_TYPE=0x03; //Test if the command set OK
CMD_ID=0;
Senddata();
Delay(1000);
if(ChangeAdrTest==0) ;
else
{ChangeAdrTest=1;GroupBox8->Cursor=crDefault;BitBtn33->Cursor=crDefault;GroupBox7->Cursor=crDefault;
GroupBox1->Cursor=crDefault;
GroupBox2->Cursor=crDefault;
GroupBox3->Cursor=crDefault;
GroupBox4->Cursor=crDefault;
GroupBox11->Cursor=crDefault;
ShowMessage("地址修改不成功");return;}
// ShowMessage(" ");
Edit2->Text=Edit22->Text;
GroupBox8->Cursor=crDefault;
GroupBox7->Cursor=crDefault;
BitBtn33->Cursor=crDefault;
GroupBox1->Cursor=crDefault;
GroupBox2->Cursor=crDefault;
GroupBox3->Cursor=crDefault;
GroupBox4->Cursor=crDefault;
GroupBox11->Cursor=crDefault;
//if get address success then continue
TableP->First();
l689:;
if(TableP->FieldByName("Addr")->AsString.ToInt()==addre) ;
else{if (!TableP->Eof) {TableP->Next();goto l689; }
else return;
}
TableP->Edit();
TableP->FieldByName("Addr")->AsString=Edit22->Text;
TableP->Post();
TableP->Refresh();
ShowMessage("数据库更新成功!");
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn31Click(TObject *Sender)
{ //Set the fan rotate speed
if(!Comm1->PortOpen) {ShowMessage("串口未打开!!"); return;}
Checktimeout=0;
ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x02;
CMD_ID=0x04;
int rotSpd;
rotSpd=RadioGroup3->ItemIndex;
switch(rotSpd)
{case 0: INFO0=0x01; break; //Auto
case 1: INFO0=0x02; break; //Big
case 2: INFO0=0x03; break; //Middle
case 3: INFO0=0x04; break; //Small
default :break;
}
Senddata();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn30Click(TObject *Sender)
{ //Set work mode
if(!Comm1->PortOpen) {ShowMessage("串口未打开!!"); return;}
Checktimeout=0;
ADR=Edit2->Text.ToInt();
MYADR=myadr;
CMD_TYPE=0x02;
CMD_ID=0x03;
int rotSpd;
rotSpd=RadioGroup2->ItemIndex;
switch(rotSpd)
{case 0: INFO0=0x01; break; //Hot
case 1: INFO0=0x02; break; //Cold
case 2: INFO0=0x03; break; //Fan
case 3: INFO0=0x04; break; //Remove humid
case 4: INFO0=0x05; break; //Auto
default :break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -