⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 watcherdlg.cpp

📁 这是一个红外温度探头测试程序
💻 CPP
字号:
// WatcherDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Watcher.h"
#include "WatcherDlg.h"
#include "afxdb.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWatcherDlg dialog

CWatcherDlg::CWatcherDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWatcherDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWatcherDlg)
	m_timestr = _T("");
	m_envtemp = _T("");
	m_spottemp = _T("");
	m_samptime = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWatcherDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWatcherDlg)
	DDX_Control(pDX, IDC_LOOP, m_loop);
	DDX_Control(pDX, IDC_STEPTIME, m_steptime);
	DDX_Control(pDX, IDC_RECORD, m_record);
	DDX_Control(pDX, IDC_MESUREONCE, m_once);
	DDX_Control(pDX, IDC_LASERFLAG, m_laserflag);
	DDX_Control(pDX, IDC_LASER, m_laser);
	DDX_Control(pDX, IDC_TEMPFLAG, m_tempflag);
	DDX_Control(pDX, IDC_TEMP, m_temp);
	DDX_Control(pDX, IDC_POWERFLAG, m_powerflag);
	DDX_Control(pDX, IDC_POWER, m_power);
	DDX_Control(pDX, IDC_COMCTR, m_comctr);
	DDX_Control(pDX, IDC_COMBSEL, m_comsel);
	DDX_Text(pDX, IDC_TIMESTR, m_timestr);
	DDX_Text(pDX, IDC_ENVTEMP, m_envtemp);
	DDX_Text(pDX, IDC_POINTTEMP, m_spottemp);
	DDX_Text(pDX, IDC_SAMPTIME, m_samptime);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWatcherDlg, CDialog)
	//{{AFX_MSG_MAP(CWatcherDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_COMCTR, OnComctr)
	ON_BN_CLICKED(IDC_POWER, OnPower)
	ON_BN_CLICKED(IDC_TEMP, OnTemp)
	ON_BN_CLICKED(IDC_LASER, OnLaser)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_LOOP, OnLoop)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_BN_CLICKED(IDC_MESUREONCE, OnMesureonce)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWatcherDlg message handlers

BOOL CWatcherDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	HICON icon;
	m_comsel.SetCurSel(0);
	m_comctr.SetWindowText("打开");
	IfComOpen=FALSE;

	icon=AfxGetApp()->LoadIcon(IDI_OFF);
	m_powerflag.SetIcon(icon);
	m_power.SetWindowText("打开");
	m_power.EnableWindow(FALSE);
	IfPowerOn=FALSE;

	icon=AfxGetApp()->LoadIcon(IDI_OFF);
	m_tempflag.SetIcon(icon);
	m_temp.SetWindowText("启动");
	m_temp.EnableWindow(FALSE);
	IfTempOn=FALSE;

	icon=AfxGetApp()->LoadIcon(IDI_OFF);
	m_laserflag.SetIcon(icon);
	m_laser.SetWindowText("开启");
	m_laser.EnableWindow(FALSE);
	IfLaserOn=FALSE;

	m_samptime="未知";
	m_spottemp="未知";
	m_envtemp="未知";
	m_loop.SetCheck(FALSE);
	m_loop.EnableWindow(FALSE);
	m_once.EnableWindow(FALSE);
	m_steptime.EnableWindow(FALSE);
	m_steptime.SetWindowText("1");
	UpdateData(FALSE);

	m_record.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
	m_record.InsertColumn(0,"测量时间",LVCFMT_IMAGE|LVCFMT_LEFT,70);
	m_record.InsertColumn(1,"定点温度",LVCFMT_IMAGE|LVCFMT_LEFT,75);
	m_record.InsertColumn(2,"环境温度",LVCFMT_IMAGE|LVCFMT_LEFT,75);
	InitialData();
	SetTimer(2000,1,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CWatcherDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CWatcherDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CWatcherDlg::OnComctr() 
{
	// TODO: Add your control notification handler code here
	if(IfComOpen)
	{
		Serial.Close();
		m_comctr.SetWindowText("打开");
		m_power.EnableWindow(FALSE);
		m_comsel.EnableWindow();
		IfComOpen=FALSE;
	}
	else
	{
		if(!Serial.Open(m_comsel.GetCurSel()+1,2400))
		{
			AfxMessageBox("无法打开端口");
			return;
		}
		else
		{
			m_comctr.SetWindowText("关闭");
			m_power.EnableWindow();
			m_comsel.EnableWindow(FALSE);
			IfComOpen=TRUE;
		}
	}
}

void CWatcherDlg::OnPower() 
{
	// TODO: Add your control notification handler code here
	HICON icon;
//	char rec[20];
	if(IfPowerOn)
	{
		//send power off command;
		//If the return string does not match, show warning.
		SendCommand(POWEROFF);
//		if(!GetReceive(rec))
//			return;
//		else
//			;
		m_power.SetWindowText("打开");
		icon=AfxGetApp()->LoadIcon(IDI_OFF);
		m_powerflag.SetIcon(icon);
		m_comctr.EnableWindow();
		m_temp.EnableWindow(FALSE);
		m_laser.EnableWindow(FALSE);
		IfPowerOn=FALSE;
	}
	else
	{
		//send power on command
		//If the return string does not match, show warning.
		m_power.SetWindowText("关闭");
		icon=AfxGetApp()->LoadIcon(IDI_ON);
		m_powerflag.SetIcon(icon);
		m_comctr.EnableWindow(FALSE);
		m_temp.EnableWindow();
		m_laser.EnableWindow();
		IfPowerOn=TRUE;
	}
}

void CWatcherDlg::OnTemp() 
{
	// TODO: Add your control notification handler code here
	HICON icon;
	if(IfTempOn)
	{
		//send temp off command
		//If the return string does not match, show warning.
		m_temp.SetWindowText("启动");
		icon=AfxGetApp()->LoadIcon(IDI_OFF);
		m_tempflag.SetIcon(icon);
		if(m_loop.GetCheck())
			KillTimer(1000);
		m_loop.SetCheck(FALSE);
		m_loop.EnableWindow(FALSE);
		m_once.EnableWindow(FALSE);
		m_steptime.EnableWindow(FALSE);
		if(!IfLaserOn)
			m_power.EnableWindow();
		IfTempOn=FALSE;
	}
	else
	{
		//send temp on command
		//If the return string does not match, show warning.
		m_temp.SetWindowText("停止");
		icon=AfxGetApp()->LoadIcon(IDI_ON);
		m_tempflag.SetIcon(icon);
		m_power.EnableWindow(FALSE);
		m_loop.EnableWindow(TRUE);
		m_once.EnableWindow(TRUE);
		m_steptime.EnableWindow(TRUE);
		IfTempOn=TRUE;
	}
}

void CWatcherDlg::OnLaser() 
{
	// TODO: Add your control notification handler code here
	HICON icon;
	if(IfLaserOn)
	{
		//send laser on command,
		//If the return string does not match, show warning.
		m_laser.SetWindowText("开启");
		icon=AfxGetApp()->LoadIcon(IDI_OFF);
		m_laserflag.SetIcon(icon);
		if(!IfTempOn)
			m_power.EnableWindow();
		IfLaserOn=FALSE;
	}
	else
	{
		//send laser off command,
		//If the return string does not match, show warning.
		m_laser.SetWindowText("关闭");
		icon=AfxGetApp()->LoadIcon(IDI_ON);
		m_laserflag.SetIcon(icon);
		m_power.EnableWindow(FALSE);
		IfLaserOn=TRUE;
	}
}

void CWatcherDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent==2000)
	{
		m_timestr=GetTimeString();
		UpdateData(FALSE);
	}
	else if(nIDEvent==1000)
		GetTemp();
	CDialog::OnTimer(nIDEvent);
}

CString CWatcherDlg::GetTimeString(void)
{
	CString timestr;
	SYSTEMTIME CurTime;
	GetLocalTime(&CurTime);
	timestr.Format("%d:%d:%d",CurTime.wHour,CurTime.wMinute,CurTime.wSecond);
	return timestr;
}

void CWatcherDlg::OnCancel(void)
{
	if(IfComOpen)
	{
		AfxMessageBox("端口尚未关闭!");
		return;
	}
	KillTimer(2000);
	CDialog::OnCancel();
}

void CWatcherDlg::OnLoop() 
{
	// TODO: Add your control notification handler code here
	char steptimestr[20]={0};
	int steptime;
	if(m_loop.GetCheck())
	{
		m_once.EnableWindow(FALSE);
		m_steptime.GetWindowText(steptimestr,m_steptime.GetWindowTextLength()+1);
		steptime=atoi(steptimestr);
		m_steptime.EnableWindow(FALSE);
		SetTimer(1000,1000*steptime,NULL);
	}
	else
	{
		m_once.EnableWindow();
		m_steptime.EnableWindow();
		KillTimer(1000);
	}
}

void CWatcherDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_record.DeleteAllItems();
}

void CWatcherDlg::InitialData(void)
{
	POWERON[0]=0x04;POWERON[1]=0x30;POWERON[2]=0x31;POWERON[3]=0x53;POWERON[4]=0x30;POWERON[5]=0x30;POWERON[6]=0x31;POWERON[7]=0x49;POWERON[8]=0x05;
	POWEROFF[0]=0x04;POWEROFF[1]=0x30;POWEROFF[2]=0x31;POWEROFF[3]=0x53;POWEROFF[4]=0x30;POWEROFF[5]=0x30;POWEROFF[6]=0x32;POWEROFF[7]=0x4a;POWEROFF[8]=0x05;
	TEMPON[0]=0x04;TEMPON[1]=0x30;TEMPON[2]=0x31;TEMPON[3]=0x53;TEMPON[4]=0x30;TEMPON[5]=0x30;TEMPON[6]=0x33;TEMPON[7]=0x4b;TEMPON[8]=0x05;
	TEMPOFF[0]=0x04;TEMPOFF[1]=0x30;TEMPOFF[2]=0x31;TEMPOFF[3]=0x53;TEMPOFF[4]=0x30;TEMPOFF[5]=0x30;TEMPOFF[6]=0x34;TEMPOFF[7]=0x4c;TEMPOFF[8]=0x05;
	LASERON[0]=0x04;LASERON[1]=0x30;LASERON[2]=0x31;LASERON[3]=0x53;LASERON[4]=0x30;LASERON[5]=0x30;LASERON[6]=0x35;LASERON[7]=0x4d;LASERON[8]=0x05;
	LASEROFF[0]=0x04;LASEROFF[1]=0x30;LASEROFF[2]=0x31;LASEROFF[3]=0x53;LASEROFF[4]=0x30;LASEROFF[5]=0x30;LASEROFF[6]=0x36;LASEROFF[7]=0x4e;LASEROFF[8]=0x05;
	SPOTTEMP[0]=0x04;SPOTTEMP[1]=0x30;SPOTTEMP[2]=0x31;SPOTTEMP[3]=0x52;SPOTTEMP[4]=0x30;SPOTTEMP[5]=0x30;SPOTTEMP[6]=0x30;SPOTTEMP[7]=0x47;SPOTTEMP[8]=0x05;
	ENVTEMP[0]=0x04;ENVTEMP[1]=0x30;ENVTEMP[2]=0x31;ENVTEMP[3]=0x52;ENVTEMP[4]=0x30;ENVTEMP[5]=0x30;ENVTEMP[6]=0x31;ENVTEMP[7]=0x48;ENVTEMP[8]=0x05;
	MAXTEMP[0]=0x04;MAXTEMP[1]=0x30;MAXTEMP[2]=0x31;MAXTEMP[3]=0x52;MAXTEMP[4]=0x30;MAXTEMP[5]=0x30;MAXTEMP[6]=0x32;MAXTEMP[7]=0x49;MAXTEMP[8]=0x05;
	MINTEMP[0]=0x04;MINTEMP[1]=0x30;MINTEMP[2]=0x31;MINTEMP[3]=0x52;MINTEMP[4]=0x30;MINTEMP[5]=0x30;MINTEMP[6]=0x33;MINTEMP[7]=0x4a;MINTEMP[8]=0x05;
	AVETEMP[0]=0x04;AVETEMP[1]=0x30;AVETEMP[2]=0x31;AVETEMP[3]=0x52;AVETEMP[4]=0x30;AVETEMP[5]=0x30;AVETEMP[6]=0x34;AVETEMP[7]=0x4b;AVETEMP[8]=0x05;
}

void CWatcherDlg::SendCommand(char *command)
{
	for(int n=0;n<9;n++)
		Serial.SendData(&command[n],1);
}

BOOL CWatcherDlg::GetReceive(char *receive)
{
	DWORD dwStart = GetTickCount();
	DWORD dwEnd = dwStart;
	do
	{  
		dwEnd = GetTickCount();
	}while((dwEnd - dwStart)<=200); 
	if(Serial.ReadDataWaiting()!=9)
	{
		AfxMessageBox("返回数据错误!");
		return FALSE;
	}
	else
	{
		Serial.ReadData(receive,9);
		return TRUE;
	}
}

BOOL CWatcherDlg::GetTemp(void)
{
	CSize size;
	m_samptime=GetTimeString();
	int n=m_record.GetItemCount();
	m_record.InsertItem(n,m_samptime,0);
	m_record.SetItemText(n,1,"21.3");
	m_record.SetItemText(n,2,"21.5");
	size.cx=220;
	size.cy=100;
	m_record.Scroll(size);
	SendCommand(SPOTTEMP);
	UpdateData(FALSE);
	return TRUE;
}

void CWatcherDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	m_loop.SetCheck(FALSE);
	OnLoop();
	ExportAsExcel("test.xls",m_record);
}

void CWatcherDlg::ExportAsExcel(CString filename,CListCtrl &resultlist)
{
	CDatabase database;
	CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)";
	CString sSql,sExcelFile;
	CFileDialog fileDlg (FALSE, "Path", filename,OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, "*.xls");
	if( fileDlg.DoModal()==IDOK)
	{
		sExcelFile = fileDlg.GetPathName();
		CFileFind finder;
		BOOL bWorking = finder.FindFile(sExcelFile);
		if (bWorking)
		{
			DeleteFile(sExcelFile);
		}
	}
	else return;
 
	TRY
	{
		sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile);
		if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
		{
			CHeaderCtrl* pHeader = resultlist.GetHeaderCtrl();
			int nColCount = pHeader->GetItemCount();
			int nLineCount = resultlist.GetItemCount();
			int ColOrderArray[100];
			CString ca[100];
			resultlist.GetColumnOrderArray(ColOrderArray, nColCount);
			for(int i =0 ; i< nColCount; i++)
			{
				LVCOLUMN lvc;
				char text[100];
				lvc.mask = LVCF_TEXT|LVCF_SUBITEM;
				lvc.pszText = text;
				lvc.cchTextMax = 100;
				resultlist.GetColumn(ColOrderArray[i], &lvc);
				ca[i] = lvc.pszText;      
			}
			CString tempsql="(";
			tempsql+=ca[0];
			tempsql+=" TEXT,";
			tempsql+=ca[1];
			tempsql+=" FLOAT,";
			tempsql+=ca[2];
			tempsql+=" FLOAT)";
			sSql = "CREATE TABLE Sheet1 ";
			sSql+=tempsql;
			database.ExecuteSQL(sSql);
			int item_count=resultlist.GetItemCount();
			tempsql="(";
			for(i =0 ; i< nColCount-1; i++)
			{
				tempsql+=ca[i];
				tempsql+=" ,";
			}
			tempsql+=ca[nColCount-1];
			tempsql+=")";
			for(int itemnum=0;itemnum<item_count;itemnum++)
			{    
				sSql="";
				sSql ="INSERT INTO Sheet1 ";
				sSql+=tempsql;
				sSql+="VALUES ('";
				sSql+=resultlist.GetItemText(itemnum, 0);
				sSql+="',";
				sSql+=resultlist.GetItemText(itemnum, 1);
				sSql+=",";
				sSql+=resultlist.GetItemText(itemnum, 2);
				sSql+=")";
				database.ExecuteSQL(sSql);   
			}
		}      
		database.Close();  
		AfxMessageBox("Excel文件写入成功!");
	}
	CATCH_ALL(e)
	{
		TRACE1("Excel驱动没有安装: %s",sDriver);
	}
	END_CATCH_ALL; 
}

void CWatcherDlg::OnMesureonce() 
{
	// TODO: Add your control notification handler code here
	GetTemp();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -