📄 unit1.cpp
字号:
//---------------------------------------------------------------------------
#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//系统信息
{
int CommPort;//通讯端口
int DayNum;//天数
int StanTemp;//标准室温
int TimeUnit;//显示时间单位
int Memo1;//备用1
int Memo2;//备用2
AnsiString Memo3;//备用3
bool Memo4;//备用4
}objSysInfo;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::AutoCreateFolder()
{//自动建立文件夹
if (!DirectoryExists(GetCurrentDir()+"\\" +"历史数据\\"))
{
if (!CreateDir(GetCurrentDir()+"\\" +"历史数据\\"))
throw Exception("不能建立历史数据文件夹");
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ReadIniFile()
{//读配置信息或建立新配置信息
AnsiString strPromt="系统信息";
TIniFile *FileIni = new TIniFile (IniFilePath);
AutoCreateFolder();
if(FileExists(IniFilePath))
{ objSysInfo.CommPort=FileIni->ReadInteger(strPromt, "串口号", 2);
objSysInfo.DayNum=FileIni->ReadInteger(strPromt, "天数", 1);
objSysInfo.StanTemp=FileIni->ReadInteger(strPromt, "标准室温", 18);
objSysInfo.TimeUnit=FileIni->ReadInteger(strPromt, "显示时间单位", 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("配置文件不存在,请重新设置参数!",NULL, MB_OK);
FileIni->WriteInteger(strPromt, "串口号",1);
FileIni->WriteInteger(strPromt, "天数",7);
FileIni->WriteInteger(strPromt, "标准室温",18);
FileIni->WriteInteger(strPromt, "显示时间单位",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)
{//根据串口状态使能菜单
mnuControl->Enabled=bCtrl;
mnuGraph->Enabled=bCtrl;
mnuData->Enabled=bCtrl;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::OpenComm()
{//打开串行端口
AnsiString strMeg="不能打开端口"+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(),"端口错误,重新设置端口",MB_OK);
MenuEnable(false);
}
//改变菜单选项
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("系统信息", "串口号",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]="时间";
grdData->Cells[1][0]="温度值(℃)";
OpenFile->InitialDir=GetCurrentDir()+"\\历史数据";
OpenFile->Filter = "温度记录值tev(*.tev)|*.tev";
OpenFile->Title = "打开温度记录值文件";
SaveFile->InitialDir=GetCurrentDir()+"\\历史数据";
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)
{//发送报文
int len;
MSComm1->InBufferCount = 0;
SendData[0] = 0XEB ;//报文头
SendData[1] = 0X90;
SendData[2] = ReportType ;//类型
if(ReportType==1 || ReportType==2)
{
len=6;
SendData[3] = objSysInfo.DayNum ;//天数
}
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;//结束位
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()
{ //接收模块遥测数据
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)//报文头
{ //计算校验码
for(int i = 0;i<InBufferCount-2;i++)
ChkSum =ChkSum ^ ReceiveData[i];
//校验码结束位有效
if(ChkSum==ReceiveData[InBufferCount-2] && ReceiveData[InBufferCount-1] == 0X0D)
{
switch(ReceiveData[2])//类型
{ case 1: //遥测
IniOldData() ;
ShowMsg("返回历史数据报文");
break;
case 2://控制
if(ReceiveData[3]==1)
ShowMsg("历史数据清空执行完毕");
else
ShowMsg("历史数据清空执行错误");
break;
case 3://校时
if(ReceiveData[3]==1)
ShowMsg("校时命令执行完毕");
else
ShowMsg("记录仪内时钟芯片DS12887电池欠电");
break;
}
}
}
}
else
ShowMsg("通讯错误");
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)// 接收有效站端数据
{
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()
{//曲线区间内刷新
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -