龚建伟MFC串口通信


龚建伟用CSerialPoet类写的例子
资源截图
代码片段和文件信息
#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_nWriteSize=1;

m_bThreadAlive = FALSE;
}

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


TRACE(“Thread ended
“);

delete [] m_szWriteBuffer;
}

//
// Initialize the port. This can be port 1 to 4.
//
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
{
assert(portnr > 0 && portnr < 5);
assert(pPortOwner != NULL);

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

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

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

if (m_hShutdownEvent != NULL)
ResetEvent(m_hShutdownEvent);
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_csCommunicationSync);

// set buffersize for writing and save the owner
m_pOwner = pPortOwner;

if (m_szWriteBuffer != NULL)
delete [] m_szWriteBuffer;
m_szWriteBuffer = new char[writebuffersize];

m_nPortNr = portnr;

m_nWriteBufferSize = writebuffersize;
m_dwCommEvents = dwCommEvents;

BOOL bResult = FALSE;
char *szPort = new char[50];
char *szBaud = new char[50];

// now it critical!
EnterCriticalSection(&m_csCommunicationSync);

// if the port is already opened: close it
if (m_hComm != NULL)
{
CloseHandle(m_hComm);
m_hComm = NULL;
}

// prepare port strings
sprintf(szPort “COM%d“ portnr);
sprintf(szBaud “baud=%d parity=%c data=%d stop=%d“ baud parity databits stopbits);

// get a handle to the port
m_hComm = CreateFile(szPort // communication port string (COMX)
     GENERIC_READ | GENERIC_WRITE // read/write types
     0 // comm devices must be opened with exclusive access
     NULL // no security attributes
     OPEN_EXISTING // comm dev

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

     文件      12326  2014-10-30 08:18  SerialPortTest龚建伟DebugRCa01760

     文件      12326  2014-10-30 08:17  SerialPortTest龚建伟DebugRCa02148

     文件      12318  2014-10-30 08:18  SerialPortTest龚建伟DebugRCa02812

     文件      12326  2014-10-30 08:16  SerialPortTest龚建伟DebugRCa02820

     文件      12326  2014-10-30 08:22  SerialPortTest龚建伟DebugRCa03912

     文件      12326  2014-10-30 08:28  SerialPortTest龚建伟DebugRCa03976

     文件      27501  2014-10-30 08:30  SerialPortTest龚建伟DebugSerialPort.obj

     文件     118925  2014-10-30 08:48  SerialPortTest龚建伟DebugSerialPortTest.exe

     文件     262196  2014-10-30 08:48  SerialPortTest龚建伟DebugSerialPortTest.ilk

     文件      15416  2014-10-30 08:30  SerialPortTest龚建伟DebugSerialPortTest.obj

     文件    5628828  2014-10-30 08:30  SerialPortTest龚建伟DebugSerialPortTest.pch

     文件     418816  2014-10-30 08:48  SerialPortTest龚建伟DebugSerialPortTest.pdb

     文件       2984  2014-10-30 08:30  SerialPortTest龚建伟DebugSerialPortTest.res

     文件      30774  2014-10-30 08:48  SerialPortTest龚建伟DebugSerialPortTestDlg.obj

     文件     106124  2014-10-30 08:30  SerialPortTest龚建伟DebugStdAfx.obj

     文件     197632  2014-10-30 08:48  SerialPortTest龚建伟Debugvc60.idb

     文件     364544  2014-10-30 08:48  SerialPortTest龚建伟Debugvc60.pdb

     文件       3723  2004-01-30 22:43  SerialPortTest龚建伟ReadMe.txt

     文件       1078  2010-04-13 13:55  SerialPortTest龚建伟
esSerialPortTest.ico

     文件        406  2010-04-13 13:55  SerialPortTest龚建伟
esSerialPortTest.rc2

     文件        922  2004-02-01 12:25  SerialPortTest龚建伟RESOURCE.H

     文件      18440  2004-02-13 23:13  SerialPortTest龚建伟SerialPort.cpp

     文件       2746  2004-01-28 01:27  SerialPortTest龚建伟SerialPort.h

     文件      22024  2014-10-30 08:30  SerialPortTest龚建伟SerialPortTest.aps

     文件       1549  2014-10-30 08:50  SerialPortTest龚建伟SerialPortTest.clw

     文件       2175  2004-01-30 22:43  SerialPortTest龚建伟SerialPortTest.cpp

     文件       4435  2004-01-30 23:15  SerialPortTest龚建伟SerialPortTest.dsp

     文件        553  2004-01-30 23:18  SerialPortTest龚建伟SerialPortTest.dsw

     文件       1412  2004-01-30 22:43  SerialPortTest龚建伟SerialPortTest.h

     文件     132096  2014-10-30 08:50  SerialPortTest龚建伟SerialPortTest.ncb

............此处省略13个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)