⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unitmain.cpp

📁 C++BUILDER 6.0 delphi 7 串口控件 由www.cppfans.com網站所提供 內涵說明 非常好用
💻 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 + -