📄 hywdlg.cpp
字号:
// hywDlg.cpp : implementation file
//
#include "stdafx.h"
#include "hyw.h"
#include "hywDlg.h"
#include "string.h"
#include "windows.h"
#include "winioctl.h"
#include "devintf.h"
#ifndef __JJT_JT18Aioctl__h_
#define __JJT_JT18Aioctl__h_
#define JJT_JT18A_IOCTL_CONTROL CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define JJT_JT18A_IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define JJT_JT18A_IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define JJT_JT18ADevice_CLASS_GUID \
{ 0x5f36fdba, 0x7adf, 0x4027, { 0x8e, 0xfc, 0xc3, 0xdf, 0x23, 0x77, 0xdb, 0xc8 } }
#define IOCTL_INBUF_SIZE 512
#define IOCTL_OUTBUF_SIZE 512
GUID ClassGuid=JJT_JT18ADevice_CLASS_GUID;
CFile file;
int filenum=1;
CHAR bufInput[IOCTL_INBUF_SIZE]; // Input to device
CHAR bufOutput[IOCTL_OUTBUF_SIZE]; // Output from device
ULONG nOutput; // Count written to bufOutput
HANDLE hDevice;
HANDLE OpenByInterface(
GUID* pClassGuid, // points to the GUID that identifies the interface class
DWORD instance, // specifies which instance of the enumerated devices to open
PDWORD pError // address of variable to receive error status
)
{
HANDLE hDev;
CDeviceInterfaceClass DevClass(pClassGuid, pError);
if (*pError != ERROR_SUCCESS)
return INVALID_HANDLE_VALUE;
CDeviceInterface DevInterface(&DevClass, instance, pError);
if (*pError != ERROR_SUCCESS)
return INVALID_HANDLE_VALUE;
hDev = CreateFile(
DevInterface.DevicePath(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hDev == INVALID_HANDLE_VALUE)
*pError = GetLastError();
return hDev;
}
/////////////////////////////////////////////////////////////////////////////
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHywDlg dialog
CHywDlg::CHywDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHywDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHywDlg)
m_edit1 = _T("");
m_edit2 = _T("");
m_edit3 = _T("");
m_edit4 = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHywDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHywDlg)
DDX_Text(pDX, IDC_EDIT1, m_edit1);
DDX_Text(pDX, IDC_EDIT2, m_edit2);
DDX_Text(pDX, IDC_EDIT3, m_edit3);
DDX_Text(pDX, IDC_EDIT4, m_edit4);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHywDlg, CDialog)
//{{AFX_MSG_MAP(CHywDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHywDlg message handlers
BOOL CHywDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
void CHywDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CHywDlg::OnPaint()
{
UpdateData(FALSE);
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 CHywDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHywDlg::OnButton1()
{
write(1);
read();
filenum=bufInput[0];
m_edit3.Format("%d",filenum);
UpdateData(FALSE);
mess="读数据启动";
m_edit2.Format("%s",mess);
UpdateData(FALSE);
int code=1;
readthread=AfxBeginThread(readdatat,&code);
}
void CHywDlg::OnClose()
{closeusb();
if(file!=NULL)file.Close();
CDialog::OnClose();
}
void CHywDlg::OnButton2()
{
io();
}
void CHywDlg::OnButton3()
{
mess="读数据启动";
m_edit2.Format("%s",mess);
UpdateData(FALSE);
int code=1;
readthread=AfxBeginThread(readdatat,&code);
}
void CHywDlg::OnButton4()
{
}
CHywDlg::openusb()
{
DWORD Error;
hDevice = OpenByInterface( &ClassGuid, 0, &Error);
if (hDevice == INVALID_HANDLE_VALUE)
{
MessageBox("ERROR opening device: returned from CreateFile\n");
}
else
{
MessageBox("Device found, handle open.\n");
}
}
CHywDlg::closeusb()
{
if (hDevice != INVALID_HANDLE_VALUE)
{
// Close the handle to the driver
if (!CloseHandle(hDevice))
{
MessageBox("ERROR: CloseHandle returns .\n" );
}
hDevice = INVALID_HANDLE_VALUE;
}
}
BOOL CHywDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
return CDialog::Create(IDD, pParentWnd);
}
int CHywDlg::DoModal()
{
openusb();file.Open("comm.dat",CFile::modeWrite|CFile::modeCreate );
return CDialog::DoModal();
}
void CHywDlg::write(int n)
{
char *buf;
ULONG nWritten;
int i;
int j;
buf = (char *) malloc(n);
if (buf == NULL)
{
MessageBox("Failed to allocate buffer for write");
PostQuitMessage(0);
}
j = (n % 26);
// load buffer with dummy data (abcdefg...)
for (i=0; i<n; i++, j=(j + 1)%26)
{
buf[i] = 'c' + j;
}
// Write data to driver
MessageBox("Writing to device - ");
WriteFile(hDevice, buf, n, &nWritten, NULL);
MessageBox("%d bytes written to device (%d attempted).\n");
free(buf);
}
CHywDlg::io()
{
MessageBox("Issuing Ioctl to device - ");
if (!DeviceIoControl(hDevice,
JJT_JT18A_IOCTL_WRITE,
bufInput,
IOCTL_INBUF_SIZE,
bufOutput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
MessageBox("ERROR: DeviceIoControl returns %0x.");
PostQuitMessage(1);
}
}
CHywDlg::control()
{
if (!DeviceIoControl(hDevice,
JJT_JT18A_IOCTL_CONTROL,
bufInput,
IOCTL_INBUF_SIZE,
bufOutput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
MessageBox("ERROR: DeviceIoControl returns %0x.");
PostQuitMessage(1);
}
}
CHywDlg::read()
{
if (!DeviceIoControl(hDevice,
JJT_JT18A_IOCTL_READ,
bufInput,
IOCTL_INBUF_SIZE,
bufInput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
MessageBox("ERROR: DeviceIoControl returns %0x.");
PostQuitMessage(1);
}
file.SeekToEnd();
file.Write(bufInput,nOutput);
if (!DeviceIoControl(hDevice,
JJT_JT18A_IOCTL_READ,
bufInput,
IOCTL_INBUF_SIZE,
bufInput,
IOCTL_OUTBUF_SIZE,
&nOutput,
NULL)
)
{
MessageBox("ERROR: DeviceIoControl returns %0x.");
PostQuitMessage(1);
}
}
UINT readdatat(LPVOID pParam)
{
CHywDlg mdlg =(CHywDlg*)pParam ;
for (int i=0;i<filenum;i++)
{
mdlg.control();
mdlg.read();
}
mdlg.mess="读数据结束";
mdlg.m_edit2.Format("%s",mdlg.mess);
AfxMessageBox(mdlg.mess);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -