open() throws 'TimeOut' PhidgetException

Discuss problems and usage. Support for Java is through a Java Native Interface included in the phidget21 C library
Zebajin
Fresh meat
Posts: 1
Joined: Sun Oct 29, 2017 5:16 am
Contact:

open() throws 'TimeOut' PhidgetException

Postby Zebajin » Sun Oct 29, 2017 5:25 am

Hi everybody.
I'm new to phidget programming and was just trying to write a little program that displays the voltage of a VoltageInput type sensor (slider) in Java.

I looked at the VoltageInput exemple and the online guide.

Whenever I execute the code, the open() method throws a timeout exception which I believe is due to no phidgets being attached during the duration of the open.

However I do have a slider plugged in my 8/8/8 1018 interface kit and it shows in the software control panel.

Here is my code:

Code: Select all

import com.phidget22.*;

public class LedSlider {
   
   private static VoltageInput sl;
   
   public static void main(String[] args) throws PhidgetException {

      sl = new VoltageInput();
      
      //Attach Listener
      sl.addAttachListener(new AttachListener() {
         public void onAttach(AttachEvent ae) {
            VoltageInput slider = (VoltageInput) ae.getSource();
            try {
               System.out.println("Channel" + slider.getChannel() + "on device" + slider.getDeviceSerialNumber() + "connected\n" + slider.getVoltage());
            }catch (PhidgetException ex){
               System.out.println("AL ex " + ex.getDescription());
            }
         }
      });
      
      //Detach Listener
      sl.addDetachListener(new DetachListener() {
         public void onDetach(DetachEvent de) {
            VoltageInput slider = (VoltageInput) de.getSource();
            try {
               System.out.println("Channel " + slider.getChannel() + "on device " + slider.getDeviceSerialNumber() + "detached");
            }catch(PhidgetException ex) {
               System.out.println("DL ex " + ex.getDescription());
            }
         }
      });
      
      //Upgrade Listener
      sl.addVoltageChangeListener(new VoltageInputVoltageChangeListener() {
         public void onVoltageChange(VoltageInputVoltageChangeEvent e) {
            System.out.printf("Voltage changed: %.2f\n", e.getVoltage());
         }
      });
      
      try {
         System.out.println("5000ms to attach device");
         sl.open(5000);
         
         Thread.sleep(10000);
         sl.close();
         System.out.println("Channel closed");
         
      } catch (PhidgetException ex) {
         System.out.println(ex.getDescription());
      } catch (InterruptedException ex) {
         System.out.println("Interrupted!");
      }   

   }

}

User avatar
mparadis
Site Admin
Posts: 462
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: open() throws 'TimeOut' PhidgetException

Postby mparadis » Mon Oct 30, 2017 7:38 am

1. Use functions like setDeviceSerialNumber (to the serial number of the 1018) and setChannel (the number above the analog input on the 1018) on your object ("sl" in this case) before opening in order to narrow down which port it's looking for.

2. Ensure that the same channel is not open in the Control Panel while you run your program. A Phidget channel can only be opened by one program at a time, so having it open in the control panel blocks your Java program from finding it. The only way to have a Phidget channel open in two programs simultaneously is to open it remotely (using setIsRemote) in both programs.


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest