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

📄 unit1.cpp

📁 C++Builder 串口编程的很好的控件包
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "yb_base.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "YbCommDevice"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
  YbCommDevice1->FrameSettings->FrameHead = 0xdb; //数据包头
  YbCommDevice1->FrameSettings->FrameTail = 0xde; //数据包尾
  YbCommDevice1->FrameSettings->FrameCtrl = 0xdc; //数据控制符

  try
   {
     YbCommDevice1->Active = true;
   }
  catch(Exception &e)
   {
     ShowMessage("YbCommDevice1: "+e.Message);
     if(!YbCommDevice1->SettingsDialog(this,true))
       Application->Terminate();
   }

  YbCommDevice1->PackageSize = 4096; //最大可发送 4096 个字节的数据包
  YbCommDevice1->PackageType = cptFrameHeadTail;
  YbCommDevice1->UsePackage = true; //启动数据包 (可以随时启动和停止, 与 Active 属性无关)
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSetClick(TObject *Sender)
{
  YbCommDevice1->SettingsDialog(this,true);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButtonSendClick(TObject *Sender)
{
  try
   {
     ButtonSend->Enabled = false;

     char Buf[4096];
     long FromPos, FileSize;

     TBinFile f;
     f.FileName = "d:/topladv.gif"; //要发送的档案名
     f.Active = true;
     FileSize = f.Size; //档案长度

     Buf[0] = 0x01; //规定用 0x01 表示档案名
     *(long*)(Buf+1) = FileSize; //档案长度 (Bytes)
     strcpy(Buf+5, f.FileName.c_str());
     while(!YbCommDevice1->WritePackage(Buf,f.FileName.Length()+6))
      {
        Application->ProcessMessages(); //等候发送
      }

     FromPos = 0; //从档案第 0 个位元组开始发送
     while(FromPos<FileSize)
      {
        long BytesRemain = FileSize - FromPos;
        long BytesToRead = BytesRemain<2000?BytesRemain:2000;

        f.Pos = FromPos; //从档案的第 FromPos 个位元组开始读取
        Buf[0] = 0x02; //规定用 0x02 表示档案内容
        *(long*)(Buf+1) = FromPos;
        *(long*)(Buf+5) = f.Read(Buf+9,BytesToRead);
        while(!YbCommDevice1->WritePackage(Buf,BytesToRead+9))
         {
           Application->ProcessMessages(); //等候发送
         }

        FromPos += BytesToRead;
      }
   }
  __finally
   {
     ButtonSend->Enabled = true;
   }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::YbCommDevice1Package(TObject *Sender, int NotifyType)
{
  try
   {
     char Buf[4096];
     while(YbCommDevice1->ReadPackage(Buf,4096)>0)
      {
        if(Buf[0]==0x01) //档案名
         {
           ReceivedFileSize = *(long*)(Buf+1);
           FileReceived.FileName = TRelPath().FileName(ExtractFileName(Buf+5));
           FileReceived.OpenMode = TBinFile::omCreateNew;
           FileReceived.Active = true;
         }
        else if(Buf[0]==0x02) //档案内容
         {
           if(FileReceived.Active)
            {
              long FromPos = *(long*)(Buf+1);
              long DataSize = *(long*)(Buf+5);
              FileReceived.Pos = FromPos;
              FileReceived.Write(Buf+9,DataSize);

              if(FromPos+DataSize>=ReceivedFileSize) //接收完成
               {
                 FileReceived.Active = false;
               }
            }
         }
      }
   }
  catch(Exception &e)
   {
     Memo1->Lines->Add(e.Message);
   }
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -