open() throws 'TimeOut' PhidgetException

Supporting Java SE version 7 and up
Post Reply
Zebajin
Fresh meat
Posts: 1
Joined: Sun Oct 29, 2017 5:16 am
Contact:

open() throws 'TimeOut' PhidgetException

Post by Zebajin »

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: 959
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: open() throws 'TimeOut' PhidgetException

Post by mparadis »

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.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests