资源简介

利用API函数实现了串口通信功能,发送和接收可选择字符格式,含有周期性发送功能,有兴趣可以帮忙优化一下

资源截图

代码片段和文件信息

#include “StdAfx.h“
#include “SerialComm.h“


CSerialComm::CSerialComm(void)
{
m_recvData = ““;
m_bConnected = FALSE;
m_pThread = NULL;
}

CSerialComm::~CSerialComm(void)
{
if(m_bConnected)             //程序结束时删除线程、关闭串口操作
closeConnection();

if(m_hPostMsgEvent)         //删除事件句柄
CloseHandle(m_hPostMsgEvent);

if(m_osRead.hEvent)
CloseHandle(m_osRead.hEvent);

if(m_osWrite.hEvent)
CloseHandle(m_osWrite.hEvent);
}

BOOL CSerialComm::openConnection(CString commno UINT8 baud UINT8 datalen UINT8 stopbit UINT8 parity)
{
if((baud > COMM_BAUD_115200) || 
   (datalen > COMM_DATA_LEN_8) || 
   (stopbit > COMM_STOP_BIT_2) || 
   (parity > COMM_SPACE_PARITY))
{
m_bConnected = FALSE;
AfxMessageBox(_T(“串口参数设置错误“));

return FALSE;
}

m_hPostMsgEvent = CreateEvent(NULL TRUE TRUE NULL);
if(m_hPostMsgEvent == NULL)
return FALSE;

memset(&m_osRead 0 sizeof(OVERLAPPED));
memset(&m_osWrite 0 sizeof(OVERLAPPED));

m_osRead.hEvent = CreateEvent(NULL TRUE FALSE NULL);  //为重叠读创建事件对象,手工重置,初始化为无信号
if(m_osRead.hEvent == NULL)
return FALSE;

m_osWrite.hEvent = CreateEvent(NULL TRUE FALSE NULL); //为重叠写创建事件对象,手工重置,初始化为无信号
if(m_osWrite.hEvent == NULL)
return FALSE;

m_hComm = CreateFile(commno                             //打开串口
                 GENERIC_READ | GENERIC_WRITE       //允许对设备进行读写访问
 0                                  //独占方式
 NULL                               //
 OPEN_EXISTING                      //打开而不是创建
 FILE_FLAG_OVERLAPPED               //使用异步通信
 NULL);
if(m_hComm == (HANDLE)-1)
{
m_bConnected = FALSE;
AfxMessageBox(_T(“打开串口失败“));

return FALSE;
} else
{
SetupComm(m_hComm MAXBLOCK MAXBLOCK);              //输入缓冲区和输出缓冲区的大小都是1024
SetCommMask(m_hComm EV_RXCHAR | EV_TXEMPTY );       //设置事件驱动的类型

COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = MAXDWORD;             //时间间隔设为最大,设为0会导致ReadFile立即返回并完成操作
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;

TimeOuts.WriteTotalTimeoutMultiplier = 50;           //设置写超时用于GetOverlapperResult函数的等待时间
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(m_hComm &TimeOuts);                 //设置超时

//PurgeComm(m_hComm PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ); //清干净输入、输出缓冲区

if(!configureConnection(commno baud datalen stopbit parity))
{
CloseHandle(m_hComm);
return FALSE;
}

return TRUE;
}
}

BOOL CSerialComm::configureConnection(CString commno UINT8 baud UINT8 datalen UINT8 stopbit UINT8 parity)
{
DCB dcb;
if(!GetCommState(m_hComm &dcb))           //读串口原来的参数设置
return false;

switch(baud)                               //波特率
{
case COMM_BAUD_2400:
dcb.BaudRate = 2400;
break;
case COMM_BAUD_4800:
dcb.BaudRate = 4800;
break;
case COMM_BAUD_9600:
dcb.BaudRate = 9600;
break;
case COMM_BAUD

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3165  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\ReadMe.txt

     文件      67777  2009-08-31 02:31  API_SerialComm\YDVS_SerialComm\res\icon1.ico

     文件      67777  2009-08-31 02:31  API_SerialComm\YDVS_SerialComm\res\YDVS_SerialComm.ico

     文件        686  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\res\YDVS_SerialComm.rc2

     文件       3306  2013-12-24 12:01  API_SerialComm\YDVS_SerialComm\resource.h

     文件       8224  2013-12-20 17:50  API_SerialComm\YDVS_SerialComm\SerialComm.cpp

     文件       1671  2013-12-20 17:48  API_SerialComm\YDVS_SerialComm\SerialComm.h

     文件        148  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\stdafx.cpp

     文件       1636  2013-12-11 19:20  API_SerialComm\YDVS_SerialComm\stdafx.h

     文件        234  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\targetver.h

     文件      41056  2013-12-24 12:01  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.aps

     文件       2110  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.cpp

     文件        496  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.h

     文件       1481  2013-12-20 13:50  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.idc

     文件      15336  2013-12-24 12:01  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.rc

     文件       6119  2013-12-24 12:01  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.vcxproj

     文件       2316  2013-12-24 12:01  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.vcxproj.filters

     文件        143  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm\YDVS_SerialComm.vcxproj.user

     文件      12834  2014-01-19 16:18  API_SerialComm\YDVS_SerialComm\YDVS_SerialCommDlg.cpp

     文件       1754  2013-12-24 11:31  API_SerialComm\YDVS_SerialComm\YDVS_SerialCommDlg.h

     文件   58544128  2014-01-19 16:20  API_SerialComm\YDVS_SerialComm.sdf

     文件        912  2013-12-11 19:13  API_SerialComm\YDVS_SerialComm.sln

    ..A..H.     22528  2014-01-19 16:20  API_SerialComm\YDVS_SerialComm.suo

     目录          0  2014-01-19 16:16  API_SerialComm\ipch\ydvs_serialcomm-65adb533

     目录          0  2014-01-19 16:21  API_SerialComm\YDVS_SerialComm\Debug

     目录          0  2014-01-19 16:15  API_SerialComm\YDVS_SerialComm\res

     目录          0  2014-01-19 16:20  API_SerialComm\Debug

     目录          0  2014-01-19 16:16  API_SerialComm\ipch

     目录          0  2014-01-19 16:15  API_SerialComm\YDVS_SerialComm

     目录          0  2014-01-19 16:20  API_SerialComm

............此处省略3个文件信息

评论

共有 条评论