VM140 Connected DLL API error

Hi,

I think that I found a problem in the Connected API function.

I can’t reopen device after I detect a connected error (USB disconnected).

I try close device before reopen it, but OpenDevice always return -1 (all devices opened)

If I jump the connected call function (with debugger), the reopen works property.

I’m using the new K8061_DLL_v3003

Thanks

This is my loop code example:

[code]

bool ret=false;
int CardAddress = -100;

CardAddress = OpenDevice();
if (CardAddress >= 0)
{
cout << “Card #”<< CardAddress << " opened." << endl;
}
else
{
cout << "Card not found " << CardAddress << endl;
CardAddress = 0;
exit(-1);
}

while (1){

            cout << "Press Enter to \"Connected\"" << endl;
            cin.get();
            CardAddress = 0;
            ret = Connected(CardAddress);
            cout << "Deviced Connected " << ret << endl;
            if (ret == false){
                CardAddress = 0;
                cout << "Press Enter to \"CloseDevice\"" << endl;
                cin.get();
                CloseDevice(CardAddress);
                cout << "Device Closed " << endl;
                CardAddress=-100;
                while (CardAddress != 0){
                    cout << "Press Enter to \"OpenDevice\"" << endl;
                    cin.get();
                    CardAddress=-100;
                    CardAddress = OpenDevice();
                    cout << "OpenDevice return = " << CardAddress << endl;
                }

}[/code]

When the USB plug is disconnected and reconnected you have to use first CloseDevices() and then OpenDevice().
It should then reopen the card.

Here an example code:

[code]#include
#include <windows.h>
#include

using namespace std;

typedef void(WINAPI *t_func0)();
typedef int(WINAPI *t_func5)();
typedef bool(WINAPI *t_func9)(int);

t_func5 OpenDevice;
t_func9 PowerGood;
t_func9 Connected;
t_func0 CloseDevices;

int init();

HINSTANCE hDLL;
int foundDLL = 0;

int main(void)
{
int CardAddress;

int h = init();
    if (!h)
    {
            cout << "DLL found" << endl;
            foundDLL = 1;
    }
    else
    {
            cout << "DLL not found" << endl;
            cout << "Error code: " << h << endl;
    cout << "Press a key to exit." << endl;
    cin.get();
    }
    if (foundDLL)
    {
            CardAddress = OpenDevice();
            if (CardAddress >= 0)
            {
                    cout << "Card #"<< CardAddress << " opened." << endl;
            }
            else
            {
                    cout << "Card not found" << endl;
            }
    }
    if (foundDLL)
    {

        bool ret;

        cout << "Now disconnect and reconnect the USB plug. Then Press Enter." << endl;
        cin.get();
        cout << "Press Enter to \"PowerGood\"" << endl;
        cin.get();
        ret  = true;
        ret = PowerGood(CardAddress);
        cout << "--------------  PowerGood " << ret << endl << "-------------"<< endl;

        cout << "Press Enter to \"Connected\"" << endl;
        cin.get();
        ret = true;
        ret = Connected(CardAddress);
        cout << "--------------  Connected " << ret << endl << "-------------"<< endl;
        
        cout << "Press Enter to try to \"OpenDevice\" " << endl;
        cin.get();            
        CardAddress = OpenDevice();
            if (CardAddress >= 0)
            {
                    cout << "Card #"<< CardAddress << " opened." << endl;
            }
            else
            {
                    cout << "Card not found" << endl;
            }  

        cout << "Press Enter to \"CloseDevices\" " << endl;
        cin.get();
        CloseDevices();
        
        cout << "Press Enter to \"OpenDevice\" " << endl;
        cin.get();            
        CardAddress = OpenDevice();
            if (CardAddress >= 0)
            {
                    cout << "Card #"<< CardAddress << " opened." << endl;
            }
            else
            {
                    cout << "Card not found" << endl;
            }       
        
        cout << "Press Enter to \"CloseDevices\" and to \"FreeLibrary\"" << endl;
        cin.get();          
        CloseDevices();         
        FreeLibrary(hDLL);
}
return EXIT_SUCCESS;

}

int init()
{
hDLL = LoadLibrary(TEXT(“K8061.DLL”));
if (hDLL != NULL)
{
OpenDevice = (t_func5) GetProcAddress(hDLL, “OpenDevice”);
if (!OpenDevice)
{ // handle the error
FreeLibrary(hDLL);
return -2;
}
CloseDevices = (t_func0) GetProcAddress(hDLL, “CloseDevices”);
if (!CloseDevices)
{ // handle the error
FreeLibrary(hDLL);
return -3;
}
Connected = (t_func9) GetProcAddress(hDLL, “Connected”);
if (!Connected)
{ // handle the error
FreeLibrary(hDLL);
return -4;
}
PowerGood = (t_func9) GetProcAddress(hDLL, “PowerGood”);
if (!PowerGood)
{ // handle the error
FreeLibrary(hDLL);
return -5;
}
return 0; // OK
}
return -1; // error load DLL
}[/code]

[quote=“VEL255”]When the USB plug is disconnected and reconnected you have to use first CloseDevices() and then OpenDevice().
It should then reopen the card.
[/quote]

I don’t want to close all my cards because only one of them has been disconnected (imagine that I have 2 applications, one with 4 cards and the other one with 3, both are running and connected to the same computer, so it would not be right to close all the cards).

I think that the problem is caused in the connection call because if I’ve tested that I don’t call this function (controlling the exact timing of the disconnection with the debugger), the device is reopened properly after call closeDevice(0) and OpenDevice(0).

Are there any alternatives?

[quote]Are there any alternatives?[/quote]It seems there are no alternatives.
After disconnecting and reconnecting the USB plug you can use the CloseDevice(CardAddress) to just close the individual card.
Then open it again by using the OpenDevice().