mfc 读串口方法

CComHelp m_ComHelp;




m_ComHelp.ReadConfig();
if (!m_ComHelp.ConnectCOM())
{
MessageBox(_T("串口连接失败,配置好COM连接参数!"), _T("Error"), MB_ICONERROR|MB_OK);
return ;
}
m_ComHelp.ReadContent(pDlg->laserData); 

m_ComHelp.CloseCOM();



附m_ComHelp类


#pragma once


class CComHelp
{
public:
CComHelp(void);
~CComHelp(void);
public:
CString m_strCom;
DWORD m_dBaudRate;
int m_nReadIntervalTimeout;
int m_nReadTotalTimeoutConstant;
int m_nReadTotalTimeoutMultiplier;
int     m_nWriteTotalTimeoutMultiplier;
int m_nWriteTotalTimeoutConstant;


BOOL ConnectCOM();
void CloseCOM();
void ClearBuffer();
void ReadConfig();
void WriteConfig();
BOOL ReadContent(CString &_str);
BOOL WriteContent(CString _str);


private:
BOOL m_bConnect;
HANDLE m_Handle;
};


#include "StdAfx.h"
#include "ComHelp.h"


#include "stdafx.h"




CComHelp::CComHelp(void)
{
m_strCom = _T("");
m_dBaudRate = 0;
m_nReadIntervalTimeout = 0;
m_nReadTotalTimeoutConstant = 0;
m_nReadTotalTimeoutMultiplier = 0;
m_nWriteTotalTimeoutMultiplier = 0;
m_nWriteTotalTimeoutConstant = 0;
m_bConnect = FALSE;
m_Handle = (HANDLE)-1;
}


CComHelp::~CComHelp(void)
{
}


BOOL CComHelp::ConnectCOM()
{
if(!m_bConnect)
{
if(m_Handle == (HANDLE)-1)
{
m_Handle = CreateFile(m_strCom,GENERIC_READ|GENERIC_WRITE, 0, NULL ,OPEN_EXISTING,0,NULL);


if (m_Handle==(HANDLE)-1)
{
// CPRINTF(_T("打开Com失败!"));
return FALSE;
}


DCB wdcb;
GetCommState(m_Handle,&wdcb);//读取串口设置(波特率,校验,停止位,数据位等).
wdcb.BaudRate = /*CBR_19200;*/m_dBaudRate;
wdcb.StopBits = ONESTOPBIT;
wdcb.ByteSize = 8;
wdcb.fParity = FALSE;
wdcb.Parity = NOPARITY;
SetCommState(m_Handle,&wdcb);
SetupComm(m_Handle,1024,1024);
COMMTIMEOUTS time;
LPCOMMTIMEOUTS lpCommTimeouts = &time;
GetCommTimeouts(m_Handle,lpCommTimeouts);
lpCommTimeouts->ReadIntervalTimeout = m_nReadIntervalTimeout;
lpCommTimeouts->ReadTotalTimeoutConstant = m_nReadTotalTimeoutConstant;
lpCommTimeouts->ReadTotalTimeoutMultiplier = m_nReadTotalTimeoutMultiplier;


SetCommTimeouts(m_Handle,lpCommTimeouts);
PurgeComm(m_Handle,PURGE_TXCLEAR|PURGE_RXCLEAR);
}
m_bConnect = TRUE;
}
return TRUE;
}
void CComHelp::CloseCOM()
{
if(m_bConnect)
{
if(m_Handle != (HANDLE)-1)
{
CloseHandle(m_Handle);
m_Handle = (HANDLE)-1; 
}
m_bConnect = FALSE;
}
}


void CComHelp::ClearBuffer()
{
if(m_Handle != (HANDLE)-1)
{
PurgeComm(m_Handle,PURGE_TXCLEAR|PURGE_RXCLEAR/*|PURGE_TXABORT|PURGE_RXABORT*/);
}
}
void CComHelp::ReadConfig()
{
  m_strCom = IniReadString(_T("ComSetting"),_T("Com"),_T("Com9"),GetFilePath(CONFIG));
  m_dBaudRate = IniReadInt(_T("ComSetting"),_T("BaudRate"),CBR_9600,GetFilePath(CONFIG));
  m_nReadIntervalTimeout = IniReadInt(_T("ComSetting"),_T("nReadIntervalTimeout"),10,GetFilePath(CONFIG));
  m_nReadTotalTimeoutConstant = IniReadInt(_T("ComSetting"),_T("nReadTotalTimeoutConstant"),20,GetFilePath(CONFIG));
  m_nReadTotalTimeoutMultiplier = IniReadInt(_T("ComSetting"),_T("nReadTotalTimeoutMultiplier"),30,GetFilePath(CONFIG));
  m_nWriteTotalTimeoutMultiplier = IniReadInt(_T("ComSetting"),_T("nWriteTotalTimeoutMultiplier"),0,GetFilePath(CONFIG));
  m_nWriteTotalTimeoutConstant = IniReadInt(_T("ComSetting"),_T("nWriteTotalTimeoutConstant"),0,GetFilePath(CONFIG));


WriteConfig();
}
void CComHelp::WriteConfig()
{
  IniWriteString(_T("ComSetting"),_T("Com"),m_strCom,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("BaudRate"),m_dBaudRate,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("nReadIntervalTimeout"),m_nReadIntervalTimeout,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("nReadTotalTimeoutConstant"),m_nReadTotalTimeoutConstant,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("nReadTotalTimeoutMultiplier"),m_nReadTotalTimeoutMultiplier,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("nWriteTotalTimeoutMultiplier"),m_nWriteTotalTimeoutMultiplier,GetFilePath(CONFIG));
  IniWriteInt(_T("ComSetting"),_T("nWriteTotalTimeoutConstant"),m_nWriteTotalTimeoutConstant,GetFilePath(CONFIG));
}


BOOL CComHelp::ReadContent(CString &_str)
{
if(m_Handle == (HANDLE)-1)
return FALSE;
char ch[255] = {0};
DWORD _nrecv = 0;
if(!ReadFile(m_Handle,ch, 255, &_nrecv, NULL)) return FALSE;
CString _strW(ch);
_str = _strW;
return TRUE;
}


BOOL CComHelp::WriteContent(CString _str)
{
CStringA str(_str);
if (m_Handle == (HANDLE)-1) return FALSE;
char ch[255] = {0};
DWORD _nrecv = 0;
if (!WriteFile(m_Handle, str, str.GetLength(), &_nrecv, NULL)) return FALSE;


return TRUE;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值