Setting sensorType keeps sensor from functioning

Supporting Java SE version 7 and up
Post Reply
bmoon
Fresh meat
Posts: 3
Joined: Mon Jul 31, 2017 12:28 pm
Contact:

Setting sensorType keeps sensor from functioning

Post by bmoon »

OS/X, can confirm my 8/8/8 with a 1103 on channel 0 works via the native os/x application.

If I download the sample VoltageRationInputExample and configure it to listen to the sensor WITHOUT setting the sensor type, I get readings from 0 to 1 that vary as I move my hand up/down in front of the sensor. If on the other hand I set the sensorType to PN_1103, then I no longer get readings. Here's the code without setting the sensorType:

Code: Select all


import com.phidget22.*;
import java.util.concurrent.TimeUnit;

public class VoltageRatioInputExample {

    public static void main(String[] args) throws Exception {
        //Enable logging to stdout
        com.phidget22.Log.enable(LogLevel.DEBUG, null);

        VoltageRatioInput ch = new VoltageRatioInput();

        ch.addAttachListener(new AttachListener() {
            public void onAttach(AttachEvent ae) {
                VoltageRatioInput phid = (VoltageRatioInput) ae.getSource();
                try {
                    if (phid.getDeviceClass() != DeviceClass.VINT) {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " attached");
                    } else {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " hub port " + phid.getHubPort() + " attached");
                    }
                } catch (PhidgetException ex) {
                    System.out.println(ex.getDescription());
                }
            }
        });

        ch.addDetachListener(new DetachListener() {
            public void onDetach(DetachEvent de) {
                VoltageRatioInput phid = (VoltageRatioInput) de.getSource();
                try {
                    if (phid.getDeviceClass() != DeviceClass.VINT) {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " detached");
                    } else {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " hub port " + phid.getHubPort() + " detached");
                    }
                } catch (PhidgetException ex) {
                    System.out.println(ex.getDescription());
                }
            }
        });

        ch.addErrorListener(new ErrorListener() {
            public void onError(ErrorEvent ee) {
                System.out.println("Error: " + ee.getDescription());
            }
        });

        ch.addVoltageRatioChangeListener(new VoltageRatioInputVoltageRatioChangeListener() {
            public void onVoltageRatioChange(VoltageRatioInputVoltageRatioChangeEvent e) {
                System.out.printf("Voltage Ratio Changed: %.3g\n", e.getVoltageRatio());
            }
        });

        try {
            /*
            * Please review the Phidget22 channel matching documentation for details on the device
            * and class architecture of Phidget22, and how channels are matched to device features.
             */

 /*
            * Specifies the serial number of the device to attach to.
            * For VINT devices, this is the hub serial number.
            *
            * The default is any device.
             */
            ch.setDeviceSerialNumber(433248);
            /*
            * For VINT devices, this specifies the port the VINT device must be plugged into.
            *
            * The default is any port.
             */
            //ch.setHubPort(0);

            /*
            * Specifies that the channel should only match a VINT hub port.
            * The only valid channel id is 0.
            *
            * The default is 0 (false), meaning VINT hub ports will never match
             */
//            ch.setIsHubPortDevice(false);

            /*
            * Specifies which channel to attach to.  It is important that the channel of
            * the device is the same class as the channel that is being opened.
            *
            * The default is any channel.
             */
            ch.setChannel(0);

            /*
            * In order to attach to a network Phidget, the program must connect to a Phidget22 Network Server.
            * In a normal environment this can be done automatically by enabling server discovery, which
            * will cause the client to discovery and connect to available servers.
            *
            * To force the channel to only match a network Phidget, set remote to 1.
             */
            // Net.enableServerDiscovery(ServerType.DEVICE);
            // ch.setIsRemote(true);
            System.out.println("Opening and waiting 5 seconds for attachment...");
            ch.open(5000);
            System.out.println("name is " + ch.getDeviceName() + "min " + ch.getMinDataInterval() + "max " + ch.getMaxDataInterval() +
                    " " + ch.getMinVoltageRatio() + " " + ch.getMaxVoltageRatio());
//            ch.setSensorType(VoltageRatioSensorType.PN_1103);

            System.out.println("\n\nGathering data for 30 seconds\n\n");
            TimeUnit.SECONDS.sleep(30);

            ch.close();
            System.out.println("\nClosed Voltage Ratio Input");

        } catch (PhidgetException ex) {
            System.out.println(ex.getDescription());
        }
    }
}
and here's the output:

java -cp .:phidget22.jar VoltageRatioInputExample
Opening and waiting 5 seconds for attachment...
INFO [phidget22][2017-07-31T15:52:35 macusb.c+416 RawDeviceAdded()]: Attach: 0x00007403
DEBUG [phidget22][2017-07-31T15:52:35 phidget.c+1463 _addDevice()]: PhidgetInterfaceKit 8/8/8(1010/1018/1019) (433248)
DEBUG [phidget22][2017-07-31T15:52:35 dispatch.c+161 entryDispatched()]: creating dispatcher
DEBUG [phidget22][2017-07-31T15:52:35 dispatch.c+163 entryDispatched()]: created dispatcher
INFO [phidget22][2017-07-31T15:52:35 macusb.c+416 RawDeviceAdded()]: Attach: 0x00008c03
DEBUG [phidget22][2017-07-31T15:52:35 phidget.c+1463 _addDevice()]: PhidgetLED-64 Advanced(1032) (436843)
INFO [phidget22][2017-07-31T15:52:35 usbmac.c+287 PhidgetUSBOpenHandle()]: Using Control Endpoint for Host->Device communication.
INFO [phidget22][2017-07-31T15:52:35 usbmac.c+291 PhidgetUSBOpenHandle()]: Attach: 0x00007403
INFO [phidget22][2017-07-31T15:52:35 usb.c+391 PhidgetUSBReadThreadFunction()]: PhidgetInterfaceKit 8/8/8(1010/1018/1019) (433248): ReadThread starting
channel 0 on device 433248 attached
Voltage Ratio Changed: 0.998
name is PhidgetInterfaceKit 8/8/8min 1max 1000 0.0 1.0


Gathering data for 30 seconds


Voltage Ratio Changed: 0.997
Voltage Ratio Changed: 0.998
Voltage Ratio Changed: 0.998
Voltage Ratio Changed: 0.997
Voltage Ratio Changed: 0.998
Voltage Ratio Changed: 0.998
Voltage Ratio Changed: 0.997
Voltage Ratio Changed: 0.998

Now, If I uncomment the "sensorType" setting:

Code: Select all


import com.phidget22.*;
import java.util.concurrent.TimeUnit;

public class VoltageRatioInputExample {

    public static void main(String[] args) throws Exception {
        //Enable logging to stdout
        com.phidget22.Log.enable(LogLevel.DEBUG, null);

        VoltageRatioInput ch = new VoltageRatioInput();

        ch.addAttachListener(new AttachListener() {
            public void onAttach(AttachEvent ae) {
                VoltageRatioInput phid = (VoltageRatioInput) ae.getSource();
                try {
                    if (phid.getDeviceClass() != DeviceClass.VINT) {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " attached");
                    } else {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " hub port " + phid.getHubPort() + " attached");
                    }
                } catch (PhidgetException ex) {
                    System.out.println(ex.getDescription());
                }
            }
        });

        ch.addDetachListener(new DetachListener() {
            public void onDetach(DetachEvent de) {
                VoltageRatioInput phid = (VoltageRatioInput) de.getSource();
                try {
                    if (phid.getDeviceClass() != DeviceClass.VINT) {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " detached");
                    } else {
                        System.out.println("channel " + phid.getChannel() + " on device " + phid.getDeviceSerialNumber() + " hub port " + phid.getHubPort() + " detached");
                    }
                } catch (PhidgetException ex) {
                    System.out.println(ex.getDescription());
                }
            }
        });

        ch.addErrorListener(new ErrorListener() {
            public void onError(ErrorEvent ee) {
                System.out.println("Error: " + ee.getDescription());
            }
        });

        ch.addVoltageRatioChangeListener(new VoltageRatioInputVoltageRatioChangeListener() {
            public void onVoltageRatioChange(VoltageRatioInputVoltageRatioChangeEvent e) {
                System.out.printf("Voltage Ratio Changed: %.3g\n", e.getVoltageRatio());
            }
        });

        try {
            /*
            * Please review the Phidget22 channel matching documentation for details on the device
            * and class architecture of Phidget22, and how channels are matched to device features.
             */

 /*
            * Specifies the serial number of the device to attach to.
            * For VINT devices, this is the hub serial number.
            *
            * The default is any device.
             */
            ch.setDeviceSerialNumber(433248);
            /*
            * For VINT devices, this specifies the port the VINT device must be plugged into.
            *
            * The default is any port.
             */
            //ch.setHubPort(0);

            /*
            * Specifies that the channel should only match a VINT hub port.
            * The only valid channel id is 0.
            *
            * The default is 0 (false), meaning VINT hub ports will never match
             */
//            ch.setIsHubPortDevice(false);

            /*
            * Specifies which channel to attach to.  It is important that the channel of
            * the device is the same class as the channel that is being opened.
            *
            * The default is any channel.
             */
            ch.setChannel(0);

            /*
            * In order to attach to a network Phidget, the program must connect to a Phidget22 Network Server.
            * In a normal environment this can be done automatically by enabling server discovery, which
            * will cause the client to discovery and connect to available servers.
            *
            * To force the channel to only match a network Phidget, set remote to 1.
             */
            // Net.enableServerDiscovery(ServerType.DEVICE);
            // ch.setIsRemote(true);
            System.out.println("Opening and waiting 5 seconds for attachment...");
            ch.open(5000);
            System.out.println("name is " + ch.getDeviceName() + "min " + ch.getMinDataInterval() + "max " + ch.getMaxDataInterval() +
                    " " + ch.getMinVoltageRatio() + " " + ch.getMaxVoltageRatio());
            ch.setSensorType(VoltageRatioSensorType.PN_1103);

            System.out.println("\n\nGathering data for 30 seconds\n\n");
            TimeUnit.SECONDS.sleep(30);

            ch.close();
            System.out.println("\nClosed Voltage Ratio Input");

        } catch (PhidgetException ex) {
            System.out.println(ex.getDescription());
        }
    }
}
I get this output:

java -cp .:phidget22.jar VoltageRatioInputExample
Opening and waiting 5 seconds for attachment...
INFO [phidget22][2017-07-31T15:54:18 macusb.c+416 RawDeviceAdded()]: Attach: 0x00007503
DEBUG [phidget22][2017-07-31T15:54:18 phidget.c+1463 _addDevice()]: PhidgetInterfaceKit 8/8/8(1010/1018/1019) (433248)
DEBUG [phidget22][2017-07-31T15:54:18 dispatch.c+161 entryDispatched()]: creating dispatcher
DEBUG [phidget22][2017-07-31T15:54:18 dispatch.c+163 entryDispatched()]: created dispatcher
INFO [phidget22][2017-07-31T15:54:18 macusb.c+416 RawDeviceAdded()]: Attach: 0x00008d03
DEBUG [phidget22][2017-07-31T15:54:18 phidget.c+1463 _addDevice()]: PhidgetLED-64 Advanced(1032) (436843)
INFO [phidget22][2017-07-31T15:54:18 usbmac.c+287 PhidgetUSBOpenHandle()]: Using Control Endpoint for Host->Device communication.
INFO [phidget22][2017-07-31T15:54:18 usbmac.c+291 PhidgetUSBOpenHandle()]: Attach: 0x00007503
INFO [phidget22][2017-07-31T15:54:18 usb.c+391 PhidgetUSBReadThreadFunction()]: PhidgetInterfaceKit 8/8/8(1010/1018/1019) (433248): ReadThread starting
channel 0 on device 433248 attached
Voltage Ratio Changed: 0.997
name is PhidgetInterfaceKit 8/8/8min 1max 1000 0.0 1.0


Gathering data for 30 seconds

If I try moving the "setSensorType" to before the open, I get an error that says the channel is not attached.

java -cp .:phidget22.jar VoltageRatioInputExample
ERR [phidget22][2017-07-31T15:55:48 voltageratioinput.gen.c+1172 PhidgetVoltageRatioInput_setSensorType()]: PhidgetVoltageRatioInput is not attached
Device not Attached
User avatar
Patrick
Lead Developer
Posts: 3399
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: Setting sensorType keeps sensor from functioning

Post by Patrick »

If you set the sensor type, you will get SensorChange events not VoltageRatioChange events - looks like the example is missing code to set up a Sensor Change event.

-Patrick
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest