資源簡介
基于MFC的串口調試助手,已經測試通過!

代碼片段和文件信息
#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\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?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);
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?de
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????26750??2011-02-26?15:53??Test_Com2\Debug\SerialPort.obj
?????文件?????105401??2011-02-22?15:44??Test_Com2\Debug\StdAfx.obj
?????文件?????122929??2011-05-08?11:45??Test_Com2\Debug\Test_Com2.exe
?????文件?????271136??2011-05-08?11:45??Test_Com2\Debug\Test_Com2.ilk
?????文件??????16544??2011-05-08?11:10??Test_Com2\Debug\Test_Com2.obj
?????文件????5503668??2011-02-22?15:44??Test_Com2\Debug\Test_Com2.pch
?????文件?????451584??2011-05-08?11:45??Test_Com2\Debug\Test_Com2.pdb
?????文件???????3796??2011-05-05?13:33??Test_Com2\Debug\Test_Com2.res
?????文件??????51617??2011-05-08?11:45??Test_Com2\Debug\Test_Com2Dlg.obj
?????文件?????222208??2011-05-08?11:47??Test_Com2\Debug\vc60.idb
?????文件?????380928??2011-05-08?11:45??Test_Com2\Debug\vc60.pdb
?????文件???????3597??2005-05-25?09:16??Test_Com2\ReadMe.txt
?????文件???????1078??2011-02-22?15:41??Test_Com2\res\Test_Com2.ico
?????文件????????401??2011-02-22?15:41??Test_Com2\res\Test_Com2.rc2
?????文件???????1193??2011-02-24?22:25??Test_Com2\resource.h
?????文件??????18441??2011-02-26?15:53??Test_Com2\SerialPort.cpp
?????文件???????2747??2011-02-26?15:51??Test_Com2\SerialPort.h
?????文件????????211??2011-02-22?15:41??Test_Com2\StdAfx.cpp
?????文件???????1054??2011-02-22?15:41??Test_Com2\StdAfx.h
?????文件??????37708??2011-05-05?13:33??Test_Com2\Test_Com2.aps
?????文件???????1905??2011-05-08?11:48??Test_Com2\Test_Com2.clw
?????文件???????2105??2011-02-22?15:41??Test_Com2\Test_Com2.cpp
?????文件???????4342??2011-02-23?17:17??Test_Com2\Test_Com2.dsp
?????文件????????541??2011-02-22?15:41??Test_Com2\Test_Com2.dsw
?????文件???????1357??2011-02-22?15:41??Test_Com2\Test_Com2.h
?????文件??????74752??2011-05-08?11:50??Test_Com2\Test_Com2.ncb
?????文件??????49664??2011-05-08?11:50??Test_Com2\Test_Com2.opt
?????文件????????252??2011-05-08?11:47??Test_Com2\Test_Com2.plg
?????文件???????9386??2011-05-05?13:33??Test_Com2\Test_Com2.rc
?????文件??????10922??2011-05-08?11:45??Test_Com2\Test_Com2Dlg.cpp
............此處省略7個文件信息
- 上一篇:基于MFC五子棋包含網絡對戰(zhàn)
- 下一篇:VC++小游戲 彈球游戲
評論
共有 條評論