-
大小: 28.24MB文件类型: .rar金币: 1下载: 0 次发布日期: 2023-07-04
- 语言: C/C++
- 标签:
资源简介
很简单实用的16进制串口通信demo,还带非常实用的虚拟串口和串口监视工具
代码片段和文件信息
/*
** 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
**
** 2007-12-25 mrlong https://code.google.com/p/mycom/
** 2011-11-06 liquanhai http://blog.csdn.net/liquanhai/article/details/6941574
** 2013-12-04 viruscamp
** 2014-01-10 itas http://blog.csdn.net/itas109
** 2014-12-18 liquanhai http://blog.csdn.net/liquanhai/article/details/6941574
*/
#include “stdafx.h“
#include “SerialPort.h“
#include
int m_nComArray[20];
//
// 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;
m_nWriteSize = 1;
m_bIsSuspened = FALSE;
}
//
// Delete dynamic memory
//
CSerialPort::~CSerialPort()
{
do
{
SetEvent(m_hShutdownEvent);
} while (m_bThreadAlive);
// if the port is still opened: close it
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;
}
//
// Initialize the port. This can be port 1 to MaxSerialPortNum.
///初始化串口。只能是1-MaxSerialPortNum
//
//parity:
// n=none
// e=even
// o=odd
// m=mark
// s=space
//data:
// 5678
//stop:
// 11.52
//
BOOL CSerialPort::InitPort(HWND pPortOwner // the owner (CWnd) of the port (receives message)
UINT portnr // portnumber (1..MaxSerialPortNum)
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 < 200);
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)
ResetEven
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 170496 2017-07-16 22:19 test2\Debug\test2.exe
文件 1178324 2017-07-16 22:19 test2\Debug\test2.ilk
文件 5049344 2017-07-16 22:19 test2\Debug\test2.pdb
文件 2738 2017-07-16 22:19 test2\test2\Debug\cl.command.1.tlog
文件 49048 2017-07-16 22:19 test2\test2\Debug\CL.read.1.tlog
文件 1930 2017-07-16 22:19 test2\test2\Debug\CL.write.1.tlog
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 2 2017-07-16 22:19 test2\test2\Debug\li
文件 1596 2017-07-16 22:19 test2\test2\Debug\li
文件 4630 2017-07-16 22:19 test2\test2\Debug\li
文件 978 2017-07-16 22:19 test2\test2\Debug\li
文件 540 2017-07-16 22:19 test2\test2\Debug\mt.command.1.tlog
文件 456 2017-07-16 22:19 test2\test2\Debug\mt.read.1.tlog
文件 358 2017-07-16 22:19 test2\test2\Debug\mt.write.1.tlog
文件 916 2017-07-16 22:17 test2\test2\Debug\rc.command.1.tlog
文件 2990 2017-07-16 22:17 test2\test2\Debug\rc.read.1.tlog
文件 414 2017-07-16 22:17 test2\test2\Debug\rc.write.1.tlog
文件 51820 2017-07-16 22:06 test2\test2\Debug\SerialPort.obj
文件 630385 2017-07-16 21:07 test2\test2\Debug\stdafx.obj
文件 667 2017-07-16 21:07 test2\test2\Debug\test2.exe.em
文件 732 2017-07-16 21:07 test2\test2\Debug\test2.exe.em
文件 381 2017-07-16 22:19 test2\test2\Debug\test2.exe.intermediate.manifest
文件 50 2017-07-16 22:19 test2\test2\Debug\test2.lastbuildstate
文件 2115 2017-07-16 22:19 test2\test2\Debug\test2.log
文件 29991 2017-07-16 22:17 test2\test2\Debug\test2.obj
文件 33751040 2017-07-16 21:07 test2\test2\Debug\test2.pch
............此处省略40个文件信息
- 上一篇:Teigha帮助文档
- 下一篇:C++GUI+Qt4编程中文(中文最新版)
评论
共有 条评论