资源简介
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\n“);
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\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_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 SerialPort\SerialPortTest\Debug\cl.command.1.tlog
文件 25242 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\CL.read.1.tlog
文件 2470 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\CL.write.1.tlog
文件 2 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\li
文件 2 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\li
文件 2160 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\li
文件 5028 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\li
文件 1718 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\li
文件 642 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\mt.command.1.tlog
文件 588 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\mt.read.1.tlog
文件 498 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\mt.write.1.tlog
文件 1400 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\rc.command.1.tlog
文件 2998 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\rc.read.1.tlog
文件 710 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\rc.write.1.tlog
文件 38286 2014-01-02 15:31 SerialPort\SerialPortTest\Debug\SerialPort.obj
文件 112128 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.exe
文件 667 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.exe.em
文件 732 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.exe.em
文件 381 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.exe.intermediate.manifest
文件 1215064 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.ilk
文件 84 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.lastbuildstate
文件 3533 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.log
文件 27781 2014-01-02 15:31 SerialPort\SerialPortTest\Debug\SerialPortTest.obj
文件 20316160 2014-01-02 15:31 SerialPort\SerialPortTest\Debug\SerialPortTest.pch
文件 3787776 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.pdb
文件 2972 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest.res
文件 43667 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTestDlg.obj
文件 224 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\SerialPortTest_manifest.rc
文件 550636 2014-01-02 15:31 SerialPort\SerialPortTest\Debug\StdAfx.obj
文件 1027072 2014-01-02 15:33 SerialPort\SerialPortTest\Debug\vc100.idb
............此处省略39个文件信息
评论
共有 条评论