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;
}