![Yoctopuce Yocto-GPS User Manual Download Page 77](http://html1.mh-extra.com/html/yoctopuce/yocto-gps/yocto-gps_user-manual_3415557077.webp)
Listing the modules
Obtaining the list of the connected modules is performed with the
yFirstModule()
function which
returns the first module found. Then, you only need to call the
nextModule()
function of this
object to find the following modules, and this as long as the returned value is not
NULL
. Below a
short example listing the connected modules.
#import <Foundation/Foundation.h>
#import "yocto_api.h"
int
main
(
int
argc,
const
char
* argv
[])
{
NSError
*error;
@autoreleasepool
{
// Setup the API to use local USB devices
if([
YAPI RegisterHub:
@"usb"
:&error
]
!= YAPI_SUCCESS
)
{
NSLog
(
@"RegisterHub error: %@
\n
"
,
[
error localizedDescription
])
;
return
1
;
}
NSLog
(
@"Device list:
\n
"
)
;
YModule *module =
[
YModule FirstModule
]
;
while
(
module !=
nil
)
{
NSLog
(
@"%@ %@"
, module.serialNumber, module.productName
)
;
module =
[
module nextModule
]
;
}
[
YAPI FreeAPI
]
;
}
return
0
;
}
10.3. Error handling
When you implement a program which must interact with USB modules, you cannot disregard error
handling. Inevitably, there will be a time when a user will have unplugged the device, either before
running the software, or even while the software is running. The Yoctopuce library is designed to help
you support this kind of behavior, but your code must nevertheless be conceived to interpret in the
best possible way the errors indicated by the library.
The simplest way to work around the problem is the one used in the short examples provided in this
chapter: before accessing a module, check that it is online with the
isOnline
function, and then
hope that it will stay so during the fraction of a second necessary for the following code lines to run.
This method is not perfect, but it can be sufficient in some cases. You must however be aware that
you cannot completely exclude an error which would occur after the call to
isOnline
and which
could crash the software. The only way to prevent this is to implement one of the two error handling
techniques described below.
The method recommended by most programming languages for unpredictable error handling is the
use of exceptions. By default, it is the behavior of the Yoctopuce library. If an error happens while
you try to access a module, the library throws an exception. In this case, there are three possibilities:
• If your code catches the exception and handles it, everything goes well.
• If your program is running in debug mode, you can relatively easily determine where the
problem happened and view the explanatory message linked to the exception.
• Otherwise... the exception makes your program crash, bang!
As this latest situation is not the most desirable, the Yoctopuce library offers another possibility for
error handling, allowing you to create a robust program without needing to catch exceptions at every
line of code. You simply need to call the
YAPI.DisableExceptions()
function to commute the
library to a mode where exceptions for all the functions are systematically replaced by specific return
values, which can be tested by the caller when necessary. For each function, the name of each
return value in case of error is systematically documented in the library reference. The name always
follows the same logic: a
get_state()
method returns a
Y_STATE_INVALID
value, a
10. Using Yocto-GPS with Objective-C
www.yoctopuce.com
71
Summary of Contents for Yocto-GPS
Page 1: ...Yocto GPS User s guide...
Page 2: ......
Page 6: ...22 Characteristics 557 Blueprint 559 Index 561...
Page 10: ...4 www yoctopuce com...
Page 14: ...8 www yoctopuce com...
Page 18: ...12 www yoctopuce com...
Page 22: ...16 www yoctopuce com...
Page 38: ...32 www yoctopuce com...
Page 42: ...36 www yoctopuce com...
Page 54: ...48 www yoctopuce com...
Page 72: ...66 www yoctopuce com...
Page 92: ...86 www yoctopuce com...
Page 122: ...116 www yoctopuce com...
Page 132: ...126 www yoctopuce com...
Page 564: ...22 Characteristics 558 www yoctopuce com...
Page 565: ......
Page 566: ......