Code Troubleshooting

From Phidgets Support
Jump to: navigation, search

If you haven't already, we recommend testing your Phidgets using the Phidget Control Panel (see the User Guide for your device for instructions). If that works, try to get our sample code running before your try to debug your own code. That will rule out the possibility of hardware or operating system issues.

How to find the problem

  • Fix compiler errors

If you get an error when compiling or building your program, the compiler should tell you exactly which line is causing the problem. You can compare that line against our code samples and Phidget22 API to try and find the discrepancy.


  • Reduce your code

You should temporarily remove or comment out as much of your program as possible to narrow down what's causing it to fail. You can also add lines that print messages to the screen so you know which parts of your code are being executed before the program fails.


  • Check for exceptions

When a call to a Phidgets function fails it will throw an exception (or return an error code, if you're programming in C). It is important to check each function call for errors, and to catch and handle any errors. You can handle exceptions and return codes as follows:

try:
    ch.openWaitForAttachment(5000);
except PhidgetException as e:
    print("Failed to open: " + e.details)

try {
    ch.open(5000);
} catch (PhidgetException ex) {
    System.out.println("Failed to open: " + ex.getMessage());
}

try
{
    ratio.Open(); //open the device
}
catch (PhidgetException ex)
{
    Console.WriteLine("Failed to open: " + ex.Description)
}
ch.Open(5000);

PhidgetDigitalInputHandle ch;
PhidgetReturnCode res;
...    
res = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
if (res != EPHIDGET_OK) {
    char* desc;
    Phidget_getErrorDescription(res, &desc);
    printf("Failed to open: (%d) %s\n", res, desc);
}


  • Handle error events

When something wrong happens with the Phidget itself, an error event will fire (e.g. a temperature sensor measuring a temperature higher than its maximum would fire a saturation error event). In order to see these errors, you need to register an error event handler and write a short function that prints the error info to the screen. For an introduction to events, see the Using Events page. For an example of how to set up error events, have a look at the Error Events page.


  • Enable Logging

You can enable logging to get more debugging information. This would happen at the very start of your program, before even initializing your software object or opening it. Logging lets you get feedback from the Phidget libraries about things happening behind the scenes. You can read more about logging here.

Log.enable(Phidget22.LogLevel.LogLevel.PHIDGET_LOG_INFO, "file.log")

Log.enable(LogLevel.INFO, "file.log");

Log.Enable(LogLevel.Info, "file.log");

PhidgetLog_enable(PHIDGET_LOG_INFO, "file.log");

The log file is difficult to read without in-depth knowledge of how Phidgets talk to your computer, so we recommend that you send us the log file along with a description of what is going wrong.


Common Issues

  • I get the "timeout" exception when I try to open a channel on my Phidget

Your program wasn't able to find the specified Phidget channel that you wanted to open. This usually means one of two things. Either you set the addressing parameters wrong (e.g. wrong DeviceSerialNumber, Channel, IsHubPortDevice, etc.) or the channel is already opened (either by this program or a different program). Double check the parameters you've set, or try commenting them out, since open will try to match any channel of the specified class even if you don't set any parameters.

Also check to make sure that the channel isn't already open in the Phidget Control Panel or another instance of your program. You may need to use the task manager (Windows) or activity monitor (macOS) to close background processes that could be tying up the channel. You can also try rebooting, which should free up all devices (unless you have a program that uses Phidgets running on system startup). As a side note, most Phidgets can be opened in more than one program at once if it's being opened remotely in each case.

  • I get the "busy" exception or error event when I try to open a channel on my Phidget

Check to make sure that the channel isn't already open in the Phidget Control Panel or another instance of your program. You may need to use the task manager (Windows) or activity monitor (macOS) to close background processes that could be tying up the channel. You can also try rebooting, which should free up all devices (unless you have a program that uses Phidgets running on system startup). As a side note, most Phidgets can be opened in more than one program at once if it's being opened remotely in each case.

  • I get the "unknownval" exception when I try to access a property or data

This usually means you're trying to get a property of the Phidget or data from the sensor before the channel finishes attaching or before the first few samples of data come in.

If you are trying to get a property of the Phidget, do it in the attach handler instead of in your main function right after opening. This ensures that the channel is done attaching.

If you are trying to get data from a sensor, you may want to try Using Events instead of polling, so your code will only run when new samples come in. You could also try checking the Attached property of the channel to make sure it's true before attempting to get anything from it.


  • My compiler doesn't recognize the names of functions from the Phidgets API

Make sure you've referenced the proper includes or imports required to run Phidgets functions. You can find them at the top of the file in our code samples. If have the same problem with our code samples, it's possible the programming environment for that programming language hasn't been set up properly for use with Phidgets. Go to the language page for your language and follow the steps to make sure it has all been set up correctly.