?? unitmain.cpp
字號:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
#include "UnitCommSet.h"
#include "UnitOptions.h"
#include "UnitAbout.h"
#include "TestSetData.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "YbCommDevice"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
AppCfg.Load(YbCommDevice1); //如果配置文件存在,從配置文件載入信息
InitCtrls(); //初始化控件信息
PostMessage(Handle,WM_USERCMD,UC_LOGIN,0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::InitCtrls(void) //初始化控件信息
{
Font->Name = AppLang.FontName;
Font->Charset = AppLang.Charset;
Font->Size = AppLang.FontSize;
TAppStrings Msg;
Application->Title = Msg.Str_AppTitle;
Caption = Msg.Str_AppTitle;
MenuFile->Caption = Msg.Mnu_File;
MenuData->Caption = Msg.Mnu_Data;
MenuHelp->Caption = Msg.Mnu_Help;
ActionCommSet->Caption = Msg.Mnu_CommSet ;
ActionLanguage->Caption = Msg.Mnu_Language;
ActionExit->Caption = Msg.Mnu_Exit ;
ActionHelp->Caption = Msg.Mnu_Help ;
ActionAbout->Caption = Msg.Mnu_About ;
ActionSend->Caption = Msg.Mnu_Send ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::UserLogin(int iParam) //當主窗體第一次顯示出來時產生的事件
{
try
{
YbCommDevice1->Active = true;
}
catch(Exception &e)
{
if(!CommSet())
Close();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WndProc(Messages::TMessage &Message)
{
if(Message.Msg==WM_USERCMD)
{
if(Message.WParam == UC_LOGIN)
{
UserLogin(Message.LParam);
}
}
TForm::WndProc(Message);
}
//---------------------------------------------------------------------------
bool __fastcall TForm1::CommSet(void)
{
FormCommSettings = new TFormCommSettings(this,YbCommDevice1,true);
bool bSetOK = FormCommSettings->ShowModal() == IDOK;
delete FormCommSettings;
if(bSetOK)
{
AppCfg.ReadCommToCfg(YbCommDevice1);
AppCfg.Save(); //保存到配置文件
}
return bSetOK;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel4Resize(TObject *Sender)
{
Edit1->Width = Panel4->Width;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ActionExitExecute(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ActionCommSetExecute(TObject *Sender)
{
CommSet();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ActionLanguageExecute(TObject *Sender)
{
FormOptions = new TFormOptions(this);
bool bOK = FormOptions->ShowModal();
delete FormOptions;
if(bOK)InitCtrls();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ActionAboutExecute(TObject *Sender)
{
FormAbout = new TFormAbout(this);
FormAbout->ShowModal();
delete FormAbout;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ActionSendExecute(TObject *Sender)
{
int nBytes = 0;
char Buffer[1000], *EndPtr;
AnsiString t,s = Edit1->Text.Trim();
while(s.Length()>0)
{
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->Write(Buffer,nBytes);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
unsigned char Buf[8192]; //收到的字節數不會超過串口緩存的容量, 所以分配一個緩存容量相同的Buf
int n = YbCommDevice1->Read(Buf,8192); //收到 n 個字節
AnsiString s;
for(int i=0; i<n; i++)
s += IntToHex(Buf[i],2) + " ";
s = s.Trim();
if(!s.IsEmpty())
Memo1->Lines->Add(s);
}
//---------------------------------------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -