📄 u2sdlg.cpp
字号:
// u2sDlg.cpp : implementation file
//
#include "stdafx.h"
#include "u2s.h"
#include "u2sDlg.h"
#include "devioctl.h"
#include "setupapi.h"
#include "ioctl.h"
#include "device.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CString g_strCaption_onoff;
OVERLAPPED ovInteral;
LPOVERLAPPED lpov;
UINT InterruptIn(void * pParam)
{
LONG len,k;
BYTE rxdata[18];
CString strtemp;
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
HANDLE hFile;
CU2sDlg * u2sDlg;
threadParam=(struct CThreadParam *)pParam;
u2sDlg = threadParam->u2sDlg;
threadParam->bUpdate=FALSE;
threadParam->bInThread=TRUE;
threadParam->pcIoBuffer = ( char *)malloc (threadParam->uiLength+16);
do{
u2sDlg->m_ctrlOnoff.GetWindowText(g_strCaption_onoff);
if(g_strCaption_onoff=="停止设备"){
hFile=open_file(threadParam->pipe_name);
if(hFile!=INVALID_HANDLE_VALUE){
bResult=ReadFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
if(bResult==TRUE)
{
if(nBytes>0)
{
len=nBytes;
memcpy(rxdata,threadParam->pcIoBuffer,len);
for(k=0;k<len;k++)
{
BYTE bt=*(char*)(rxdata+k);
strtemp.Format("%c",bt);
u2sDlg->m_strRXDataENP1+=strtemp;
}
}
threadParam->bUpdate=TRUE;
}
CloseHandle(hFile);
}else {
u2sDlg->MessageBox("open pipe fail!");
threadParam->bInThread=FALSE;
}
}
}while(threadParam->bInThread==TRUE);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
UINT GenericOut(void * pParam)
{
char temp [30],cc[30];
BOOLEAN bResult = FALSE;
ULONG nBytes = 0;
struct CThreadParam * threadParam;
HANDLE hFile;
CU2sDlg * u2sDlg;
threadParam=(struct CThreadParam *)pParam;
threadParam->bInThread = FALSE;
u2sDlg = threadParam->u2sDlg;
hFile = open_file(threadParam->pipe_name);
if(hFile == INVALID_HANDLE_VALUE) {
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
strcpy(temp, "无法打开");
strcat(temp, threadParam->pipe_name);
strcat(temp, "管道.");
u2sDlg->MessageBox((LPCSTR)temp, "错误提示!",MB_OK);
return 0;
}
if (hFile != INVALID_HANDLE_VALUE) {
threadParam->bInThread = TRUE;
bResult = WriteFile(hFile,
threadParam->pcIoBuffer,
threadParam->uiLength,
&nBytes,
NULL);
if (bResult != TRUE) {
DeviceIoControl(hFile,
IOCTL_D12_RESET_PIPE,
0,
0,
0,
0,
&nBytes,
NULL);
wsprintf (cc, "写 ");
strcat( cc, threadParam->pipe_name);
strcat( cc, " 失败! 请稍候再试!");
u2sDlg->MessageBox((LPCSTR)cc, "错误提示!");
}
}
CloseHandle(hFile);
free(threadParam->pcIoBuffer);
threadParam->pcIoBuffer = NULL;
threadParam->bInThread = FALSE;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CU2sDlg dialog
CU2sDlg::CU2sDlg(CWnd* pParent /*=NULL*/)
: CDialog(CU2sDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CU2sDlg)
m_strRXDataENP1 = _T("");
m_strTXData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CU2sDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CU2sDlg)
DDX_Control(pDX, IDC_BUTTON_START, m_ctrlOnoff);
DDX_Control(pDX, IDC_BUTTON_SEND, m_ctrlTXData);
DDX_Control(pDX, IDC_BUTTON_CLEARS, m_ctrlClearTxd);
DDX_Control(pDX, IDC_BUTTON_CLEAR_ENP1, m_ctrlClearEnp1);
DDX_Control(pDX, IDC_EDIT_RXD_ENP1, m_ctrlRXDataENP1);
DDX_Text(pDX, IDC_EDIT_RXD_ENP1, m_strRXDataENP1);
DDX_Text(pDX, IDC_EDIT_TXD, m_strTXData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CU2sDlg, CDialog)
//{{AFX_MSG_MAP(CU2sDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_CLEARS, OnButtonClears)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_CLEAR_ENP1, OnButtonClearEnp1)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CU2sDlg message handlers
BOOL CU2sDlg::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
// m_ctrlClearEnp1.EnableWindow(FALSE);
// m_ctrlClearTxd.EnableWindow(FALSE);
m_ctrlTXData.EnableWindow(FALSE);
m_strTXData="";
m_strRXDataENP1="";
UpdateData(FALSE);
SetTimer(1, 1, 0);
m_InterruptIn.u2sDlg = this;
m_InterruptIn.uiLength = 16;
strcpy(m_InterruptIn.pipe_name, (LPCSTR)"PIPE00");
CWinThread * wt = AfxBeginThread(
InterruptIn, // thread function
&m_InterruptIn); // argument to thread function
m_InterruptIn.hThread = wt->m_hThread;
return TRUE; // return TRUE unless you set the focus to a control
}
void CU2sDlg::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 CU2sDlg::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 CU2sDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CU2sDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
m_ctrlOnoff.GetWindowText(g_strCaption_onoff);
if(g_strCaption_onoff=="启动设备"){
hD12=open_dev();
if(hD12!=INVALID_HANDLE_VALUE)
{
memset(&ovInteral,0,sizeof(OVERLAPPED));
ovInteral.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
if(ovInteral.hEvent==NULL)
{
return;
}
lpov=&ovInteral;
//MessageBox("设备成功打开!");
m_ctrlOnoff.SetWindowText("停止设备");
// m_ctrlClearEnp1.EnableWindow(TRUE);
// m_ctrlClearTxd.EnableWindow(TRUE);
m_ctrlTXData.EnableWindow(TRUE);
}
else
MessageBox("无法连接设备,请检查设备状况!","查找设备失败",MB_OK);
}
else
{
KillTimer(1);
CloseHandle(hD12);
m_ctrlOnoff.SetWindowText("启动设备");
// m_ctrlClearEnp1.EnableWindow(FALSE);
// m_ctrlClearTxd.EnableWindow(FALSE);
m_ctrlTXData.EnableWindow(FALSE);
}
}
void CU2sDlg::OnOK()
{
// TODO: Add extra validation here
CloseHandle(hD12);
CDialog::OnOK();
}
void CU2sDlg::OnButtonClears()
{
// TODO: Add your control notification handler code here
m_strTXData="";
this->SetDlgItemText(IDC_EDIT_TXD,"");
}
void CU2sDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
if( m_GenericOut.bInThread == TRUE) {
return;
}
char * data_buf;
int len;
len=m_strTXData.GetLength();
data_buf=(char *)malloc(len);
memcpy(data_buf,m_strTXData.GetBuffer(0),len);
if(len>64){
MessageBox("暂时无法发送大于64字节的数据!","抱歉!",MB_OK);
}
else if(len>16){
m_GenericOut.u2sDlg = this;
m_GenericOut.uiLength = len;
m_GenericOut.pcIoBuffer = data_buf;
strcpy(m_GenericOut.pipe_name, (LPCSTR)"PIPE03");
CWinThread * wt = AfxBeginThread(
GenericOut,
&m_GenericOut);
m_GenericOut.hThread = wt->m_hThread;
}else if(len<=16&&len){
m_GenericOut.u2sDlg = this;
m_GenericOut.uiLength = len;
m_GenericOut.pcIoBuffer = data_buf;
strcpy(m_GenericOut.pipe_name, (LPCSTR)"PIPE01");
CWinThread * wt = AfxBeginThread(
GenericOut,
&m_GenericOut);
m_GenericOut.hThread = wt->m_hThread;
}
else MessageBox("请输入数据!","提示",MB_OK);
}
void CU2sDlg::OnButtonClearEnp1()
{
// TODO: Add your control notification handler code here
m_strRXDataENP1="";
this->SetDlgItemText(IDC_EDIT_RXD_ENP1,"");
}
BOOL CU2sDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
KillTimer(1);
return CDialog::DestroyWindow();
}
void CU2sDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(m_InterruptIn.bUpdate==TRUE)
{
m_InterruptIn.bUpdate=FALSE;
UpdateData(FALSE);
m_ctrlRXDataENP1.LineScroll(m_ctrlRXDataENP1.GetLineCount(),0);
}
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -