📄 xugy03dlg.cpp
字号:
// Xugy03Dlg.cpp : implementation file
//
#include "stdafx.h"
#include <mmsystem.h>
#include "Xugy03.h"
#include "Xugy03Dlg.h"
#include <devioctl.h>
#include "process.h"
#include "rwbulk.h"
#include "ioctl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
UINT InterruptIn(void * pParam) //测试板按键处理线程
{
char cc[80], c;
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam *threadParam;
HANDLE hFile, hDevice;
CXugy03Dlg * testDlg;
IO_BLOCK ioBlock;
threadParam = (struct CThreadParam *)pParam;
threadParam->bInThread = TRUE;
testDlg = threadParam->testDlg;
threadParam->pcIoBuffer = (unsigned char *) malloc
(threadParam->uiLength + 16);
if(threadParam->pcIoBuffer == NULL)
{
testDlg->MessageBox("内存不够", "InterruptIn:", MB_ICONSTOP);
testDlg->EndDialog(0);
return 0;
}
do
{
do
{
Sleep(100);
hFile = open_file(threadParam->pipe_name);
if(hFile != INVALID_HANDLE_VALUE)
{
hDevice = open_dev();
ioBlock.uOffset = 0;
ioBlock.uLength = 1;
ioBlock.pbyData = (PUCHAR)&c;
ioBlock.uIndex = GET_FIRMWARE_VERSION;
bResult = DeviceIoControl(hDevice,
IOCTL_READ_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
(PVOID)&c,
1,
&nBytes,
NULL);
if(!bResult)
{
CloseHandle(hFile);
CloseHandle(hDevice);
threadParam->bInThread = FALSE;
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
wsprintf(cc, "固件不支持");
testDlg->MessageBox((LPCSTR)cc, "InterruptIn:", MB_ICONSTOP);
testDlg->EndDialog(0);
return 0;
}
}
}while((hFile == INVALID_HANDLE_VALUE) && threadParam->bInThread);
threadParam->ulData[0] |= D12_DRIVERLOAD;
threadParam->ulData[1] = c;
threadParam->bUpdate = TRUE;
if(hFile != INVALID_HANDLE_VALUE)
{
while(threadParam->bInThread)
{
bResult = ReadFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
if(bResult)
{
threadParam->ulData[0] |= D12_KEYSTATUS;
threadParam->ulData[2] = *(threadParam->pcIoBuffer);
threadParam->bUpdate = TRUE;
}
else
{
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0, 0, 0, 0,
&nBytes,
NULL);
threadParam->ulData[0] |= D12_DRIVERUNLOAD;
threadParam->bUpdate = TRUE;
break;
}
};//end while(threadParam->bInThread)
}
}while(threadParam->bInThread);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
UINT GenericOut(void * pParam) //通用输出线程
{
char temp[80],cc[80];
BOOLEAN bResult = FALSE;
ULONG nBytes =0;
struct CThreadParam *threadParam;
HANDLE hFile;
CXugy03Dlg *testDlg;
threadParam = (struct CThreadParam *)pParam;
testDlg = threadParam->testDlg;
threadParam->bInThread =FALSE;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE)
{
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
strcpy(temp,"驱动打开失败");
strcat(temp,threadParam->pipe_name);
testDlg->MessageBox(temp,"GenericOut",MB_OK);
return 0;
}
if(hFile != INVALID_HANDLE_VALUE)
{
threadParam->bInThread = TRUE;
bResult = WriteFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
if (!bResult)
{
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0, 0, 0, 0,
&nBytes,
NULL);
wsprintf(cc,"写入失败");
strcat(cc, threadParam->pipe_name);
MessageBox(NULL, cc, "GenericOut:", MB_OK);
}
}
CloseHandle(hFile);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
UINT ReadPipe(void * pParam) //扫描仪模式线程
{
char temp[80];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
CXugy03Dlg * testDlg;
HANDLE hFile,hDevice;
DWORD time0, time1;
IO_BLOCK ioBlock;
IO_REQUEST ioRequest;
threadParam = (struct CThreadParam *) pParam;
threadParam->bInThread = FALSE;
testDlg = threadParam->testDlg;
threadParam ->bUpdate = FALSE;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE)
{
strcpy(temp,"open_file打开失败 ");
strcat(temp,threadParam->pipe_name);
testDlg->MessageBox(temp, " ReadPipe err notice:",MB_OK);
testDlg->m_InStartBtn.EnableWindow(TRUE);
testDlg->m_InStopBtn.EnableWindow(FALSE);
return 0;
}
hDevice = open_dev();
if (hDevice == INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
strcpy(temp,"open_dev打开失败 ");
strcat(temp,threadParam->pipe_name);
testDlg->MessageBox(temp, " ReadPipe err notice:",MB_OK);
testDlg->m_InStartBtn.EnableWindow(TRUE);
testDlg->m_InStopBtn.EnableWindow(FALSE);
return 0;
}
threadParam->pcIoBuffer = (unsigned char *)
malloc (threadParam->uiLength+16);
if(threadParam->pcIoBuffer == NULL)
{
testDlg->MessageBox("内存不够", "ReadPipe:", MB_ICONSTOP);
testDlg->EndDialog(0);
return 0;
}
threadParam->ulCount =0;
threadParam->total_time = 0;
threadParam->max_rate = 0;
threadParam->cur_rate = 0;
if(hFile != INVALID_HANDLE_VALUE)
{
threadParam->bInThread = TRUE;
do
{
ioRequest.uAddressL = 0;
ioRequest.bAddressH = 0;
ioRequest.uSize = threadParam->uiLength;
ioRequest.bCommand = 0x81; //start read
ioBlock.uOffset = 0;
ioBlock.uLength = sizeof(IO_REQUEST);
ioBlock.pbyData = (PUCHAR)&ioRequest;
ioBlock.uIndex = SETUP_DMA_REQUEST;
bResult = DeviceIoControl(hDevice,
IOCTL_WRITE_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
NULL,
0,
&nBytes,
NULL);
if(!bResult)
{
threadParam->bInThread = FALSE;
strcpy(temp,"DeviceIoControl 失败 ");
strcat(temp,threadParam->pipe_name);
testDlg->MessageBox(temp, " ReadPipe err notice:",MB_OK);
break;
}
time0 = timeGetTime();
bResult = ReadFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
time1 = timeGetTime();
if(!bResult)
{
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0, 0, 0, 0,
&nBytes,
NULL);
threadParam->bInThread = FALSE;
strcpy(temp,"ReadFile失败 ");
strcat(temp,threadParam->pipe_name);
testDlg->MessageBox(temp, " ReadPipe err notice:",MB_OK);
break;
}
else
{
threadParam->ulCount += nBytes;
if ((time1-time0-1) > 0)
{
threadParam->cur_rate = ((nBytes * 1000)
/(time1 - time0 - 1));
if (threadParam->cur_rate > threadParam->max_rate)
threadParam->max_rate = threadParam->cur_rate;
threadParam->total_time += time1-time0-1;
threadParam->bUpdate = TRUE;
}
else
{
if(nBytes > 0)
{
threadParam->cur_rate = (nBytes * 1000) / 1;
if (threadParam->cur_rate > threadParam->max_rate)
threadParam->max_rate = threadParam->cur_rate;
threadParam->total_time ++;
threadParam->bUpdate = TRUE;
}
}
}//end if(!bRequest),else
} while(threadParam->bInThread == TRUE);
} //end if(hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
CloseHandle(hDevice);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
testDlg->m_InStartBtn.EnableWindow(TRUE);
testDlg->m_InStopBtn.EnableWindow(FALSE);
return 0;
}
UINT WritePipe(void * pParam)
{
// MessageBox(NULL, "这是WritePipe", "Woo提示:", 0);
char temp [80],cc[80];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0, ulCount = 0, index, cnt, seq;
struct CThreadParam * threadParam;
CXugy03Dlg *testDlg;
unsigned char rNum;
HANDLE hFile,hDevice;
DWORD time0,time1;
IO_BLOCK ioBlock;
IO_REQUEST ioRequest;
threadParam = (struct CThreadParam *)pParam;
threadParam->bInThread = FALSE;
testDlg = threadParam->testDlg;
threadParam->bUpdate = FALSE;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE)
{
strcpy(temp, "open_file 错误! ");
strcat(temp, threadParam->pipe_name);
testDlg->MessageBox((LPCSTR)temp, "WritePipe");
testDlg->m_OutStartBtn.EnableWindow(TRUE);
testDlg->m_OutStopBtn.EnableWindow(FALSE);
return 0;
}
hDevice = open_dev();
if(hDevice==INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
strcpy(temp, "open_dev 错误! ");
strcat(temp, threadParam->pipe_name);
testDlg->MessageBox((LPCSTR)temp, "WritePipe");
testDlg->m_OutStartBtn.EnableWindow(TRUE);
testDlg->m_OutStopBtn.EnableWindow(FALSE);
return 0;
}
threadParam->pcIoBuffer = (unsigned char *)
malloc (threadParam->uiLength + 16);
if(NULL == threadParam->pcIoBuffer)
{
testDlg->MessageBox("内存不够", "WritePipe:", MB_ICONSTOP);
testDlg->EndDialog(0);
return 0;
}
threadParam->ulCount = 0;
threadParam->total_time = 0;
threadParam->cur_rate = 0;
threadParam->max_rate = 0;
if(hFile != INVALID_HANDLE_VALUE)
{
int buffLen;
buffLen = threadParam->uiLength - sizeof(ulCount);
threadParam->bInThread = TRUE;
if(threadParam->uiLength == 0)
{
}
else
{
do
{
if(buffLen>0)
{
rNum = 0;
for(index = 0; index < threadParam->uiLength; index++)
*(threadParam->pcIoBuffer + index) = rNum++;
cnt = threadParam->uiLength / 64;
for(index = 0; index<cnt;index++)
{
seq = ulCount * cnt + index;
memcpy(threadParam->pcIoBuffer + index*64,
&seq, min(sizeof(ulCount), threadParam->uiLength));
}
}
ioRequest.uAddressL = 0;
ioRequest.bAddressH = 0;
ioRequest.uSize = threadParam->uiLength;
ioRequest.bCommand = 0x80;
ioBlock.uOffset = 0;
ioBlock.uLength = sizeof(IO_REQUEST);
ioBlock.pbyData = (PUCHAR)&ioRequest;
ioBlock.uIndex = SETUP_DMA_REQUEST;
bResult = DeviceIoControl(hDevice,
IOCTL_WRITE_REGISTERS,
(PVOID)&ioBlock,
sizeof(IO_BLOCK),
NULL,
0,
&nBytes,
NULL);
if(!bResult)
{
threadParam->bInThread = FALSE;
wsprintf(cc,threadParam->pipe_name);
strcat(cc," DeviceIoControl请求下传数据失败");
testDlg->MessageBox((LPCSTR)cc,"WritePipe",MB_OK);
break;
}
time0 = timeGetTime();
bResult = WriteFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
time1 = timeGetTime();
if(!bResult)
{
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0, 0, 0, 0,
&nBytes,
NULL);
threadParam->bInThread = FALSE;
wsprintf(cc,threadParam->pipe_name);
strcat(cc," WriteFile请求下传数据失败");
testDlg->MessageBox((LPCSTR)cc,"WritePipe",MB_OK);
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -