encoder exit, sigint, blocking in mach_msg_trap

Supporting 10.7 and up
convolvatron

encoder exit, sigint, blocking in mach_msg_trap

Postby convolvatron » Tue Sep 08, 2009 4:07 pm

i have a simple executable using the phidgets 21 framework which
reads samples from 3 encoders (a jog paddle in a cnc application)

this works quite well, and was easy to put together. but i've been having a problem that i cant seem to work around.

normal termination signals are being masked somewhere in the phidgets library, thus i can't kill it properly. the program is always left stuck waiting for further input. i cant run another instance of the program because the encoders are opened for exclusive access.

if i spin all three encoders, thus injecting another event and waking up the sleeping threads, the program exits normally. however i'd really like to be able to just have it die and restart it

thanks for any light anyone can shed

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

Re: encoder exit, sigint, blocking in mach_msg_trap

Postby Patrick » Wed Sep 09, 2009 9:49 am

Are you calling close on all of the encoders before exiting your program? The encoders only report data when they are moving, so they block on Reads, and this makes it more important then with other Phidgets to call close, which will cancel the pending reads.

-Patrick

convolvatron

Re: encoder exit, sigint, blocking in mach_msg_trap

Postby convolvatron » Wed Sep 09, 2009 10:03 am

as you can see in the attached code i'm trying to clean things up both by a signal handler and by detecting an error reading standard input, but the processes remain in E state

shouldn't a normal SIGINT release the blocked reads? it would be preferable not to try to add signal handling at all

fragment pasted below (the attach function doesn't seem to like any files)

thanks for your time

------------

static CPhidgetHandle phidgets_open[10];
static phidgets_count = 0;

rotary create_rotary(unsigned int tag, unsigned int id)
{
rotary r = malloc(sizeof(struct rotary));
r->tag = tag;
r->state = 0; // can we ask?

//create the encoder object
CPhidgetEncoder_create(&r->e);
CPhidgetHandle e = (CPhidgetHandle)r->e;

CPhidget_set_OnAttach_Handler(e, (void *)AttachHandler, r);
CPhidget_set_OnDetach_Handler(e, (void *)DetachHandler, r);
CPhidget_set_OnError_Handler(e, (void *)ErrorHandler, e);
CPhidgetEncoder_set_OnInputChange_Handler
(r->e,
(void *)InputChangeHandler, r);
CPhidgetEncoder_set_OnPositionChange_Handler
(r->e, (void *)PositionChangeHandler, r);

CPhidget_open(e, id);
phidgets_open[phidgets_count++] = e;
}

void foo()
{
int i;
for (i=0; i < phidgets_count; i++) {
CPhidget_close(phidgets_open[i]);
CPhidget_delete(phidgets_open[i]);
}
exit(0);
}

int main(int argc, char **argv)
{
int result;
const char *err;
rotary
r0 = create_rotary(0, 66379),
r1 = create_rotary(1, 66107),
r2 = create_rotary(2, 66403);
signal(SIGTERM, foo);
signal(SIGHUP, foo);
signal(SIGKILL, foo);
signal(SIGINT, foo);
char x;
read(0, &x, 1);
foo();
// pause();
exit(0);
}

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

Re: encoder exit, sigint, blocking in mach_msg_trap

Postby Patrick » Wed Sep 09, 2009 10:48 am

There might be an issue on MacOS, I'll have to look into it further. You're using the high-speed encoders?

You should only need to attach a signal handler if you plan to ctrl-c quit your app. Either way, you do need to call close on the Phidgets - a SIGINT only affects the main thread, and the background threads need to be closed nicely or they will keep spinning.

-Patrick

convolvatron

Re: encoder exit, sigint, blocking in mach_msg_trap

Postby convolvatron » Sun Oct 18, 2009 11:07 pm

i eventually got back to this issue and resolved it using libusb-1.0.
it took 75 lines of C to get threadless asynch i/o without the phidgets library


Return to “macOS”

Who is online

Users browsing this forum: No registered users and 2 guests