开发者

C++ DLL function does not seem to work in C#

开发者 https://www.devze.com 2023-03-09 16:10 出处:网络
I have developed a small program using C# and bird.dll, but the birdRS232WakeUp() function seem not to be working.

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
};


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消