A simple program

Supporting 10.7 and up
Simvetanylen

A simple program

Postby Simvetanylen » Wed May 19, 2010 8:13 am

Hello world!

I want to write a simple program for begin with Phidget.
This is my code:

Phidget.h

Code: Select all

#import <Cocoa/Cocoa.h>
#import <Phidget21/Phidget21.h>


@interface Phidget : NSObject {
   
   CPhidgetInterfaceKitHandle ifkit;

}

-(id) init;

@end


Phidget.m

Code: Select all

int gotAttach(CPhidgetHandle phid, void *context) {
   [(id)context performSelectorOnMainThread:@selector(phidgetAdded:)
                          withObject:nil
                        waitUntilDone:NO];
   return 0;
}

@implementation Phidget


-(id)init {
   int serial = -1;
   
   CPhidgetInterfaceKit_create(&ifkit);
   
   CPhidget_set_OnAttach_Handler((CPhidgetHandle)ifkit, gotAttach, self);
   
   CPhidget_open((CPhidgetHandle)ifkit, 115780);
   
   CPhidget_getSerialNumber((CPhidgetHandle)ifkit, &serial);
   
   NSLog([[NSString alloc] initWithFormat:@"%d", serial]);
   
   return self;
}
@end


115780 is the serial number of my Phidget interface kit.
The program must just return the serial number, but when it is launched, it return ever -1 (the starting value).

I don't see where is the problem. Someone can help me?

Thank you in advance,
Oreste

PS: Sorry for the language, I am French.

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: A simple program

Postby Patrick » Wed May 19, 2010 10:13 am

Your problem is that open is asynchronous. You need to call CPhidget_waitForAttachment after calling open, before calling any other functions. Also, your attach event references a "phidgetAdded" method which does not exist, so the attach event really isn't doing anything useful.

-Patrick

Simvetanylen

Re: A simple program

Postby Simvetanylen » Thu May 20, 2010 3:43 am

Yes, it's works!

Thank you very much for your help!

Cordially,
Oreste

Simvetanylen

Re: A simple program

Postby Simvetanylen » Fri May 21, 2010 2:28 am

int CPhidget_waitForAttachment (CPhidgetHandle phid, int milliseconds)
Waits for attachment to happen. This can be called wirght after calling CPhidget_open, as an alternative to using the attach handler.


So, that is mean that is not possible to use CPhidget_openRemote fonction?
And how do I proceed for use openRemote?

Thank you,
Oreste

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: A simple program

Postby Patrick » Fri May 21, 2010 8:41 am

You can use openRemote just like you would use open - just replace it, it's almost exactly the same, just opens over the network rather then directly.

-Patrick

Simvetanylen

Re: A simple program

Postby Simvetanylen » Fri May 21, 2010 9:15 am

Yes, it is what I have do:

Code: Select all

-(id)init {
   int serial = -1;
   
   CPhidgetInterfaceKit_create(&ifkit);
   
   CPhidget_openRemote((CPhidgetHandle)ifkit, 115780, NULL, NULL);
   
   CPhidget_waitForAttachment((CPhidgetHandle)ifkit, 100);
   
   CPhidget_getSerialNumber((CPhidgetHandle)ifkit, &serial);
   
   NSLog([[NSString alloc] initWithFormat:@"%d", serial]);
   
   return self;
}


But the program return -1


Edit: When I use:

Code: Select all

CPhidget_waitForAttachment((CPhidgetHandle)ifkit, 0);

It's works.

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: A simple program

Postby Patrick » Fri May 21, 2010 11:57 am

You need to increase your timeout - at least 250ms for local open and probably longer would be a good idea for openRemote. 0 works because that's an infinite timeout.

-Patrick


Return to “macOS”

Who is online

Users browsing this forum: No registered users and 1 guest