Making sure the phidget is attached before checking a sensor

Supporting Java SE version 7 and up
anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Making sure the phidget is attached before checking a sensor

Postby anewsome » Fri Jul 08, 2011 12:45 am

Hi guys. First post here. I've got my code running nicely on my first phidget project but I have a few questions.

My code has for loop that causes it to run forever, like a daemon. In the big for loop, it periodically checks the status of a sensor input. This works fine but if the phidget is disconnected for any reason, the code crashes.

I looked through the code samples to try and find a way to catch this error and saw a try, catch block that looked like what I needed but it didn't work. The program still dies if I try and check the sensor status with no phidget attached.

My code for checking the status looks like this:

if (ik.getInputState(0) == false) {
out.println("Status: OPEN");
break;
} else {
out.println("Status: CLOSED");
break;
}

Does anyone know of a way to check to make sure a phidget is connected before calling getInputState()?

The code works great as long as the phidget is attached.

RobOrt
Phidgeteer!
Posts: 85
Joined: Wed Jun 17, 2009 11:39 am
Contact:

Re: Making sure the phidget is attached before checking a se

Postby RobOrt » Fri Jul 08, 2011 5:58 am

anewsome wrote:Hi guys. First post here. I've got my code running nicely on my first phidget project but I have a few questions.

My code has for loop that causes it to run forever, like a daemon. In the big for loop, it periodically checks the status of a sensor input. This works fine but if the phidget is disconnected for any reason, the code crashes.

I looked through the code samples to try and find a way to catch this error and saw a try, catch block that looked like what I needed but it didn't work. The program still dies if I try and check the sensor status with no phidget attached.

My code for checking the status looks like this:

if (ik.getInputState(0) == false) {
out.println("Status: OPEN");
break;
} else {
out.println("Status: CLOSED");
break;
}

Does anyone know of a way to check to make sure a phidget is connected before calling getInputState()?

The code works great as long as the phidget is attached.


You need to incorporate a Detach() listener but you should also reconsider changing your program to be event-driven for the following reasons.

You are currently wasting clock cycles checking your phidget device which may have not changed state since you last checked it (or the previous 10 times). There is also the possibility you actually miss X state changes between each interrogation of the device, dependent upon the frequency of checking.

Far better is to allow the Phidget to tell your program when something has changed i.e. event driven. To do this you setup a Listener (in Java) which will be called by the underlying Phidget drivers when something of interest has changed on the Phidget.

Try Googling 'Event driven programming' or 'Asynchronous programming' for more info & refer to the Phidget docs too

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

Re: Making sure the phidget is attached before checking a se

Postby Patrick » Fri Jul 08, 2011 9:02 am

Also, you need to handle phidgets exceptions - this could be why your program crashes, as getInputState will throw an exception if the phidget is not attached.

-Patrick

anewsome
Phidgeteer!
Posts: 54
Joined: Thu Jul 07, 2011 1:43 pm
Contact:

Re: Making sure the phidget is attached before checking a se

Postby anewsome » Fri Jul 08, 2011 11:12 am

Thanks for the tips guys. I am using the event model. I do have the callback for the unattach event. In all the code examples from phidgets, the only thing I see in the callback is that it just prints a line that says the phidget is disconnected. I'm not an expert java programmer, that I can really figure out how to use the disconnect event to prevent my code somewhere else in the program from checking a sensor input.

I usually code stuff in Perl but there is no Perl module for Phidgets. In Perl, I would just have a global variable for connected status and in the detach handler, I would set connected to false. Then, before I check the sensor I would first check the connected variable.

I haven't been able to figure out how to use something like global variables in Java. My Java book says they are explicitly not allowed, which makes it a better language I guess.

Just to be clear, I AM using the event model with phidgets. The reason I have a for loop is because I'm also using network sockets and accepting connections over TCP in my program. Once connected to the socket, the client can send a command like "getStatus" and my program responds with the sensor status.

I just need to be able to check that the phidget is actually connected before trying to check the status.

I tried the try/catch block and tried to catch the phidget exception but it's not working right. Maybe it's in the wrong place.

Anyway, thanks for your help guys. I'll try and figure it out on my own. If I can't, I'll just write another script to make sure my java program has not crashed and restart it if need be.


Return to “Java”

Who is online

Users browsing this forum: No registered users and 4 guests