资源简介

基于 http://blog.csdn.net/liquanhai/article/details/6941574 CSerialPort串口类最新修正版(解决关闭死锁问题)2014-01-11 - 木木- 博客频道- CSDN.NET.htm SerialPortDemo_Unicode[基于2014-12-18版SerialPort类Rn修改支持Unicode编码的基于对话框版]

资源截图

代码片段和文件信息

/*
** 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
**
**  2007-12-25 mrlong    https://code.google.com/p/mycom/
**  2011-11-06 liquanhai http://blog.csdn.net/liquanhai/article/details/6941574
**  2013-12-04 viruscamp
**  2014-12-18 liquanhai
** 2015-01-05 Rn
*/

#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(HWND 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 < 200);
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;

/

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     947704  2015-01-05 19:37  SerialPortDemo_Unicode\debug\SerialPortDemo.ilk

     文件       2889  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\ReadMe.txt

     文件      21630  2005-11-11 22:45  SerialPortDemo_Unicode\SerialPortDemo\res\SerialPortDemo.ico

     文件        370  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\res\SerialPortDemo.rc2

     文件        535  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\Resource.h

     文件      22369  2015-01-05 19:38  SerialPortDemo_Unicode\SerialPortDemo\SerialPort.cpp

     文件       5601  2015-01-05 19:38  SerialPortDemo_Unicode\SerialPortDemo\SerialPort.h

     文件      58760  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.aps

     文件       1753  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.cpp

     文件        514  2015-01-05 10:17  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.h

     文件       5129  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.rc

     文件       5601  2015-01-05 10:25  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.vcproj

     文件       1427  2015-01-05 19:37  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemo.vcproj.ATA-3579AD02262.FSMPC013.user

     文件       3027  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemoDlg.cpp

     文件        598  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\SerialPortDemoDlg.h

     文件        145  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\stdafx.cpp

     文件       2489  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\stdafx.h

     文件        907  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo.sln

    ..A..H.     15872  2015-01-05 19:37  SerialPortDemo_Unicode\SerialPortDemo.suo

     目录          0  2015-01-05 19:37  SerialPortDemo_Unicode\SerialPortDemo\Debug

     目录          0  2015-01-05 10:15  SerialPortDemo_Unicode\SerialPortDemo\res

     目录          0  2015-01-05 19:37  SerialPortDemo_Unicode\debug

     目录          0  2015-01-05 19:38  SerialPortDemo_Unicode\SerialPortDemo

     目录          0  2015-01-05 19:39  SerialPortDemo_Unicode

----------- ---------  ---------- -----  ----

              1097320                    24


评论

共有 条评论