Un exemple réel
Lancez Xcode 4.2 et ouvrez le projet exemple correspondant, fourni dans le répertoire Examples/
Doc-GettingStarted-Yocto-4-20mA-Tx de la librairie Yoctopuce.
Vous reconnaîtrez dans cet exemple l'utilisation des fonctions expliquées ci-dessus, cette fois
utilisées avec le décorum nécessaire à en faire un petit programme d'exemple concret.
#import <Foundation/Foundation.h>
#import "yocto_api.h"
#import "yocto_currentloopoutput.h"
static void
usage(void)
{
NSLog(@"Wrong command line
NSLog(@"usage: demo <serial_number>
NSLog(@"
demo <logical_name>
NSLog(@"
demo any <value> (use any discovered
NSLog(@"Eg.");
NSLog(@"
demo any
NSLog(@"
demo TX420MA1-123456
exit(1);
}
int main(int
argc,
{
NSError
*error;
YCurrentLoopOutput
if
(argc <
3) {
usage();
}
@autoreleasepool {
NSString
*target
// Setup the API to use local USB devices
if([YAPI
RegisterHub:@"usb": &error] != YAPI_SUCCESS) {
NSLog(@"RegisterHub error:
return
1;
}
if
([target
isEqualToString:@"any"]) {
// retreive any generic sensor
loop
=
[YCurrentLoopOutput
if
(loop
== NULL) {
NSLog(@"No module connected (check USB
return
1;
}
} else {
loop
=
[YCurrentLoopOutput
@".currentLoopOutput"]];
}
double
value
=
// we need to retreive both DC and AC current from the device.
if
([loop
isOnline])
[loop set_current:value];
Y_LOOPPOWER_enum loopPower
if
(loopPower
NSLog(@"Current loop not
return
1;
}
if
(loopPower
NSLog(@"Insufficient voltage on current
return
1;
}
NSLog(@"current loop set to %f
} else {
NSLog(@"Module not connected (check identification and USB
}
[YAPI
FreeAPI];
}
return
0;
}
www.yoctopuce.com
arguments");
<value>");
<value>");
12");
20");
const char
* argv[])
*loop;
=
[NSString
stringWithUTF8String:argv[1]];
%@", [error localizedDescription]);
FirstCurrentLoopOutput];
FindCurrentLoopOutput:[target
atof(argv[2]);
{
=
[loop get_loopPower];
== Y_LOOPPOWER_NOPWR) {
powered");
== Y_LOOPPOWER_LOWPWR) {
mA", value);
10. Utilisation du Yocto-4-20mA-Tx en Objective-C
device)");
cable)");
stringByAppendingString:
loop");
cable)");
63