📄 usbdoc.cpp
字号:
// USBDoc.cpp : implementation of the CUSBDoc class
//
#include "stdafx.h"
#include "USB.h"
#include "main.h"
#include "ADFrame.h"
#include <winioctl.h>
#include "ezusbsys.h"
#include "UserDef.h"
#include "USBDoc.h"
#include "USBDigitView.h"
#include "USBWaveView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//-----------------------------------------------------------
//自定义函数
BOOL TD_Init(LPVOID lpParameter); //控制参数传输函数
UINT ReadDataThread(LPVOID lpParameter); //读数据线程函数
UINT ProccessDataThread(PVOID hWnd); //处理数据线程函数
//硬件操作
HANDLE hDevice = NULL; //设备句柄
BULK_TRANSFER_CONTROL bulkControl; //传输类型
int nChannelCount=16; //通道总数
BOOLEAN bResult = FALSE; //设备描述符
Control_Mode m_Control; //控制参数传输
HANDLE hExitEvent; //线程同步信号
BOOL bDeviceRun=FALSE; //采集线程运行标志
LONG ReadSizeWords; // 读入的数据长度
WORD ADBuffer[MAX_SEGMENT][16384]; // 缓冲队列
int CurrentIndex; // AD数据处理线程当前缓冲区索引号
int ReadIndex; // AD数据采集线程当前缓冲区索引号
int DrawIndex=0; // 保存一屏数据索引
int SegmentCounts; // 记录共有多少段缓冲区没有来得及处理
BOOL bCreateDevice; //创建设备对象
BOOL bDataProcessing=FALSE; //数据处理标志
int nProcessMode; // 1:数字显示 2:波形显示
int m_MiddleLsb[32]; //是偏移码值,即原码减去LSB_HALF后的值,即在原点上下浮动的值
float VoltRange=VOLT_RANGE; //电压范围
float OffsetVolt=VOLT_RANGE/2; //电压范围一半
int CurrentProgress; // 更新进度的值,它由处理线程改变,Timer事件使用
int m_nDrawPointNum; //单通道点数 画图使用
int Center, PerY;//
CRect ClientRect;//
float middle1; //
int m_ProcessGraphicMode;//为1,波形显示
BOOL m_FirstScreenStop;//此变量控制是否首屏停止
BOOL m_BounceStop;//控制串道停止
BOOL m_AnalyzeCheck;//控制精度分析
BOOL m_StopUpdateView;//视图是否更新
int m_AnalyzeChannel;
int m_UpperLimit; //分析串道的上限
int m_LowerLimit;//分析串道的下限
int m_BounceCount;//存储串道数
int m_ScreenVolume;//设置屏幕显示的量程
int m_TriggerVolt;//触发电压
//------------------------------------------------------------
// CUSBDoc
IMPLEMENT_DYNCREATE(CUSBDoc, CDocument)
BEGIN_MESSAGE_MAP(CUSBDoc, CDocument)
//{{AFX_MSG_MAP(CUSBDoc)
ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
ON_BN_CLICKED(IDM_DigitalShow, OnDigitalShow)
//ON_UPDATE_COMMAND_UI(IDC_BTN_READ, OnUpdateBtnRead)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUSBDoc construction/destruction
CUSBDoc::CUSBDoc()
{
// TODO: add one-time construction code here
// nProcessMode=1; // 默认方式:数字显示
bCreateDevice=FALSE; // 表示没有创建设备对象
}
CUSBDoc::~CUSBDoc()
{
}
BOOL CUSBDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CUSBDoc serialization
void CUSBDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CUSBDoc diagnostics
#ifdef _DEBUG
void CUSBDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CUSBDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CUSBDoc commands
void CUSBDoc::OnBtnRead()
{
// TODO: Add your control notification handler code here
//CUSBApp *pApp=(CUSBApp *)AfxGetApp();
//CUSBDoc* ppUSBDoc=pApp->pDoc;
//CUSBDigitView* pDigitView;//=(CUSBDigitView*)(CWnd::FromHandle(ppUSBDoc->m_hWndDigit));//(CWnd::FromHandle(ppUSBDoc->m_hWndDigit));
//CUSBWaveView* pWaveView=(CUSBWaveView*)pUSBDoc-> //(CUSBWaveView*)(CWnd::FromHandle(pUSBDoc->m_hWndWave));
//CUSBDigitView* pDigitView=(CUSBDigitView*)(CWnd::FromHandle(m_hWndDigit));
//if (TD_Init(NULL)==FALSE)
// AfxMessageBox("设备初始化失败",NULL,MB_OK);
//else
//{
// AfxMessageBox("设备初始化成功",NULL,MB_OK);
//创建读数据线程 -----------------------------------------------------------------------------------------
m_ReadThread=AfxBeginThread(ReadDataThread,NULL, THREAD_PRIORITY_ABOVE_NORMAL, 0, CREATE_SUSPENDED);
m_ReadThread->m_bAutoDelete=false;
// 创建窗口绘制子线程
m_DrawWindowThread=AfxBeginThread(ProccessDataThread,NULL, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
m_DrawWindowThread->m_bAutoDelete=false;
// 创建数据采集与窗口绘制同步事件对象
hExitEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
//----------------------------------------------------------------------------------------------------
// CUSBDigitView* pDigitView=(CUSBDigitView*)(CWnd::FromHandle(m_hWndDigit));
/*/ 在设备开始运行时,禁止操作硬件参数-----------------------------------------------------------------
CUSBApp* pApp=(CUSBApp*)AfxGetApp();
CADFrame* pForm=pApp->pForm;
CComboBox* pFirstChannel=(CComboBox*)pForm->m_wndShowStatus.GetDlgItem(IDC_COMBO_FirstChannel);
pFirstChannel->EnableWindow(FALSE);
CComboBox* pLastChannel=(CComboBox*)pForm->m_wndShowStatus.GetDlgItem(IDC_COMBO_LastChannel);
pLastChannel->EnableWindow(FALSE);
CWnd* pFrequence=pForm->m_wndShowStatus.GetDlgItem(IDC_EDIT_Frequence);
pFrequence->EnableWindow(FALSE);
*///线程开始操作---------------------------------------------
bCreateDevice=TRUE;
bDeviceRun=TRUE; // 告之采集线程可以工作了
m_ReadThread->ResumeThread(); // 启动采集线程ReadThread
m_DrawWindowThread->ResumeThread();
// }
}
void CUSBDoc::OnBtnStop()
{
// TODO: Add your control notification handler code here
// TODO: Add your control notification handler code here
// Ezusb_StopDevice(pcDriverName);
bDeviceRun=FALSE;
CADFrame* pADFrame=((CUSBApp*)AfxGetApp())->pForm ; // 取得子帧窗口句柄
if(m_DrawWindowThread)
{
WaitForSingleObject(m_DrawWindowThread->m_hThread, 100); // 等待子线程停止
}
if(m_DrawWindowThread)
{
m_DrawWindowThread->ExitInstance();
m_DrawWindowThread=NULL;
}
if(m_ReadThread)
{
WaitForSingleObject(m_ReadThread->m_hThread, INFINITE); // 等待子线程停止
}
if(m_ReadThread)
{
m_ReadThread->ExitInstance();
m_ReadThread=NULL;
}
Beep(3000, 1); // 响铃通知线程已终止
}
UINT ReadDataThread(LPVOID lpParameter)
{
//CUSBApp *pApp=(CUSBApp *)AfxGetApp();
//CUSBDoc* pDoc=pApp->pDoc;
//CUSBDigitView* pDigitView=(CUSBDigitView*)(CWnd::FromHandle(pDoc->m_hWndDigit));
AfxMessageBox("11",NULL,MB_OK);
CUSBApp* pApp=(CUSBApp*)AfxGetApp();
CFrameWnd *pFram=(CFrameWnd*)(pApp->m_pMainWnd);
CUSBDigitView* pDigitView=(CUSBDigitView*)(pFram->GetActiveView());
CUSBDoc* ppUSBDoc=pDigitView->GetDocument();
ppUSBDoc->UpdateAllViews(pDigitView,1,NULL);
AfxMessageBox("22",NULL,MB_OK);
unsigned long nBytes = 0;
BOOL bFirst=TRUE;
int nRemainder; // 余数
BOOL ReadResult=FALSE;
nRemainder = 8192 % ( nChannelCount * 32 );
ReadSizeWords = 64;//8192 - nRemainder;
// 复位段索引号
ReadIndex = 0;
SegmentCounts = 0;
//bCreateDevice = TRUE;
while(bDeviceRun) // 循环采集AD数据
{
// Perform the BULK OUT
ReadResult = DeviceIoControl (hDevice,
IOCTL_EZUSB_BULK_READ,
&bulkControl,
sizeof(BULK_TRANSFER_CONTROL),
&ADBuffer[ReadIndex],
ReadSizeWords,
&nBytes,
NULL);
AfxMessageBox("read succes...",MB_ICONSTOP);
if(!ReadResult)
{
AfxMessageBox("读数据出错...",MB_ICONSTOP);
goto ExitReadThread;
}
// 发送事件,告诉绘制窗口线程,该批数据已采集完毕
// AfxMessageBox ("read success", NULL,MB_OK);
SetEvent(hExitEvent);
ReadIndex++;
if(ReadIndex==MAX_SEGMENT) ReadIndex=0;
}
ExitReadThread:
SetEvent(hExitEvent); // 发出设备成功退出消息,让关闭设备的线程得此消息
Beep(3000, 1); // 响铃通知用户,线程已终止
return TRUE;
}
UINT ProccessDataThread(PVOID hwnd) // 绘制数据线程
{
AfxMessageBox ("prosses begin", NULL,MB_OK);
CUSBApp* pApp=(CUSBApp*)AfxGetApp();
CFrameWnd *pFram=(CFrameWnd*)(pApp->m_pMainWnd);
CUSBDigitView* pDigitView=(CUSBDigitView*)(pFram->GetActiveView());
CUSBDoc* ppUSBDoc=pApp->pDoc;
CADFrame *pFrm=pApp->pForm ;
AfxMessageBox ("prosses begin 222", NULL,MB_OK);
// 在线程中取得视图指针
//AfxGetApp()
//CUSBDigitView* pDigitView= (CUSBDigitView*)(CWnd::FromHandle(ppUSBDoc->m_hWndDigit));
//CUSBWaveView* pWaveView=(CUSBWaveView*)(CWnd::FromHandle(ppUSBDoc->m_hWndWave));
nProcessMode=1;
BOOL bFirst=TRUE;
CurrentIndex=0;
int RedrawNum=0;
// while(bDeviceRun) // 循环采集AD数据
// {
RedrawNum++;
WaitForSingleObject(hExitEvent, INFINITE);
CurrentProgress++;
if(bDataProcessing==FALSE) DrawIndex=CurrentIndex; // 如果窗口已完成数据刷新,则置新的缓冲区索引号,使之绘制新缓冲区数据
switch(nProcessMode) // 数据处理
{
case 1: // 数字显示
// if( bFirst)
// {
AfxMessageBox ("prosses end11", NULL,MB_OK);
ppUSBDoc->UpdateAllViews(pDigitView, 1, NULL); // 传递1,要求重绘数字视图
AfxMessageBox ("prosses end", NULL,MB_OK);
bFirst=FALSE; // 置不是第一次采集的标志
// }
break;
case 2: // 波形显示
if( bFirst)
{
// ppUSBDoc->UpdateAllViews(pWaveView, 2, NULL); // 传递2,要求重绘波形视图
bFirst=FALSE;
}
break;
}
// }// 线程循环取样
return TRUE;
}
void CUSBDoc::OnDigitalShow()
{
// TODO: Add your control notification handler code here
nProcessMode=1;
/*
CUSBApp* pApp=(CUSBApp*)AfxGetApp();
CUSBDoc* ppUSBDoc=pApp->pDoc;
// 在线程中取得视图指针
CUSBDigitView* pDigitView=(CUSBDigitView*)(CWnd::FromHandle(m_hWndDigit));
*/
}
BOOLEAN bOpenDriver (HANDLE * phDeviceHandle, PCHAR devname)
{
char completeDeviceName[64] = "";
char pcMsg[64] = "";
strcat (completeDeviceName,
"\\\\.\\"
);
strcat (completeDeviceName,
devname
);
*phDeviceHandle = CreateFile( completeDeviceName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (*phDeviceHandle == INVALID_HANDLE_VALUE) {
return (FALSE);
} else {
return (TRUE);
} /*else*/
}//OpenDevice
BOOL TD_Init(LPVOID lpParameter)
{
unsigned long nBytes = 0;
UCHAR InterfaceInfo[1024];
if (bOpenDriver (&hDevice, pcDriverName) != TRUE)
{
AfxMessageBox("Driver Open Faile",NULL,MB_OK);
return FALSE ;
}
bulkControl.pipeNum=1;
bResult=DeviceIoControl (hDevice,
IOCTL_Ezusb_GET_PIPE_INFO,
NULL,
0,
&InterfaceInfo,
sizeof(InterfaceInfo),
&nBytes,
NULL);
if (hDevice != NULL)
{
//appoint the pipes of transfer
PUSBD_PIPE_INFORMATION pPipe = ((PUSBD_INTERFACE_INFORMATION) InterfaceInfo)->Pipes;
DWORD ioctl_val = IOCTL_EZUSB_BULK_WRITE;
//write data form the pipe of appoint
bResult = DeviceIoControl (hDevice, //device handle
ioctl_val, //control command
&bulkControl, //data buffer address of app transfer to driver app
sizeof (BULK_TRANSFER_CONTROL), //data byte number of app transfer to driver app
&m_Control, //data buffer of driver app callback
sizeof(m_Control), //data byte number of driver app callback
(unsigned long *)&nBytes,
NULL);
//judge the result of transfer
if(bResult == TRUE)
AfxMessageBox ("Write IOCTL passed", NULL,MB_OK);
else
{
AfxMessageBox ("Write IOCTL failed", NULL,MB_OK);
return FALSE;
}
}
else
{
AfxMessageBox("Device Open Faile",NULL,MB_OK);
return FALSE;
}
nChannelCount = m_Control.LastChannel - m_Control.FirstChannel + 1;
bDeviceRun=TRUE;
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -