C++ DLL 函数在 C# 中似乎不起作用

发布于 2024-11-11 06:12:47 字数 2417 浏览 0 评论 0原文

我使用 C# 和bird.dll 开发了一个小程序,但 birdRS232WakeUp() 函数似乎不起作用。

当我在 C++ 中调用 birdRS232WakeUp() 函数时,程序将停止一段时间(8-10 秒)。看起来它正在开始与硬件(鸟群)连接的过程。

但在 C# 中,调用 birdRS232WakeUp() 时它不会停止。我该如何解决这个问题?

C# 代码如下。

[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdRS232WakeUp(int nGroupID, Boolean  bStandAlone, int nNumDevices,
                                          ref ushort[] pwComport, uint dwBaudRate,
                                          uint dwReadTimeout, uint dwWriteTimeout);

ushort[] COM_port = new ushort[]{0,16,0,0,0};
if ((!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT, ref COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT)))
{
    LWakeUpStatus.Text = "Failde to wake up FOB";
}

C++ 代码如下所示。

WORD COM_port[5] = {0,15,0,0,0}

if ((!birdRS232WakeUp(GROUP_ID,
    FALSE, // Not stand-alone
    DEVCOUNT, // Number of Devices
    COM_port, // COM Port
    BAUD_RATE, // BAUD
    READ_TIMEOUT,WRITE_TIMEOUT, // Reponses timeouts
    GMS_GROUP_MODE_ALWAYS)))
{
    printf("Can't Wake Up Flock!\n");
    Sleep(3000);
    exit(-1);}

该函数的 C++ 头文件:

birdRS232WakeUp(int nGroupID, BOOL bStandAlone, int nNumDevices,
                WORD *pwComport, DWORD dwBaudRate, DWORD dwReadTimeout,
                DWORD dwWriteTimeout, int nGroupMode = GMS_GROUP_MODE_ALWAYS);

手册中指出“pwComport”指向一个字数组,每个字都是连接到其中一只鸟的 COM 端口号(例如,COM1 = 1,COM2 = 2,等)

更新1:

我采纳了elder_george的建议,但问题仍然存在。我必须将 C# 代码更改为以下内容。

public  static extern bool birdRS232WakeUp(int nGroupID, Boolean  bStandAlone, int nNumDevices,
                           ushort[] pwComport, uint dwBaudRate, uint dwReadTimeout,
                           uint dwWriteTimeout,int nGroupMode);

if ((!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT, COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT,2)))
{
    LWakeUpStatus.Text = "Failde to wake up FOB";
}

顺便说一句,根据下面的枚举类型,int nGroupMode 等于 2。

enum GroupModeSettings
{
    //    GMS_DEFAULT,         // Driver will determine whether or not to use RS232 group mode.
    GMS_GROUP_MODE_NEVER,      // RS232 group mode will never be used
    GMS_GROUP_MODE_ALWAYS,     // RS232 group mode will always be used
    NUM_GROUP_MODE_SETTINGS
};

I have developed a small program using C# and bird.dll, but the birdRS232WakeUp() function seem not to be working.

When I call the birdRS232WakeUp() function in C++ the program will stop for a while (8-10 seconds). It looks like it is beginning to do the process connecting with the hardware (Flock of bird).

But in C#, it does not stop when calling birdRS232WakeUp(). How do I fix this problem?

The C# code is like the following.

[DllImport(@"Bird.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern bool birdRS232WakeUp(int nGroupID, Boolean  bStandAlone, int nNumDevices,
                                          ref ushort[] pwComport, uint dwBaudRate,
                                          uint dwReadTimeout, uint dwWriteTimeout);

ushort[] COM_port = new ushort[]{0,16,0,0,0};
if ((!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT, ref COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT)))
{
    LWakeUpStatus.Text = "Failde to wake up FOB";
}

And the C++ code is looking like the following.

WORD COM_port[5] = {0,15,0,0,0}

if ((!birdRS232WakeUp(GROUP_ID,
    FALSE, // Not stand-alone
    DEVCOUNT, // Number of Devices
    COM_port, // COM Port
    BAUD_RATE, // BAUD
    READ_TIMEOUT,WRITE_TIMEOUT, // Reponses timeouts
    GMS_GROUP_MODE_ALWAYS)))
{
    printf("Can't Wake Up Flock!\n");
    Sleep(3000);
    exit(-1);}

C++ header file for this function:

birdRS232WakeUp(int nGroupID, BOOL bStandAlone, int nNumDevices,
                WORD *pwComport, DWORD dwBaudRate, DWORD dwReadTimeout,
                DWORD dwWriteTimeout, int nGroupMode = GMS_GROUP_MODE_ALWAYS);

And the manual states that "pwComport" points to an array of words, each of which is the number of the COM port attached to one of the birds (for example, COM1 = 1, COM2 = 2, etc.)

Update 1:

I have taken a suggestion from elder_george, but the problem still exist. I had to change the C# code to the following.

public  static extern bool birdRS232WakeUp(int nGroupID, Boolean  bStandAlone, int nNumDevices,
                           ushort[] pwComport, uint dwBaudRate, uint dwReadTimeout,
                           uint dwWriteTimeout,int nGroupMode);

if ((!birdRS232WakeUp(GROUP_ID, false, DEVCOUNT, COM_port, BAUD_RATE, READ_TIMEOUT, WRITE_TIMEOUT,2)))
{
    LWakeUpStatus.Text = "Failde to wake up FOB";
}

BTW, the int nGroupMode is equal to 2, based on the enum type below .

enum GroupModeSettings
{
    //    GMS_DEFAULT,         // Driver will determine whether or not to use RS232 group mode.
    GMS_GROUP_MODE_NEVER,      // RS232 group mode will never be used
    GMS_GROUP_MODE_ALWAYS,     // RS232 group mode will always be used
    NUM_GROUP_MODE_SETTINGS
};

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

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

发布评论

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

评论(1

浅忆 2024-11-18 06:12:47

不确定这些点是否能解决您的问题,但是:

1) pwComport 应声明为 ushort[] pwComport,而不是 ref ushort[] pwComport >

2) 您需要从 C# 传递 nGroupMode 参数。如果您使用 C#4,可以将其设置为默认值,但不要忽略它。

Not sure if these points will solve your problem, but:

1) pwComport should be declared as ushort[] pwComport, not ref ushort[] pwComport

2) you need to pass nGroupMode parameter from C#. You can set it to default value if you use C#4, but don't ignore it at all.

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