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

📄 iothread.cpp

📁 51单片机学习的源代码
💻 CPP
字号:
// IOThread.cpp : implementation file
//

#include "stdafx.h"
#include "mcds.h"
#include "IOThread.h"
#include "GlobalVar.h"
#include "math.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CIOThread

IMPLEMENT_DYNCREATE(CIOThread, CWinThread)

CIOThread::CIOThread()
{
}

CIOThread::~CIOThread()
{	
//	DeviceIoControl(hDev, 0x222004,NULL, 0, NULL, 0, &nOutput, NULL);//停止设备的IOCTL by ming
//	CloseHandle(hDev);//关闭设备句柄 by ming
}

BOOL CIOThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
//	ULONG  nOutput;//IOCTL 需要  by ming
//	HANDLE hDev;				//文件句柄 by ming
	BOOL bRet = FALSE;			//读取正确或失败
	BOOL bRealWork=TRUE;		//是否是真实工作状态,正式版本将该功能屏蔽
	unsigned char temp[256];   //读取数据的缓存区
	for(int i=0;i<256;i++){temp[i]=0;}
	hDev=CreateFile("\\\\.\\MotoD12Device0",
						GENERIC_READ | GENERIC_WRITE,
						FILE_SHARE_READ | FILE_SHARE_WRITE,
						NULL,
						OPEN_EXISTING,
						FILE_ATTRIBUTE_NORMAL,
						NULL);				//打开设备 by ming
	if(hDev == INVALID_HANDLE_VALUE)
	{
		if(AfxMessageBox("未能找到设备!\n\n请检查设备及其驱动是否正确连接和安装\n\n是否进行虚拟数据采集?"
			,MB_OKCANCEL,0)==IDOK)//失败报错 by ming
		{
			bRealWork=FALSE;
		}
		else
		{
			cs.Lock();//不执行虚拟则打开此句
			nNumInfo[10]="关";
			cs.Unlock();
		}
	}
	else
	{
		DeviceIoControl(hDev, 0x222000, NULL, 0, NULL, 0, &nOutput, NULL);//启动设备的IOCTL by ming
		DeviceIoControl(hDev, 0x222008,NULL, 0, NULL, 0, &nOutput, NULL);//每次读取的IOCTL by ming
	}
	while(bOnCollect()==true)//下面为数据采集代码
	{	
		if(bRealWork==FALSE)//虚拟数据
		{
			int i;
			cs.Lock();
			for (int n=0; n<=255; n++)
			{
				i=(int)(2*sin((double)n/(double)256*3.1415926)*(double)rand()/(double)32767);//模拟程序,生成数据,并可以调整计数率
				if(data[n]+i>=65535)data[n]=65535;//防止数据溢出
				else data[n]+=i;
			}
			cs.Unlock();	
		}
		else 
		{	
			DWORD dwReturned;
			bRet = ReadFile(hDev, temp, 64, &dwReturned, NULL);//依次读取4个64字节
			bRet = ReadFile(hDev, temp + 64, 64, &dwReturned, NULL);
			bRet = ReadFile(hDev, temp + 128, 64, &dwReturned, NULL);
			bRet = ReadFile(hDev, temp + 192, 64, &dwReturned, NULL);
			cs.Lock();
			for (int n=0; n<=255; n++)//将获得的值加到data上
			{
				if(data[n]+temp[n]>=65535)data[n]=65535;//防止数据溢出
				else data[n]+=temp[n];
			}
			cs.Unlock();	
		}	
		Sleep(10);
	}

	ExitInstance();
//	AfxEndThread(NULL);//终止该线程

//	DeviceIoControl(hDev, 0x222004,NULL, 0, NULL, 0, &nOutput, NULL);//停止设备的IOCTL by ming
//	CloseHandle(hDev);//关闭设备句柄 by ming

	return TRUE;
}

int CIOThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	DeviceIoControl(hDev, 0x222004,NULL, 0, NULL, 0, &nOutput, NULL);//停止设备的IOCTL by ming
	CloseHandle(hDev);//关闭设备句柄 by ming

	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CIOThread, CWinThread)
	//{{AFX_MSG_MAP(CIOThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIOThread message handlers

bool CIOThread::bOnCollect()
{
	bool temp;
	cs.Lock();
	if(nNumInfo[10]=="开")temp=true;
	else temp=false;
	cs.Unlock();
	return temp;
}

⌨️ 快捷键说明

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