• 大小: 8KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-12
  • 语言: C/C++
  • 标签: MFC  串口类  第三方  

资源简介

第三方串口类,用于扩展MFC的串口功能,下面是这个类的英文描述: This class can read, write and watch one serial port. It sends messages to its owner when something happends on the port. The class creates a thread for reading and writing so the main program is not blocked.

资源截图

代码片段和文件信息

/*
** FILENAME CSerialPort.cpp
**
** PURPOSE This class can read write and watch one serial port.
** It sends messages to its owner when something happends on the port
** The class creates a thread for reading and writing so the main
** program is not blocked.
**
** CREATION DATE 15-09-1997
** LAST MODIFICATION 12-11-1997
**
** AUTHOR Remon Spekreijse
**
**
*/

#include “stdafx.h“
#include “SerialPort.h“

#include 
 
//
// Constructor
//
CSerialPort::CSerialPort()
{
m_hComm = NULL;

// initialize overlapped structure members to zero
m_ov.Offset = 0;
m_ov.OffsetHigh = 0;

// create events
m_ov.hEvent = NULL;
m_hWriteEvent = NULL;
m_hShutdownEvent = NULL;

m_szWriteBuffer = NULL;

m_bThreadAlive = FALSE;
m_nWriteSize = 1;
}

//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);

if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}
// Close Handles  
if(m_hShutdownEvent!=NULL)
CloseHandle( m_hShutdownEvent); 
if(m_ov.hEvent!=NULL)
CloseHandle( m_ov.hEvent ); 
if(m_hWriteEvent!=NULL)
CloseHandle( m_hWriteEvent ); 

TRACE(“Thread ended\n“);

delete [] m_szWriteBuffer;
}

//
// Initialize the port. This can be port 1 to 4.
//
//
//parity:
//  n=none
//  e=even
//  o=odd
//  m=mark
//  s=space
//data:
//  5678
//stop:
//  11.52 
//
BOOL CSerialPort::InitPort(CWnd* pPortOwner // the owner (CWnd) of the port (receives message)
   UINT  portnr // portnumber (1..4)
   UINT  baud // baudrate
   char  parity // parity 
   UINT  databits // databits 
   UINT  stopbits // stopbits 
   DWORD dwCommEvents // EV_RXCHAR EV_CTS etc
   UINT  writebuffersize// size to the writebuffer
   
   DWORD   ReadIntervalTimeout
   DWORD   ReadTotalTimeoutMultiplier
   DWORD   ReadTotalTimeoutConstant
   DWORD   WriteTotalTimeoutMultiplier
   DWORD   WriteTotalTimeoutConstant )

{
assert(portnr > 0 && portnr < 20);
assert(pPortOwner != NULL);

// if the thread is alive: Kill
if (m_bThreadAlive)
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
TRACE(“Thread ended\n“);
}

// create events
if (m_ov.hEvent != NULL)
ResetEvent(m_ov.hEvent);
else
m_ov.hEvent = CreateEvent(NULL TRUE FALSE NULL);

if (m_hWriteEvent != NULL)
ResetEvent(m_hWriteEvent);
else
m_hWriteEvent = CreateEvent(NULL TRUE FALSE NULL);

if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
else
m_hShutdownEvent = CreateEvent(NULL TRUE FALSE NULL);

// initialize the event objects
m_hEventArray[0] = m_hShutdownEvent; // highest priority
m_hEventArray[1] = m_ov.hEvent;
m_hEventArray[2] = m_hWriteEvent;

// initialize critical section
InitializeCriticalSection(&m_csCommuni

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件       21522  2014-10-02 15:55  SerialPort.cpp
     文件        4222  2014-10-02 13:22  SerialPort.h

评论

共有 条评论