vs2010串口发送接收程序


VS2010的串口程序,实现串口通信数据收发
资源截图
代码片段和文件信息
/*
** 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;
}

//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
    if(m_hComm!=NULL)
    {
   CloseHandle(m_hComm);
   m_hComm=NULL;
}
    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
“);

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);
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_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]

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

     文件       3582  2014-01-02 15:33  SerialPortSerialPortTestDebugcl.command.1.tlog

     文件      25242  2014-01-02 15:33  SerialPortSerialPortTestDebugCL.read.1.tlog

     文件       2470  2014-01-02 15:33  SerialPortSerialPortTestDebugCL.write.1.tlog

     文件          2  2014-01-02 15:33  SerialPortSerialPortTestDebuglink-cvtres.read.1.tlog

     文件          2  2014-01-02 15:33  SerialPortSerialPortTestDebuglink-cvtres.write.1.tlog

     文件       2160  2014-01-02 15:33  SerialPortSerialPortTestDebuglink.command.1.tlog

     文件       5028  2014-01-02 15:33  SerialPortSerialPortTestDebuglink.read.1.tlog

     文件       1718  2014-01-02 15:33  SerialPortSerialPortTestDebuglink.write.1.tlog

     文件        642  2014-01-02 15:33  SerialPortSerialPortTestDebugmt.command.1.tlog

     文件        588  2014-01-02 15:33  SerialPortSerialPortTestDebugmt.read.1.tlog

     文件        498  2014-01-02 15:33  SerialPortSerialPortTestDebugmt.write.1.tlog

     文件       1400  2014-01-02 15:33  SerialPortSerialPortTestDebug
c.command.1.tlog

     文件       2998  2014-01-02 15:33  SerialPortSerialPortTestDebug
c.read.1.tlog

     文件        710  2014-01-02 15:33  SerialPortSerialPortTestDebug
c.write.1.tlog

     文件      38286  2014-01-02 15:31  SerialPortSerialPortTestDebugSerialPort.obj

     文件     112128  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.exe

     文件        667  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.exe.embed.manifest

     文件        732  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.exe.embed.manifest.res

     文件        381  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.exe.intermediate.manifest

     文件    1215064  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.ilk

     文件         84  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.lastbuildstate

     文件       3533  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.log

     文件      27781  2014-01-02 15:31  SerialPortSerialPortTestDebugSerialPortTest.obj

     文件   20316160  2014-01-02 15:31  SerialPortSerialPortTestDebugSerialPortTest.pch

     文件    3787776  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.pdb

     文件       2972  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest.res

     文件      43667  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTestDlg.obj

     文件        224  2014-01-02 15:33  SerialPortSerialPortTestDebugSerialPortTest_manifest.rc

     文件     550636  2014-01-02 15:31  SerialPortSerialPortTestDebugStdAfx.obj

     文件    1027072  2014-01-02 15:33  SerialPortSerialPortTestDebugvc100.idb

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

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

发表评论

评论列表(条)