• 0
Sign in to follow this  
Ormomis

How to check if Analog Discovery 2 is unplugged from PC? SDK C#

Question

Hello,

I'am writting my own software to setup an Analog Discovery 2 device and conduct some mesurements in C# using SDK library and wraper. I can connect with the device, disconnect and even retrieve some information such as serial no. I would like to have some sort of protection in case if device is rapidly unplugged from computer. In this particular circumstances in Waveforms software an error will occur.

Quote

Communication with the device failed.
Please check the USB cable and power supply connections.
For further troubleshooting consult the Help.

Communication with the device failed


DptiIO failed ERC: 0xA

 

I have tried some sort of checking with use of enumerators but enumerators works poperly when device is connected. When device is disconnected last status is remaining and no error messages are visible. 

What I hve to do to solve this problem?

Connecting with device:

 public bool AD2_Connect(ref int AErrorCode, ref int ANoOfAD2Devices)
        {
            bool AIsConnected = false;
            string AVersion = null;
            string ADeviceName = null;
            string ADeviceSn = null;
           
            // detect number of all connected supported devices
            dwf.FDwfEnum(dwf.enumfilterAll, ref ANoOfAD2Devices);        

            // open automatically the first available device
            if (dwf.FDwfDeviceOpen(-1, ref hdwf)==0)
            {
                AIsConnected = false;
            }
            else
            {
                dwf.FDwfGetVersion(out AVersion);               //get veriosn   
                dwf.FDwfEnumDeviceName(0, out ADeviceName);     //get device name
                dwf.FDwfEnumSN(0, out ADeviceSn);               //get serial no
                AIsConnected = true;                            //set flag
            }   
          
            dwf.FDwfGetLastError(ref AErrorCode);               //get last error code
            string AErrorMsg = null;
            dwf.FDwfGetLastErrorMsg(out AErrorMsg);             //get last error message
          
            return AIsConnected;                                //return flag
        }

Then I need to check if device is still connected. 

My version of chceckig if device is disconnected  or connected, but it doesn't work.

int AIsUsed = -1;
int ADevicesNo = -1;
dwf.FDwfEnum(dwf.enumfilterAll, ref ADevicesNo);
dwf.FDwfEnumDeviceIsOpened(0, ref AIsUsed);

 

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hi @Ormomis

The enumeration is not a good option since the device information is cached at driver level, this might report available for a while even after disconnect.

For simplicity, it is omitted in the example scripts, but the functions return true (1) when succede or false (0) when fail.
For a complex application each FDwf call return value should be verified and FDwfGetLastErrorMsg provides detail about the failure, like:

bool ConfigureScope(){
   if(!FDwfAnalogInChannelRangeSet(...)) return false;
   if(!FDwfAnalogInChannelOffsetSet(...)) return false;
   ...
   return true;
}
void OnError(){
    char szError[512] = {0};
    FDwfGetLastErrorMsg(szError);
    printf("DWF ERROR: %s", szError);
}
void main(){
    if(!ConfigureScope()){
        OnError();
        return;
    }
    while(true){
        if(!Measure()){
            OnError();
            return;
        }
    }
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this