-
大小: 5.25MB文件類型: .zip金幣: 2下載: 0 次發布日期: 2023-09-22
- 語言: 其他
- 標簽: SerialPort??串口??自動發送??
資源簡介
自己用SerialPort類寫的串口程序,適合初學者看看,帶有自動發送功能,打開串口,關閉串口以及自動連接串口功能

代碼片段和文件信息
#include?“stdafx.h“
#include?“SerialPort.h“
#include?
?
CSerialPort::CSerialPort()
{
m_hComm?=?NULL;
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;
}
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 ???
???DWORD???ReadIntervalTimeout
???DWORD???ReadTotalTimeoutMultiplier
???DWORD???ReadTotalTimeoutConstant
???DWORD???WriteTotalTimeoutMultiplier
???DWORD???WriteTotalTimeoutConstant?)
{
// 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];
//?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?_T(“COM%d“)?portnr);
//?stop?is?index?0
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-10-04?16:59??SerialPortTest(串口)\
?????目錄???????????0??2014-10-04?16:57??SerialPortTest(串口)\Debug\
?????文件???????31473??2014-09-15?20:14??SerialPortTest(串口)\Debug\SerialPort.obj
?????文件???????????0??2014-09-15?20:14??SerialPortTest(串口)\Debug\SerialPort.sbr
?????文件?????5391360??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTest.bsc
?????文件??????118902??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTest.exe
?????文件??????357096??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTest.ilk
?????文件???????15320??2014-09-15?21:20??SerialPortTest(串口)\Debug\SerialPortTest.obj
?????文件?????6932736??2014-09-15?20:14??SerialPortTest(串口)\Debug\SerialPortTest.pch
?????文件??????451584??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTest.pdb
?????文件????????3028??2014-09-17?21:28??SerialPortTest(串口)\Debug\SerialPortTest.res
?????文件???????????0??2014-09-15?21:20??SerialPortTest(串口)\Debug\SerialPortTest.sbr
?????文件???????38783??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTestDlg.obj
?????文件???????????0??2014-09-22?12:49??SerialPortTest(串口)\Debug\SerialPortTestDlg.sbr
?????文件??????105972??2014-09-15?20:14??SerialPortTest(串口)\Debug\StdAfx.obj
?????文件?????1369994??2014-09-15?20:14??SerialPortTest(串口)\Debug\StdAfx.sbr
?????文件??????238592??2014-10-04?16:59??SerialPortTest(串口)\Debug\vc60.idb
?????文件??????372736??2014-09-22?12:49??SerialPortTest(串口)\Debug\vc60.pdb
?????文件????????3723??2014-08-31?19:28??SerialPortTest(串口)\ReadMe.txt
?????目錄???????????0??2014-10-04?16:57??SerialPortTest(串口)\res\
?????文件????????1106??2014-09-17?21:26??SerialPortTest(串口)\resource.h
?????文件????????1078??2014-08-31?19:28??SerialPortTest(串口)\res\SerialPortTest.ico
?????文件?????????406??2014-08-31?19:28??SerialPortTest(串口)\res\SerialPortTest.rc2
?????文件???????17508??2014-09-13?10:00??SerialPortTest(串口)\SerialPort.cpp
?????文件????????3402??2014-09-13?09:25??SerialPortTest(串口)\SerialPort.h
?????文件???????21672??2014-10-04?16:58??SerialPortTest(串口)\SerialPortTest.aps
?????文件????????1679??2014-10-04?16:59??SerialPortTest(串口)\SerialPortTest.clw
?????文件????????2175??2014-08-31?19:28??SerialPortTest(串口)\SerialPortTest.cpp
?????文件????????4436??2014-09-15?20:50??SerialPortTest(串口)\SerialPortTest.dsp
?????文件?????????551??2014-08-31?19:28??SerialPortTest(串口)\SerialPortTest.dsw
?????文件????????1414??2014-08-31?20:08??SerialPortTest(串口)\SerialPortTest.h
............此處省略8個文件信息
評論
共有 條評論