简体   繁体   English

C++ DLL function 似乎在 ZD7EFA19FBE7D3972745ADB602Z223 中不起作用

[英]C++ DLL function does not seem to work in C#

I have developed a small program using C# and bird.dll , but the birdRS232WakeUp() function seem not to be working.我已经使用 C# 和bird.dll开发了一个小程序,但是birdRS232WakeUp() function 似乎不起作用。

When I call the birdRS232WakeUp() function in C++ the program will stop for a while (8-10 seconds).当我在 C++ 中调用birdRS232WakeUp() function 时,程序将停止一段时间(8-10 秒)。 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() .但在 C# 中,调用birdRS232WakeUp()时不会停止。 How do I fix this problem?我该如何解决这个问题?

The C# code is like the following. 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";
}

And the C++ code is looking like the following. 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++ header file for this function:此 function 的 C++ header 文件:

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.)并且说明书上写着“pwComport”指向一个字数组,每个字是COM端口的编号连接到其中一只鸟(例如COM1 = 1,COM2 = 2等)

Update 1:更新 1:

I have taken a suggestion from elder_george, but the problem still exist.我接受了elder_george的建议,但问题仍然存在。 I had to change the C# code to the following.我不得不将 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";
}

BTW, the int nGroupMode is equal to 2, based on the enum type below.顺便说一句,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
};

Not sure if these points will solve your problem, but:不确定这些点是否能解决您的问题,但是:

1) pwComport should be declared as ushort[] pwComport , not ref ushort[] pwComport 1) pwComport应声明为ushort[] pwComport ,而不是ref ushort[] pwComport

2) you need to pass nGroupMode parameter from C#. 2)您需要从 C# 传递nGroupMode参数。 You can set it to default value if you use C#4, but don't ignore it at all.如果您使用 C#4,您可以将其设置为默认值,但不要忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM