向 USB 设备发送指令

发布于 2025-01-08 03:10:33 字数 4158 浏览 0 评论 0原文

基本上,我试图使用提供的 DLL (mpusbapi.dll) 和头文件 (mpusbapi.h) 来控制 USB 设备(线性执行器控制板)。但是,我一生都无法弄清楚需要将哪些参数传递给 MPUSBWrite()

特别是MPUSBWrite()的第二个、第三个和第四个参数。第一个参数显然是 MPUSBOpen() 返回的句柄,最后一个参数是超时(以毫秒为单位)。

我有预感它与LAC advance config的第6页有关。

非常感谢任何帮助。谢谢!

mpusbapi.h 文件

#ifndef _MPUSBAPI_H_
#define _MPUSBAPI_H_

#define MPUSB_FAIL                  0
#define MPUSB_SUCCESS               1

#define MP_WRITE                    0
#define MP_READ                     1

// MAX_NUM_MPUSB_DEV is an abstract limitation.
// It is very unlikely that a computer system will have more
// then 127 USB devices attached to it. (single or multiple USB hosts)
#define MAX_NUM_MPUSB_DEV           127

DWORD (*MPUSBGetDLLVersion)(void);

DWORD (*MPUSBGetDeviceCount)(PCHAR pVID_PID);

HANDLE (*MPUSBOpen)(DWORD instance,         // Input
             PCHAR pVID_PID,            // Input
             PCHAR pEP,                 // Input
             DWORD dwDir,               // Input
             DWORD dwReserved);         // Input <Future Use>

DWORD (*MPUSBRead)(HANDLE handle,           // Input
            PVOID pData,                // Output
            DWORD dwLen,                // Input
            PDWORD pLength,             // Output
            DWORD dwMilliseconds);      // Input

DWORD (*MPUSBWrite)(HANDLE handle,          // Input
             PVOID pData,               // Input
             DWORD dwLen,               // Input
             PDWORD pLength,            // Output
             DWORD dwMilliseconds);     // Input

DWORD (*MPUSBReadInt)(HANDLE handle,        // Input
               PVOID pData,             // Output
               DWORD dwLen,             // Input
               PDWORD pLength,          // Output
               DWORD dwMilliseconds);   // Input

BOOL (*MPUSBClose)(HANDLE handle);

#endif

LACTesting.cpp 文件

#include <windows.h>     // This is a windows header file. The functions I mentioned above are declared here
#include "mpusbapi.h"    // This is the header file supplied. It declares the function prototypes that are defined in the DLL
#include <iostream>
#include "ioctls.h"

using namespace std;

int main(int argc, char* argv)
{
// Try to load the library
HMODULE mpbusDLL = NULL;
mpbusDLL = LoadLibrary(L"mpusbapi.dll");

if (mpbusDLL != NULL) {
    // If the library could be loaded, then load the functions using GetProcAddress()

    // Load the function 'MPUSBOpen' from the DLL
    MPUSBOpen = (HANDLE(*)(DWORD, PCHAR, PCHAR, DWORD, DWORD)) GetProcAddress(mpbusDLL, "_MPUSBOpen");
    MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBOpen");
    MPUSBGetDLLVersion=(DWORD(*)(void))GetProcAddress(mpbusDLL,"_MPUSBGetDLLVersion");
    MPUSBGetDeviceCount=(DWORD(*)(PCHAR))GetProcAddress(mpbusDLL,"_MPUSBGetDeviceCount");
    MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBWrite");
    MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBRead");
    MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBReadInt");
    MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(mpbusDLL,"_MPUSBClose");
}
//If the DLL didn't load, let me know!
else cout<<"DLL didn't load"<<endl;

//Declarations
HANDLE LACHandle;
PCHAR pipeName=MCHPUSB_PIPE_NAME;// intializes pipeName to "\\MCHP_EP"
PCHAR VidPid="vid_04d8&pid_fc5f";

LACHandle=MPUSBOpen(0,VidPid,pipeName,1,0);//open device connection
                //Not sure if I pass in the correct arguements for MBUSBOpen. VidPid and pipeName are correct.

cout<<"Device ID "<<VidPid<<"is open with "<<MPUSBGetDeviceCount(VidPid)<<" device(s)."<<endl;
//sMPUSBWrite(LACHandle,,3,,1000); <- this is where I am having issues.
                                    //I can't figure out how to use MPUSBWrite
MPUSBClose(LACHandle);// closes device connection

}

Basically, I am trying to control a USB device (a linear actuator control board) using the supplied DLL (mpusbapi.dll) and header file (mpusbapi.h). However, I can't for the life of me figure out which parameters I need to pass into MPUSBWrite().

In particular, the 2nd, 3rd and 4thparameters of MPUSBWrite(). The first parameter is clearly the handle that is returned by MPUSBOpen() and the last parameter is a timeout in ms.

