?? unit1.cpp
字號(hào):
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma link "PERFGRAP"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//*************************全局變量定義*********************
AnsiString IniFilePath=GetCurrentDir()+"\\INI.ini";
struct SystemInfo//系統(tǒng)信息
{
int CommPort;//通訊端口
int DayNum;//天數(shù)
int StanTemp;//標(biāo)準(zhǔn)室溫
int TimeUnit;//顯示時(shí)間單位
int Memo1;//備用1
int Memo2;//備用2
AnsiString Memo3;//備用3
bool Memo4;//備用4
}objSysInfo;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::AutoCreateFolder()
{//自動(dòng)建立文件夾
if (!DirectoryExists(GetCurrentDir()+"\\" +"歷史數(shù)據(jù)\\"))
{
if (!CreateDir(GetCurrentDir()+"\\" +"歷史數(shù)據(jù)\\"))
throw Exception("不能建立歷史數(shù)據(jù)文件夾");
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ReadIniFile()
{//讀配置信息或建立新配置信息
AnsiString strPromt="系統(tǒng)信息";
TIniFile *FileIni = new TIniFile (IniFilePath);
AutoCreateFolder();
if(FileExists(IniFilePath))
{ objSysInfo.CommPort=FileIni->ReadInteger(strPromt, "串口號(hào)", 2);
objSysInfo.DayNum=FileIni->ReadInteger(strPromt, "天數(shù)", 1);
objSysInfo.StanTemp=FileIni->ReadInteger(strPromt, "標(biāo)準(zhǔn)室溫", 18);
objSysInfo.TimeUnit=FileIni->ReadInteger(strPromt, "顯示時(shí)間單位", 1);
objSysInfo.Memo1=FileIni->ReadInteger(strPromt, "備用1", 1);
objSysInfo.Memo2=FileIni->ReadInteger(strPromt, "備用2", 1);
objSysInfo.Memo3=FileIni->ReadString(strPromt, "備用3", "");
objSysInfo.Memo4=FileIni->ReadBool(strPromt, "備用4", 1);
}
else
{ Application->MessageBox("配置文件不存在,請(qǐng)重新設(shè)置參數(shù)!",NULL, MB_OK);
FileIni->WriteInteger(strPromt, "串口號(hào)",1);
FileIni->WriteInteger(strPromt, "天數(shù)",7);
FileIni->WriteInteger(strPromt, "標(biāo)準(zhǔn)室溫",18);
FileIni->WriteInteger(strPromt, "顯示時(shí)間單位",2);
FileIni->WriteInteger(strPromt, "備用1",1);
FileIni->WriteInteger(strPromt, "備用2",1);
FileIni->WriteString(strPromt, "備用3","abc");
FileIni->WriteBool(strPromt, "備用4",1);
}
delete FileIni;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::MenuEnable(bool bCtrl)
{//根據(jù)串口狀態(tài)使能菜單
mnuControl->Enabled=bCtrl;
mnuGraph->Enabled=bCtrl;
mnuData->Enabled=bCtrl;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenComm()
{//打開(kāi)串行端口
AnsiString strMeg="不能打開(kāi)端口"+String(objSysInfo.CommPort);
AnsiString strMeg1="溫度記錄儀使用端口"+String(objSysInfo.CommPort);
try
{
MSComm1->CommPort=objSysInfo.CommPort;
MSComm1->PortOpen=true;
Application->MessageBox(strMeg1.c_str(),"端口",MB_OK);
MenuEnable(true);
}
catch(...)
{ Application->MessageBox(strMeg.c_str(),"端口錯(cuò)誤,重新設(shè)置端口",MB_OK);
MenuEnable(false);
}
//改變菜單選項(xiàng)
switch(objSysInfo.CommPort)
{
case 1:
mnuCOM1->Checked=true;mnuCOM2->Checked=false;
mnuCOM3->Checked=false;mnuCOM4->Checked=false;
break;
case 2:
mnuCOM1->Checked=false;mnuCOM2->Checked=true;
mnuCOM3->Checked=false;mnuCOM4->Checked=false;
break;
case 3:
mnuCOM1->Checked=false;mnuCOM2->Checked=false;
mnuCOM3->Checked=true;mnuCOM4->Checked=false;
break;
case 4:
mnuCOM1->Checked=false;mnuCOM2->Checked=false;
mnuCOM3->Checked=false;mnuCOM4->Checked=true;
break;
}
TIniFile *FileIni = new TIniFile (IniFilePath);
FileIni->WriteInteger("系統(tǒng)信息", "串口號(hào)",objSysInfo.CommPort);
delete FileIni;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::CloseComm()
{
if(MSComm1->PortOpen) MSComm1->PortOpen=false;
}
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{//初始化
ReadIniFile();
OpenComm();
grdData->Cells[0][0]="時(shí)間";
grdData->Cells[1][0]="溫度值(℃)";
OpenFile->InitialDir=GetCurrentDir()+"\\歷史數(shù)據(jù)";
OpenFile->Filter = "溫度記錄值tev(*.tev)|*.tev";
OpenFile->Title = "打開(kāi)溫度記錄值文件";
SaveFile->InitialDir=GetCurrentDir()+"\\歷史數(shù)據(jù)";
SaveFile->Filter = "溫度記錄值文件tev(*.tev)|*.tev";
SaveFile->Title = "保存溫度記錄值文件";
UDDay->Position= objSysInfo.DayNum ;
UDTemp->Position=objSysInfo.StanTemp ;
UDYear1->Position=StrToInt(FormatDateTime("yyyy", Now()))-2000;
UDYear2->Position=UDYear1->Position;
UDMonth1->Position=StrToInt(FormatDateTime("mm", Now()));
UDMonth2->Position=UDMonth1->Position;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuExitClick(TObject *Sender)
{
CloseComm();
Close();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuCOM1Click(TObject *Sender)
{ CloseComm();
objSysInfo.CommPort=1;
OpenComm();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuCOM2Click(TObject *Sender)
{ CloseComm();
objSysInfo.CommPort=2;
OpenComm();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuCOM3Click(TObject *Sender)
{ CloseComm();
objSysInfo.CommPort=3;
OpenComm();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuCOM4Click(TObject *Sender)
{
CloseComm();
objSysInfo.CommPort=4;
OpenComm();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::SendReport(Byte ReportType)
{//發(fā)送報(bào)文
int len;
MSComm1->InBufferCount = 0;
SendData[0] = 0XEB ;//報(bào)文頭
SendData[1] = 0X90;
SendData[2] = ReportType ;//類(lèi)型
if(ReportType==1 || ReportType==2)
{
len=6;
SendData[3] = objSysInfo.DayNum ;//天數(shù)
}
else
{ SendData[3] = 20;
SendData[4] = StrToInt(FormatDateTime("yyyy", Now()))-2000;
SendData[5] = StrToInt(FormatDateTime("mm", Now()));
SendData[6] = StrToInt(FormatDateTime("dd", Now()));
SendData[7] = StrToInt(FormatDateTime("hh", Now()));
SendData[8] = StrToInt(FormatDateTime("nn", Now()));
len=11;
}
SendData[len-2]=0;
SendData[len-1] = 0XD;//結(jié)束位
for(int i=0;i<len-2;i++)
SendData[len-2] = SendData[len-2] ^ SendData[i];
sBuff=VarArrayCreate(OPENARRAY(int,(0,len-1)),varByte);
for(int i=0;i<len;i++)
sBuff.PutElement(SendData[i],i);
MSComm1->Output = sBuff;
bSendFlag=true;
DelayTime=0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::mnuAboutClick(TObject *Sender)
{
frmAbout->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ReceiveReport()
{ //接收模塊遙測(cè)數(shù)據(jù)
int InBufferCount,t;
Byte ChkSum=0;
InBufferCount=MSComm1->InBufferCount;
StatusBar->Panels->Items[0]->Text =AnsiString(InBufferCount);
if(InBufferCount >=6 )
{
rBuff = VarArrayCreate(OPENARRAY(int,(0,InBufferCount-1)),varByte);
rBuff = MSComm1->Input;
for(int i = 0;i<InBufferCount;i++)
ReceiveData[i]=rBuff.GetElement(i);
if(ReceiveData[0] == 0XEB && ReceiveData[1] == 0X90)//報(bào)文頭
{ //計(jì)算校驗(yàn)碼
for(int i = 0;i<InBufferCount-2;i++)
ChkSum =ChkSum ^ ReceiveData[i];
//校驗(yàn)碼結(jié)束位有效
if(ChkSum==ReceiveData[InBufferCount-2] && ReceiveData[InBufferCount-1] == 0X0D)
{
switch(ReceiveData[2])//類(lèi)型
{ case 1: //遙測(cè)
IniOldData() ;
ShowMsg("返回歷史數(shù)據(jù)報(bào)文");
break;
case 2://控制
if(ReceiveData[3]==1)
ShowMsg("歷史數(shù)據(jù)清空?qǐng)?zhí)行完畢");
else
ShowMsg("歷史數(shù)據(jù)清空?qǐng)?zhí)行錯(cuò)誤");
break;
case 3://校時(shí)
if(ReceiveData[3]==1)
ShowMsg("校時(shí)命令執(zhí)行完畢");
else
ShowMsg("記錄儀內(nèi)時(shí)鐘芯片DS12887電池欠電");
break;
}
}
}
}
else
ShowMsg("通訊錯(cuò)誤");
bSendFlag = false; DelayTime=0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
{
StatusBar->Panels->Items[0]->Text = IntToStr(MSComm1->InBufferCount);
StatusBar->Panels->Items[1]->Text =FormatDateTime("yyyy-mm-dd hh:nn:ss",Now());
if(bSendFlag)DelayTime++;
if(DelayTime>=2)// 接收有效站端數(shù)據(jù)
{
ReceiveReport();
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ShowMsg(AnsiString strMsg)
{
StatusBar->Panels->Items[2]->Text = FormatDateTime("yyyy-mm -dd hh:nn:ss",Now())+
" "+strMsg;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::RefreshGraph()
{//曲線區(qū)間內(nèi)刷新
int p=0;
AnsiString strTime,strDate,strT,strT1;
bool b[2]={false,true};
Series1->Clear();
Series2->Clear();
Series3->Clear();
cmbDate->Text="";
cmbDate->Items->Clear();
for(int i=0;i<DayNum;i++)
{ strDate=AnsiString(TempData[i][1])+"-" + AnsiString(TempData[i][2])+ "-" +
AnsiString(TempData[i][3]);
cmbDate->Items->Add(strDate);
for(int j=4;j<100;j++)
{ strT= strDate + " " + AnsiString(j/4-1);
strT1= strTimeUpDown[0]+ " " + tFromHour->Text;
if(!b[0]&& StrLen(strT1.c_str())==StrLen(strT.c_str())) b[0]=InStr(strT1,strT) ;
if(b[0] && b[1])
{ strT1= strTimeUpDown[1]+ " " + tToHour->Text;
if(b[1] && StrLen(strT1.c_str())==StrLen(strT.c_str())) b[1]=!InStr(strT1,strT);
strTime=AnsiString(j/4-1)+":"+AnsiString((j%4)*15);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -