|
Tag: Redirect target changed |
Line 1: |
Line 1: |
| #REDIRECT [[Handling_Errors_and_Logging]] | | #REDIRECT [[Phidget Logging]] |
| [[Category:Programming]]
| |
| {|
| |
| |style="vertical-align:middle; width: 60%;"|
| |
| You've written your code, fixed the compiler errors, and yet, the program still isn't behaving as intended. The tools described on this page will help you further debug your program and figure out where in the code things are going wrong.
| |
| | |
| If you haven't read it yet, we recommend first reading the page on [[Phidget Programming Basics]] to better understand the contents of this page.
| |
| |{{TOC limit|3}}
| |
| |}
| |
| | |
| ==Errors and Error Events==
| |
| | |
| The Phidget library uses errors and error events to communicate information about potential problems in your program. These can happen in-line in the form of an exception or error code (which generally indicate a problem in the code), and as Error Events (which generally indicate a problem with the Phidget's environment).
| |
| | |
| ===Errors from Function Calls===
| |
| | |
| When a call to a Phidgets function fails it will throw an exception (or return an error code, depending on the language). It is important to check each function call for errors, and to catch and handle any errors.
| |
| | |
| Common causes of this type of errors are the Phidget not being attached, or one of the values provided to the function being outside the valid range.
| |
| | |
| You can handle exceptions and return codes as follows:
| |
| | |
| <tabber>
| |
| Java=<syntaxhighlight lang=java>
| |
| try {
| |
| ch.open(5000);
| |
| } catch (PhidgetException ex) {
| |
| System.out.println("Failed to open: " + ex.getMessage());
| |
| }
| |
| </syntaxhighlight>
| |
| |-|
| |
| Python=<syntaxhighlight lang=python>
| |
| try:
| |
| ch.openWaitForAttachment(5000);
| |
| except PhidgetException as e:
| |
| print("Failed to open: " + e.details)
| |
| </syntaxhighlight>
| |
| |-|
| |
| C#=<syntaxhighlight lang=cSharp>
| |
| try
| |
| {
| |
| ch.Open(5000);
| |
| }
| |
| catch (PhidgetException ex)
| |
| {
| |
| Console.WriteLine("Failed to open: " + ex.Description)
| |
| }
| |
| </syntaxhighlight>
| |
| |-|
| |
| C=<syntaxhighlight lang=c>
| |
| 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);
| |
| }
| |
| </syntaxhighlight>
| |
| </tabber>
| |
| | |
| The consequences of not handling errors correctly ranges from improper behavior, to the program crashing due to an unhandled exception. Programs should check for errors on each function call, and handle any errors.
| |
| | |
| ====Error Codes====
| |
| | |
| A list of error codes can be found in the {{Phidget22API}}.
| |
| | |
| ===Error Events===
| |
| | |
| Error events are asynchronously generated by Phidget devices, and by the Phidget library itself.
| |
| | |
| These do not necessarily indicate a problem with your program, and often serve as a status update from your device that your program should know. For example, a DistanceSensor might send an OutOfRange error if it does not detect anything in its field of view, or a TemperatureSensor could send a Saturation error indicating its temperature reading is outside the valid range for the sensor.
| |
| | |
| Error events are also fired when network errors occur, or when the library is unable to handle incoming change events quickly enough (event handlers could be too slow).
| |
| | |
| We strongly recommend setting up an error event handler for your channels so your program can keep track of these error conditions.
| |
| | |
| <tabber>
| |
| Java=<syntaxhighlight lang=java>
| |
| //Declare the event listener
| |
| public static ErrorListener onError = new ErrorListener() {
| |
| @Override
| |
| public void onError(ErrorEvent e) {
| |
| System.out.println("Code: " + e.getCode());
| |
| System.out.println("Description: " + e.getDescription());
| |
| }
| |
| };
| |
| ...
| |
| //Declare your object. Replace "DigitalInput" with the object for your Phidget.
| |
| DigitalInput ch;
| |
| ...
| |
| //Assign the event listener that will be called when the event occurs
| |
| ch.addErrorListener(onError);
| |
| </syntaxhighlight>
| |
| |-|
| |
| Python=<syntaxhighlight lang=python>
| |
| #Declare the event handler
| |
| def onErrorHandler(self, code, description):
| |
| print("Code: " + str(code))
| |
| print("Description: " + description)
| |
| ...
| |
| #Declare your object. Replace "DigitalInput" with the object for your Phidget
| |
| ch = DigitalInput()
| |
| ...
| |
| #Assign the handler that will be called when the event occurs
| |
| ch.setOnErrorHandler(onErrorHandler)
| |
| </syntaxhighlight>
| |
| |-|
| |
| C#=<syntaxhighlight lang=cSharp>
| |
| //Declare the event handler
| |
| void error(object sender, Phidget22.Events.ErrorEventArgs e) {
| |
| Console.WriteLine("Code: " + e.Code.ToString());
| |
| Console.WriteLine("Description: " + e.Description);
| |
| }
| |
| ...
| |
| //Declare your object. Replace "DigitalInput" with the object for your Phidget.
| |
| DigitalInput ch;
| |
| ...
| |
| //Assign the handler that will be called when the event occurs
| |
| ch.Error += error;
| |
| </syntaxhighlight>
| |
| |-|
| |
| C=<syntaxhighlight lang=c>
| |
| //Declare the event handler
| |
| static void CCONV onErrorHandler(PhidgetHandle ph, void *ctx, Phidget_ErrorEventCode code, const char* description) {
| |
| printf("Code: %d\n", code);
| |
| printf("Description: %s\n", description);
| |
| }
| |
| ...
| |
| //Declare your object. Replace "PhidgetDigitalInputHandle" with the handle for your Phidget object.
| |
| PhidgetDigitalInputHandle ch;
| |
| ...
| |
| //Assign the handler that will be called when the event occurs
| |
| Phidget_setOnErrorHandler((PhidgetHandle)ch, onErrorHandler, NULL);
| |
| </syntaxhighlight>
| |
| </tabber>
| |
| | |
| Full descriptions of all error event codes can be found in the [[Error Event Code List]] page.
| |
| | |
| ==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.
| |
| | |
| <syntaxhighlight lang=c>
| |
| PhidgetLog_enable(PHIDGET_LOG_INFO, NULL);
| |
| </syntaxhighlight>
| |
| | |
| Or in Java:
| |
| | |
| <syntaxhighlight lang=java>
| |
| Log.enable(LogLevel.INFO, null);
| |
| </syntaxhighlight>
| |
| | |
| When <code>NULL</code> is passed to <code>enable()</code> in the above examples, the logging system will output to <code>STDERR</code>.
| |
| | |
| For a more comprehensive look at the logging system in Phidget22, you can check out the [[Phidget Logging]] page.
| |
| | |
| == Other Problems ==
| |
| | |
| If your Phidget is still not behaving as expected after handling errors and exceptions, have a look at our [[General Troubleshooting]] guide to track down the problem.
| |
| | |
| == Further Reading ==
| |
| | |
| [[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.
| |
| | |
| [[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.
| |
| | |
| [[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.
| |
| | |
| [[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.
| |
| | |
| [[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.
| |
| | |
| [[Best Phidgets Practices]] - Good programming habits that will save you from common problems when writing code for your Phidgets.
| |