I have a hunch it has something to do with page 6 of LAC advance config.

Any help is GREATLY appreciated. Thanks!

mpusbapi.h file

#ifndef _MPUSBAPI_H_
#define _MPUSBAPI_H_

#define MPUSB_FAIL                  0
#define MPUSB_SUCCESS               1

#define MP_WRITE                    0
#define MP_READ                     1

// MAX_NUM_MPUSB_DEV is an abstract limitation.
// It is very unlikely that a computer system will have more
// then 127 USB devices attached to it. (single or multiple USB hosts)
#define MAX_NUM_MPUSB_DEV           127

DWORD (*MPUSBGetDLLVersion)(void);

DWORD (*MPUSBGetDeviceCount)(PCHAR pVID_PID);

HANDLE (*MPUSBOpen)(DWORD instance,         // Input
             PCHAR pVID_PID,            // Input
             PCHAR pEP,                 // Input
             DWORD dwDir,               // Input
             DWORD dwReserved);         // Input <Future Use>

DWORD (*MPUSBRead)(HANDLE handle,           // Input
            PVOID pData,                // Output
            DWORD dwLen,                // Input
            PDWORD pLength,             // Output
            DWORD dwMilliseconds);      // Input

DWORD (*MPUSBWrite)(HANDLE handle,          // Input
             PVOID pData,               // Input
             DWORD dwLen,               // Input
             PDWORD pLength,            // Output
             DWORD dwMilliseconds);     // Input

DWORD (*MPUSBReadInt)(HANDLE handle,        // Input
               PVOID pData,             // Output
               DWORD dwLen,             // Input
               PDWORD pLength,          // Output
               DWORD dwMilliseconds);   // Input

BOOL (*MPUSBClose)(HANDLE handle);

#endif

LACTesting.cpp file

#include <windows.h>     // This is a windows header file. The functions I mentioned above are declared here
#include "mpusbapi.h"    // This is the header file supplied. It declares the function prototypes that are defined in the DLL
#include <iostream>
#include "ioctls.h"

using namespace std;

int main(int argc, char* argv)
{
// Try to load the library
HMODULE mpbusDLL = NULL;
mpbusDLL = LoadLibrary(L"mpusbapi.dll");

if (mpbusDLL != NULL) {
    // If the library could be loaded, then load the functions using GetProcAddress()

    // Load the function 'MPUSBOpen' from the DLL
    MPUSBOpen = (HANDLE(*)(DWORD, PCHAR, PCHAR, DWORD, DWORD)) GetProcAddress(mpbusDLL, "_MPUSBOpen");
    MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBOpen");
    MPUSBGetDLLVersion=(DWORD(*)(void))GetProcAddress(mpbusDLL,"_MPUSBGetDLLVersion");
    MPUSBGetDeviceCount=(DWORD(*)(PCHAR))GetProcAddress(mpbusDLL,"_MPUSBGetDeviceCount");
    MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBWrite");
    MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBRead");
    MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))GetProcAddress(mpbusDLL,"_MPUSBReadInt");
    MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(mpbusDLL,"_MPUSBClose");
}
//If the DLL didn't load, let me know!
else cout<<"DLL didn't load"<<endl;

//Declarations
HANDLE LACHandle;
PCHAR pipeName=MCHPUSB_PIPE_NAME;// intializes pipeName to "\\MCHP_EP"
PCHAR VidPid="vid_04d8&pid_fc5f";

LACHandle=MPUSBOpen(0,VidPid,pipeName,1,0);//open device connection
                //Not sure if I pass in the correct arguements for MBUSBOpen. VidPid and pipeName are correct.

cout<<"Device ID "<<VidPid<<"is open with "<<MPUSBGetDeviceCount(VidPid)<<" device(s)."<<endl;
//sMPUSBWrite(LACHandle,,3,,1000); <- this is where I am having issues.
                                    //I can't figure out how to use MPUSBWrite
MPUSBClose(LACHandle);// closes device connection

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薆情海 2025-01-15 03:10:33

我认为您应该看一下

Win32 数据类型

然后研究 Windows 系统编程示例,那么你就会清楚地理解其中的逻辑。

对于你的问题,当我看到代码时我可以猜测

         // yourData which want to write
         PVOID pData
         // sizeof(yourData)  google "sizeof"
         DWORD dwLen
         /* I have no idea about this, but its output you can see it 
         after running function */               
         PDWORD pLength           

希望这会有所帮助

I think you should take a look at

Win32 Data Types

then work on Windows System Programming samples, then you will clearly understand the logic.

To your question, as i see the code i can guess

         // yourData which want to write
         PVOID pData
         // sizeof(yourData)  google "sizeof"
         DWORD dwLen
         /* I have no idea about this, but its output you can see it 
         after running function */               
         PDWORD pLength           

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文