修改之后的SerialPort.cpp(已经编译过)

CSerialPort是一个用于读写和监视串口的类,通过创建线程避免阻塞主程序。它实现了串口的初始化、事件处理、读写操作,并在关闭时释放资源。类中包含初始化端口、通信线程函数、开始和停止监控、错误处理等功能,确保串口通信的稳定和高效。
摘要由CSDN通过智能技术生成

/*
** 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 <assert.h>
 
//
// 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 the port is still opened: closed 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 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];

 // 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,        // c

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本串口工具集成目前多数串口工具的优秀功能于一身,采用CSerialPort类进行编写,并在此基础上对该类进行了改进。本工具免费试用,永不过期。并且本着奉献的精神,公开本串口工具的源代码,希望广大用户反馈意见或建议。 源码地址:code.csdn.net/itas109/serialport_itas109 博客地址:blog.csdn.net/itas109 Email:itas109@qq.com 本软件主要在XP系统做测试使用。 本工具的主要功能有: 1.接收从串口进来的数据并在窗口显示. 2.所接收到的数据数据显示方式可以选择为字符方式或者HEX方式 3.支持显示中文 4.串口波特率可以选择为300bps-115200bps. 5.可以即时刷新存在的串口号.如果您增加了usb转串口等设备,点击刷新串口按钮后,新的串口号也会在列表中出现. 6.支持最多20个串口列表,包括COM1-COM256中的任意20个活跃串口 7.可以选择“5、6、7、8”四种数据长度. 8.可以选择为“1、2”两种停止位. 9.数据可以选择为“无、奇校验、偶校验、1、0”四种方式. 10.串口设置和字符串操作等设置在程序关闭时自动保存,打开时自动载入.您可以自行设置是否自动保存. 11.可以在字符串输入框输入您想发送的字符串,并发送. 12.可以在字符串输入框输入您想发送的HEX数据串,数据的值从00到FF,没有任何限制. 13.可以定时重复发送数据,并可以设置发送时间间隔. 14.可以在发送字符串时选择发送新行,即自动加上回车换行. 15.可以即时显示发送的字节数和接收到的字节数,按清除窗口将会清零. 16、可以设置自动清空功能,在达到30行数据时,自动清空。未设置时,达到100行,自动清空. 17、在手动发送旁边的按钮可以设置按ENTER发送数据,注意点击“打开串口”后,请使焦点离开该控件. 另外要说明的是,由于本人编程能力有效,未免可能有些差错,在此先声明一下,您可以免费使用和自由传播本软件,但是我不对本软件所造成的任何损失负责!如果你使用了本软件,即表明您愿意接收这一条款。如果你不能接受,请立即将其删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值