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_serialport.h"
int main(int
argc,
{
@autoreleasepool {
NSError
*error;
// Setup the API to use local USB devices
if([YAPI
RegisterHub:@"usb": &error] != YAPI_SUCCESS) {
NSLog(@"RegisterHub error:
return
1;
}
YSerialPort
*serialPort;
if
(argc >
1) {
NSString
serialPort
=
} else {
serialPort
=
if
(serialPort
NSLog(@"No module connected (check USB
return
1;
}
}
[serialPort set_serialMode:@"9600,8N1"];
[serialPort set_protocol:@"Line"];
[serialPort reset];
NSLog(@"****************************");
NSLog(@"* make sure voltage levels
NSLog(@"* are properly configured
NSLog(@"****************************");
NSString
*line;
do {
char
input[256] =
[YAPI
Sleep:500
do {
line
=
[serialPort readLine];
if(![line
NSLog(@"Received:
}
}
while(![line isEqualToString:@""]);
NSLog(@"Type line to send, or Ctrl-C to exit:
fgets(input, sizeof(input), stdin);
line
=
[NSString
[serialPort writeLine:line];
}
while(![line isEqualToString:@"\n"]);
[YAPI
FreeAPI];
}
return
0;
}
12.2. Contrôle de la partie module
Chaque module peut-être contrôlé d'une manière similaire, vous trouverez ci dessous un simple
programme d'exemple affichant les principaux paramètres d'un module et permettant d'activer la
balise de localisation.
#import <Foundation/Foundation.h>
#import "yocto_api.h"
static void usage(const char
{
www.yoctopuce.com
const char
* argv[])
%@", [error localizedDescription]);
*target
=
[NSString
[YSerialPort
FindSerialPort:target];
[YSerialPort
FirstSerialPort];
== NULL) {
{0};
:&error];
isEqualToString:@""]) {
%@", line);
stringWithUTF8String:input];
*exe)
12. Utilisation du Yocto-Serial en Objective-C
stringWithUTF8String:argv[1]];
cable)");
*");
*");
");
85