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

📄 commdevicedriver.cpp

📁 VC中不采用控件方式进行串口通信的源码
💻 CPP
字号:
#ifndef __CDEBUGTOOL_H_INCLUDE
//#include "CDebugTool.h"
#endif

#include "CommDevLinkLayer.h"

//******************************************************************************
// SetupConnection()
//
//	描述:
//		建立DCB块。通讯采用8位数据位、1位停止位,软、硬件流控制不启用。
//		设备出错时,中断读写操作。
//
//	参数:
//		HANDLE  hComm       通讯口句柄
//		DWORD 	BaudRate    通讯波特率
//******************************************************************************
static BOOL __fastcall SetupConnection( HANDLE hComm, DWORD BaudRate, BYTE parity )
{
  BOOL   	fRetVal ;
  DCB    	dcb ;
  COMMTIMEOUTS	CommTimeOuts ;

  // setup device buffers
  SetupComm( hComm, 4096, 4096 );

  // set up for overlapped I/O
  CommTimeOuts.ReadIntervalTimeout = MAXDWORD;
  CommTimeOuts.ReadTotalTimeoutMultiplier = MAXDWORD;
  CommTimeOuts.ReadTotalTimeoutConstant = 700;
  // CBR_9600 is approximately 1byte/ms. For our purposes, allow
  // double the expected time per character for a fudge factor.
  CommTimeOuts.WriteTotalTimeoutMultiplier = 1;
  CommTimeOuts.WriteTotalTimeoutConstant = 5 ;
  SetCommTimeouts( hComm, &CommTimeOuts ) ;

  // setup DCB
  dcb.DCBlength = sizeof( DCB ) ;

  GetCommState( hComm, &dcb ) ;

  dcb.BaudRate = BaudRate ;
  dcb.ByteSize = 8;
  dcb.Parity = parity ;
  dcb.StopBits = ONESTOPBIT;

  // hardware flow control,不激活。
  dcb.fOutxDsrFlow = 0 ;
  dcb.fOutxCtsFlow = 0 ;
  dcb.fDtrControl = DTR_CONTROL_ENABLE ;
  dcb.fRtsControl = RTS_CONTROL_ENABLE ;
  dcb.fDsrSensitivity = 0 ;

  // software flow control,关闭。
  dcb.fInX = dcb.fOutX = 0 ;
  //dcb.XonChar = ASCII_XON ;
  //dcb.XoffChar = ASCII_XOFF ;
  //dcb.XonLim = 100 ;
  //dcb.XoffLim = 100 ;

  // other various settings
  dcb.fBinary = TRUE ;
  dcb.fParity = (parity == EVENPARITY || parity == ODDPARITY);
  dcb.fNull = 0;
  dcb.fAbortOnError = 1;

  fRetVal = SetCommState( hComm, &dcb ) ;

  // purge any information in the buffer
  PurgeComm( hComm, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ;

  return ( fRetVal ) ;
} // end of SetupConnection()

//******************************************************************************
// CommDev::OpenCommDev()
//
//	描述:
//		打开串行通讯口,同步方式。
//
//	参数:
//		char		   *szPort	  //串口名称
//		DWORD		   BaudRate	  //通讯波特率
//
//	备注:
//		返回“假”时,可能是由于系统不存在该名称的通讯口,或该通讯口
//              已被占用,或波特率不被系统承认,或一些必要的同步对象不能被生成。
//******************************************************************************
CommDev *CommDev::OpenCommDev(const char* szPort, DWORD BaudRate)
{
  HANDLE com_dev_handle;
  if (( com_dev_handle = CreateFile( szPort, // open com port
  		GENERIC_READ | GENERIC_WRITE,
  		0, NULL, // exclusive access, no security attrs
                OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, // synchronous I/O
		NULL )) == INVALID_HANDLE_VALUE )
    return NULL;

  if (SetupConnection (com_dev_handle, BaudRate, NOPARITY)) {
    //if( !SetCommMask( com_dev_handle, EV_ERR | EV_TXEMPTY ))
    //  goto _0;

    CommDev *aNewCommDev = new CommDev ();
    aNewCommDev->itsHandle = com_dev_handle;
    aNewCommDev->itsBaudRate = BaudRate;

    return aNewCommDev;
  }

_0:
  //abnormal return
  CloseHandle( com_dev_handle );
  return NULL;
} // end of OpenCommDev()

//******************************************************************************
// CommDev::CloseCommDev()
//
//	描述:
//              关闭串行通讯口。取消通讯口的事件掩码。
//******************************************************************************
void CommDev::closeCommDev()
{
  if( itsHandle != INVALID_HANDLE_VALUE ) {
    CloseHandle (itsHandle);
    itsHandle = INVALID_HANDLE_VALUE;
  }
} // end of CloseCommDev()

//******************************************************************************
// CommDev::reopenWithParity()
//
//	描述:
//              改变串行通讯口的校验模式。
//******************************************************************************
bool CommDev::changeParityScheme(BYTE parity)
{
   return SetupConnection(itsHandle, itsBaudRate, parity);
} // end of reopenWithParity()

//******************************************************************************
// CommDev::readRaw()
//
//	描述:
//              同步方式读出。超时设置参见 SetpuConnection 。
//******************************************************************************
DWORD CommDev::readRaw(LPVOID lpBuf, DWORD BufSize)
{
  BOOL       fWriteStat ;
  COMSTAT    ComStat ;
  DWORD      dwErrorFlags;
  DWORD      dwLength;
  DWORD      dwReads = 0;
  BYTE       *buf = (BYTE*)lpBuf;
  while (BufSize > 0) {
    ClearCommError( itsHandle, &dwErrorFlags, &ComStat );
    fWriteStat = ReadFile( itsHandle, buf, BufSize, &dwLength, NULL );
    if( fWriteStat ) {
      if (dwLength == 0)
        break;

      dwReads += dwLength;
      BufSize -= dwLength;
      buf += dwLength;
    }
    else
      break;
  }

  return dwReads;
} // end of readRaw()

bool CommDev::writeRaw(LPCVOID lpBuf, DWORD sizeToWrite)
{
  BOOL        fWriteStat ;
  DWORD       dwBytesWritten ;
  DWORD       dwErrorFlags;
  COMSTAT     ComStat;

  ClearCommError( itsHandle, &dwErrorFlags, &ComStat );
  fWriteStat = WriteFile( itsHandle, lpBuf, sizeToWrite, &dwBytesWritten, NULL );

  return (fWriteStat && sizeToWrite == dwBytesWritten);
} // end of writeRaw()

⌨️ 快捷键说明

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