问题描述
我正在用C ++在Visual Studio 2017上编写MFC应用程序。该应用程序是用于与USB设备通信的用户界面。在我的应用程序中,有一个Start
按钮。当用户按下Start
按钮时,USB设备开始传输数据,即开始发送数据包,直到我告诉它停止为止(我知道每个数据包的最大大小,但是我不知道有多少个数据包包我会得到)。我正在将流数据写入.CSV文件。
出于这个目的,我使用的是环路板(我仍然没有原始板)。 我正在尝试模拟一个真实的场景,所以我编写了一个while循环来发送和读取数据。
在我的应用程序中,我还添加了一个Stop
按钮,该按钮应该通过退出while循环来停止数据传输。
由于我无法在主对话框中执行此操作,因此我想创建一个子对话框来执行通信(它将执行while循环,以从循环板上发送和接收程序包),我将对此进行控制我的主对话框中的子窗口。
为此,我根据Barrnet Chou的建议创建了另一个类,如下所示:
header
文件:
#pragma once
#include <string>
#include "libusb.h"
class ChildDlg : public CDialog
{
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnButton1Clicked();
afx_msg void OnButton2Clicked();
};
cpp
文件:
#pragma once
#include "stdafx.h"
#include "ChildDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// constants deFinition for packet transfers
constexpr auto VID = 0x04B4;
constexpr auto PID = 0x00F0;
constexpr auto OUT_ENDPOINT_ID = 1;
constexpr auto IN_ENDPOINT_ID = 1;
constexpr auto MAX_PACKET_SIZE = 512;
constexpr int INIT_PACKET_SIZE = 4;
bool flag;
struct libusb_device_descriptor DeviceDescriptor;
libusb_context* context = NULL; //a libusb session
libusb_device_handle* DeviceHandle = NULL; //a device handle
BEGIN_MESSAGE_MAP(ChildDlg,CDialog)
ON_BN_CLICKED(IDC_BUTTON1,&ChildDlg::OnButton1Clicked)
ON_BN_CLICKED(IDC_BUTTON2,&ChildDlg::OnButton2Clicked)
END_MESSAGE_MAP()
void startCommunication() {
/* Prepare device for communication */
int RetVal = libusb_init(&context); //initialize a library session
if (RetVal < 0) {
// MessageBox(hwnd,"Error in libusb_init","Error:",MB_OK);
return;
}
else {
libusb_set_debug(context,3); //set verbosity level to 3,as suggested in the documentation
DeviceHandle = libusb_open_device_with_vid_pid(context,VID,PID); //these are vendorID and productID for Cypress FX3 specific device
}
if (DeviceHandle == NULL) {
// MessageBox(hwnd,"Cannot open device","Notice:",MB_OK);
return;
}
if (libusb_kernel_driver_active(DeviceHandle,0) == 1) { //find out if kernel driver is attached
//detach it
if (libusb_detach_kernel_driver(DeviceHandle,0) != 0) {
// MessageBox(hwnd,"Failed To Detach Kernel Driver!",MB_OK);
return;
}
}
RetVal = libusb_claim_interface(DeviceHandle,0); //claim interface 0 (the first) of device (desired device FX3 has only 1)
if (RetVal < 0) {
// MessageBox(hwnd,"Cannot Claim Interface",MB_OK);
return;
}
}
void endCommunication() {
libusb_close(DeviceHandle); //close the device we opened
libusb_exit(context); //needs to be called at the end
}
void ChildDlg::OnButton1Clicked() {
// Insert Start button handler here
flag = true;
startCommunication();
unsigned char* DataOut = new unsigned char[INIT_PACKET_SIZE]; //data to write
unsigned char* DataIn = new unsigned char[MAX_PACKET_SIZE]; //data to read
int BytesWritten; //used to find out how many bytes were written
int BytesRead; //used to find out how many bytes were read
DataOut[0] = 'a'; DataOut[1] = 'b'; DataOut[2] = 'c'; DataOut[3] = 'd'; //some dummy values
int RetVal = libusb_bulk_transfer(DeviceHandle,(OUT_ENDPOINT_ID | LIBUSB_ENDPOINT_OUT),DataOut,sizeof(DataOut),&BytesWritten,0);
if (RetVal == 0 && BytesWritten == sizeof(DataOut)) //we wrote the 4 bytes successfully
MessageBox("Writing Successful!","Notice:");
else
MessageBox("Write Error","Notice:");
int counter = 0;
while ((libusb_bulk_transfer(DeviceHandle,(IN_ENDPOINT_ID | LIBUSB_ENDPOINT_IN),DataIn,sizeof(DataIn),&BytesRead,200) == 0) && flag == true) {
std::string cleanData = std::to_string(DataIn[0]) + std::to_string(DataIn[1]) + std::to_string(DataIn[2]) + std::to_string(DataIn[3]);
MessageBox(cleanData.c_str(),"DATA IN ");
int RetVal = libusb_bulk_transfer(DeviceHandle,0); //the out endpoint of current device is 1
if (RetVal != 0 || BytesWritten != sizeof(DataOut)) MessageBox("Write Error","Error:");
}
while (libusb_bulk_transfer(DeviceHandle,200)) {
MessageBox("Garbage","Notice:");
}
delete[] DataOut; //delete the allocated memory for data
delete[] DataIn; //delete the allocated memory for data
endCommunication();
}
void ChildDlg::OnButton2Clicked() {
flag == false;
}
在主对话框中按下Start
按钮时,我将向子对话框发送一条消息,如下所示:
/* Start Handler */
void CEditableListControlDlg::OnBnClickedButton1()
{
// Todo: Add your control notification handler code here
dlg->SendDlgitemmessageA(IDC_BUTTON1,BM_CLICK);
return;
}
然后在主对话框中按下Stop
按钮时,我向子对话框发送了一条消息,如下所示:
/* Stop Handler */
void CEditableListControlDlg::OnBnClickedButton2()
{
// Todo: Add your control notification handler code here
dlg->SendDlgitemmessageA(IDC_BUTTON2,BM_CLICK);
}
我有两个问题,第一个是我不知道如何将消息从子对话框发送回主对话框。
第二个问题是我得到如下编译错误:
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON1': undeclared identifier
Severity Code Description Project File Line Suppression State
Error C2065 'IDC_BUTTON2': undeclared identifier
但是我不知道如何声明这些按钮?在主对话框中,我通过将这些按钮添加到Resource view
并单击两次(自动创建了适当的处理程序)来创建了这些按钮
是否可以将Resource view
添加到子对话框?如果没有,我应该如何创建这些标识符IDC_BUTTON1
和ICD_BUTTON2
?
谢谢。
解决方法
如果您想单击按钮并弹出窗口,建议您参考以下示例。
-
创建一个新对话框并添加一个新类(例如“ Test”)
-
添加以下代码
#include "Test.h" void CMFCApplication7Dlg::OnBnClickedButton1() { // TODO: Add your control notification handler code here Test *pDlg = new Test; pDlg->Create(IDD_DIALOG1,this); //IDD_DIALOG1 pDlg->ShowWindow(SW_SHOW); }
如果control
表示您想通过父窗口的控件来操作子窗口中的控件,则可以通过包含子窗口头文件来查看子窗口的所有控件。例如,如果子窗口是Test.h
,则将#include Test.h
添加到父窗口。