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

📄 ybcommdevice.cpp

📁 简单实用, 功能强大的 C++ Builder 串口控件! 本控件是免费的, 不需要注册, 有关授权及许可协议详见 license.txt 文件。 1.支持二进制数据和文本数据的收发 2.支
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------
#pragma hdrstop

#include "YbCommDevice.h"
#include "TCommPkg.h"
#include "Unit_YbCommDevice_Settings.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TYbCommDevice *)
{
    new TYbCommDevice(NULL);
}
//---------------------------------------------------------------------------
__fastcall TYbCommDevice::TYbCommDevice(TComponent* Owner)
                         :TYbCustomCommDevice(Owner)
{
  //do nothing
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TYbCustomCommDevice::TYbCustomCommDevice(TComponent* Owner)
                               :TComponent(Owner)
{
  _bUsePackage = false; //std. port
  _bSyncEvents = true; //synchronize events to main thread

  _lpfnPackageNotify = NULL; //no default
  _lpfnCommNotify   = NULL; //no default
  _lpfnAfterOpen      = NULL; //no default
  _lpfnBeforeClose    = NULL; //no default

  _hNotifyWindow = AllocateHWnd(_EvNotifyProc);

  _CommPort = new TComm32;
  _CommPort->OnCommNotify = _EvComm;
  _CommPort->AfterOpen    = _EvAfterOpen;
  _CommPort->BeforeClose  = _EvBeforeClose;

  _CommPackage = new TCommPackage(_CommPort);
  _CommPackage->OnCommNotify = _EvPackage;

  _FrameSettings = new TYbCommFrameSettings(_CommPackage);
}
//---------------------------------------------------------------------------

__fastcall TYbCustomCommDevice::~TYbCustomCommDevice()
{
  _CommPackage->OnCommNotify = NULL;
  _CommPackage->Active = false;

  _CommPort->BeforeClose  = NULL;
  _CommPort->AfterOpen    = NULL;
  _CommPort->OnCommNotify = NULL;
  _CommPort->Active = false;

  delete _FrameSettings;
  delete _CommPackage;
  delete _CommPort;

  DeallocateHWnd(_hNotifyWindow);
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetUsePackage(bool bup)
{
  bool bAt = fGetActive();
  _bUsePackage = bup;
  fSetActive(bAt);
}
//---------------------------------------------------------------------------

bool __fastcall TYbCustomCommDevice::fGetActive(void)
{
  if(!_bUsePackage)
    return _CommPort->Active;

  if(_CommPackage->Active)
   if(_CommPort->Active)
    return true;

  return false;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetActive(bool b)
{
  bool bpka = _bUsePackage?b:false;

  if(_CommPort->Active != b)
    _CommPort->Active = b;

  if(_CommPackage->Active != bpka)
    _CommPackage->Active = bpka;
}
//---------------------------------------------------------------------------

HANDLE __fastcall TYbCustomCommDevice::fGetHandle(void)
{
  return _CommPort->Handle;
}
//---------------------------------------------------------------------------
TCommQueue *__fastcall TYbCustomCommDevice::fGetInQueue(void)
{
  return _CommPort->InQueue;
}
//---------------------------------------------------------------------------
TCommQueue *__fastcall TYbCustomCommDevice::fGetOutQueue(void)
{
  return _CommPort->OutQueue;
}
//---------------------------------------------------------------------------
int __fastcall TYbCustomCommDevice::fGetPort(void)
{
  return _CommPort->PortNo;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetPort(int n)
{
  _CommPort->PortNo = n;
}
//---------------------------------------------------------------------------

TYbCustomCommDevice::TBaudRate __fastcall TYbCustomCommDevice::fGetBaud(void)
{
  switch(_CommPort->Baud)
   {
     case CBR_110   : return br110   ;
     case CBR_300   : return br300   ;
     case CBR_600   : return br600   ;
     case CBR_1200  : return br1200  ;
     case CBR_2400  : return br2400  ;
     case CBR_4800  : return br4800  ;
     case CBR_9600  : return br9600  ;
     case CBR_14400 : return br14400 ;
     case CBR_19200 : return br19200 ;
     case CBR_38400 : return br38400 ;
     case CBR_56000 : return br56000 ;
     case CBR_57600 : return br57600 ;
     case CBR_115200: return br115200;
     case CBR_128000: return br128000;
     case CBR_256000: return br256000;
   }
  return br115200; //default value
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetBaud(TYbCustomCommDevice::TBaudRate br)
{
  switch(br)
   {
     case br110   :_CommPort->Baud = CBR_110   ; return;
     case br300   :_CommPort->Baud = CBR_300   ; return;
     case br600   :_CommPort->Baud = CBR_600   ; return;
     case br1200  :_CommPort->Baud = CBR_1200  ; return;
     case br2400  :_CommPort->Baud = CBR_2400  ; return;
     case br4800  :_CommPort->Baud = CBR_4800  ; return;
     case br9600  :_CommPort->Baud = CBR_9600  ; return;
     case br14400 :_CommPort->Baud = CBR_14400 ; return;
     case br19200 :_CommPort->Baud = CBR_19200 ; return;
     case br38400 :_CommPort->Baud = CBR_38400 ; return;
     case br56000 :_CommPort->Baud = CBR_56000 ; return;
     case br57600 :_CommPort->Baud = CBR_57600 ; return;
     case br115200:_CommPort->Baud = CBR_115200; return;
     case br128000:_CommPort->Baud = CBR_128000; return;
     case br256000:_CommPort->Baud = CBR_256000; return;
   }
  _CommPort->Baud = CBR_115200;
}
//---------------------------------------------------------------------------

TYbCustomCommDevice::TParity __fastcall TYbCustomCommDevice::fGetParity(void)
{
  switch(_CommPort->Parity)
   {
     case NOPARITY   : return TYbCustomCommDevice::ptNoParity   ;
     case ODDPARITY  : return TYbCustomCommDevice::ptOddParity  ;
     case EVENPARITY : return TYbCustomCommDevice::ptEvenParity ;
     case MARKPARITY : return TYbCustomCommDevice::ptMarkParity ;
     case SPACEPARITY: return TYbCustomCommDevice::ptSpaceParity;
   }
  return TYbCustomCommDevice::ptNoParity;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetParity(TYbCustomCommDevice::TParity pt)
{
  switch(pt)
   {
     case TYbCustomCommDevice::ptNoParity   : _CommPort->Parity = NOPARITY   ; return;
     case TYbCustomCommDevice::ptOddParity  : _CommPort->Parity = ODDPARITY  ; return;
     case TYbCustomCommDevice::ptEvenParity : _CommPort->Parity = EVENPARITY ; return;
     case TYbCustomCommDevice::ptMarkParity : _CommPort->Parity = MARKPARITY ; return;
     case TYbCustomCommDevice::ptSpaceParity: _CommPort->Parity = SPACEPARITY; return;
   }
  _CommPort->Parity = NOPARITY;
}
//---------------------------------------------------------------------------

int __fastcall TYbCustomCommDevice::fGetByteSize(void)
{
  return _CommPort->ByteSize;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetByteSize(int bs)
{
  if(bs<0)bs=0;
  if(bs>255)bs=255;
  _CommPort->ByteSize = bs;
}
//---------------------------------------------------------------------------

TYbCustomCommDevice::TStopBits __fastcall TYbCustomCommDevice::fGetStopBits(void)
{
  switch(_CommPort->StopBits)
   {
     case ONESTOPBIT  : return TYbCustomCommDevice::sbOneStopBit    ;//1 stop bit
     case ONE5STOPBITS: return TYbCustomCommDevice::sbOne_5_StopBits;//1.5 stop bits
     case TWOSTOPBITS : return TYbCustomCommDevice::sbTwoStopBit    ;//2 stop bits
   }
  return TYbCustomCommDevice::sbOneStopBit;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetStopBits(TYbCustomCommDevice::TStopBits sb)
{
  switch(sb)
   {
     case TYbCustomCommDevice::sbOneStopBit    : _CommPort->StopBits = ONESTOPBIT  ; return; //1 stop bit
     case TYbCustomCommDevice::sbOne_5_StopBits: _CommPort->StopBits = ONE5STOPBITS; return; //1.5 stop bits
     case TYbCustomCommDevice::sbTwoStopBit    : _CommPort->StopBits = TWOSTOPBITS ; return; //2 stop bits
   }
  _CommPort->StopBits = ONESTOPBIT;
}
//---------------------------------------------------------------------------

TYbCustomCommDevice::TFlowControl __fastcall TYbCustomCommDevice::fGetFlCtrl(void)
{
  switch(_CommPort->FlowControl)
   {
     case TComm32::fcNone         : return TYbCustomCommDevice::fcNone         ;
     case TComm32::fcRtsCts       : return TYbCustomCommDevice::fcRtsCts       ;
     case TComm32::fcXonXoff      : return TYbCustomCommDevice::fcXonXoff      ;
     case TComm32::fcTranXonXoff  : return TYbCustomCommDevice::fcTranXonXoff  ;
     case TComm32::fcRtsCtsXonXoff: return TYbCustomCommDevice::fcRtsCtsXonXoff;
   }
  return TYbCustomCommDevice::fcNone; //default value
}
//---------------------------------------------------------------------------
void __fastcall TYbCustomCommDevice::fSetFlCtrl(TYbCustomCommDevice::TFlowControl fc)
{
  switch(fc)
   {
     case TYbCustomCommDevice::fcNone         : _CommPort->FlowControl = TComm32::fcNone         ; return;
     case TYbCustomCommDevice::fcRtsCts       : _CommPort->FlowControl = TComm32::fcRtsCts       ; return;
     case TYbCustomCommDevice::fcXonXoff      : _CommPort->FlowControl = TComm32::fcXonXoff      ; return;
     case TYbCustomCommDevice::fcTranXonXoff  : _CommPort->FlowControl = TComm32::fcTranXonXoff  ; return;
     case TYbCustomCommDevice::fcRtsCtsXonXoff: _CommPort->FlowControl = TComm32::fcRtsCtsXonXoff; return;
   }
  _CommPort->FlowControl = TComm32::fcNone;
}
//---------------------------------------------------------------------------

int  __fastcall TYbCustomCommDevice::fGetAutAns(void)
{
  return _CommPort->AutoAnswer;
}
//---------------------------------------------------------------------------
void __fastcall TYbCustomCommDevice::fSetAutAns(int iaa)
{
  if(iaa<0)iaa=0;
  if(iaa>255)iaa=255;
  _CommPort->AutoAnswer=iaa;
}
//---------------------------------------------------------------------------

TYbCustomCommDevice::TModemStatus __fastcall TYbCustomCommDevice::fGetModemStatus(void)
{
  TYbCustomCommDevice::TModemStatus ms;
  DWORD dwMs = _CommPort->ModemStatus;

  if(dwMs & MS_CTS_ON ) ms << msCtsOn ;
  if(dwMs & MS_DSR_ON ) ms << msDsrOn ;
  if(dwMs & MS_RING_ON) ms << msRingOn;
  if(dwMs & MS_RLSD_ON) ms << msRlsdOn;

  return ms;
}
//---------------------------------------------------------------------------

bool __fastcall TYbCustomCommDevice::fGetDisableWrite(void)
{
  return _CommPort->DisableWrite;
}
//---------------------------------------------------------------------------

void __fastcall TYbCustomCommDevice::fSetDisableWrite(bool bdw)
{
  _CommPort->DisableWrite = bdw;

⌨️ 快捷键说明

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