C# Attaching Problem

Supporting Visual Studio on Windows
DIYDyno
Phidgetsian
Posts: 6
Joined: Sat May 05, 2018 10:40 pm
Contact:

C# Attaching Problem

Postby DIYDyno » Sat May 05, 2018 10:51 pm

Hello,

I am trying to connect several sensors through a VINT hub and out-putting the data in a text box (similar to the Phidget_Programming_Basics video example).
https://www.phidgets.com/docs/Phidget_P ... ing_Basics

Unfortunately I am unable to see all of the code in the video so I cannot duplicate all of it, if you could post the code in that example, that might be all I need.

I tried to make use of the C# code examples, but I could not do much with them because some of the supporting files are not included.

The problem I am having now is that the attached code does not seem to be doing anything and I can't figure out why.

Nothing changes in the text box.

The Phidget Control Panel does show all the devices.

Will you please look at the code below and see if something obvious is missing?

Code: Select all

namespace Phidgets_Basic
{
    public partial class Form1 : Form
    {
        HumiditySensor humidity_sensor;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            humidity_sensor = new HumiditySensor();
            humidity_sensor.DeviceSerialNumber = 496925;
            humidity_sensor.HubPort = 0;
            humidity_sensor.IsHubPortDevice = true;
            humidity_sensor.Attach += humidity_sensor_attach;
            humidity_sensor.Detach += humidity_sensor_detach;
            humidity_sensor.HumidityChange += humidity_sensor_change;

            try
            {
                //MessageBox.Show("trying to open humidity sensor 1");
                humidity_sensor = new HumiditySensor();
                //MessageBox.Show("trying to open humidity sensor 2");
                humidity_sensor.Open();
                //MessageBox.Show("trying to open humidity sensor 3");
            }
            catch (PhidgetException ex)
            {
                MessageBox.Show("Error: " + ex.Description);
            }
        }

            //Humidity sensor
            void humidity_sensor_attach(object sender, Phidget22.Events.AttachEventArgs e)
            {
                textBox1.Text = ("1");
 

                HumiditySensor attachedDevice = (HumiditySensor)sender;

                try
                {
                    attachedDevice.DataInterval = attachedDevice.MinDataInterval;

                    attachedDevice.HumidityChangeTrigger = attachedDevice.MinHumidityChangeTrigger;
                }
                catch (PhidgetException ex) { textBox1.Text = ("Error initializing device: " + ex.Message); }
            }

            void humidity_sensor_detach(object sender, Phidget22.Events.DetachEventArgs e)
            {

            }

            void humidity_error(object sender, Phidget22.Events.ErrorEventArgs e)
            {
                MessageBox.Show(e.Description);
                //errorBox.addMessage(e.Description);
            }

            void humidity_sensor_change(object sender, HumiditySensorHumidityChangeEventArgs e)
            {
                try
                {
                textBox1.Text = Math.Round(e.Humidity, 4).ToString() + "%";
                }
                catch (PhidgetException ex) { textBox1.Text=("Error reading humidity: " + ex.Message); }

            }
        }
    }

fraser
Engineering
Posts: 238
Joined: Thu Nov 19, 2009 4:41 pm
Contact:

Re: C# Attaching Problem

Postby fraser » Mon May 07, 2018 7:58 am

change IsHubPortDevice to false instead. This property is used for opening internal channels on the VINT Hub, ie if you wanted to use the VoltageInput channel on HubPort 0. If you are connecting to a external device from the VINT Hub, set this property to false. The naming is a bit confusing.

All the other code looks good

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

Re: C# Attaching Problem

Postby mparadis » Mon May 07, 2018 10:06 am

Also you're creating humidity_sensor twice- when you set it the second time in the 'try' block, you're wiping out all of the properties you just set, including the event handlers.

Out of curiosity, which files appeared to be missing from the C# example?

DIYDyno
Phidgetsian
Posts: 6
Joined: Sat May 05, 2018 10:40 pm
Contact:

Re: C# Attaching Problem

Postby DIYDyno » Mon May 07, 2018 11:50 pm

mparadis wrote:Also you're creating humidity_sensor twice- when you set it the second time in the 'try' block, you're wiping out all of the properties you just set, including the event handlers.

Out of curiosity, which files appeared to be missing from the C# example?


When I ran it, there were about 5 messages on the designer page that it couldn't find some referenced files.
Then today when I ran it, the UI appeared and the messages were gone.

DIYDyno
Phidgetsian
Posts: 6
Joined: Sat May 05, 2018 10:40 pm
Contact:

Re: C# Attaching Problem

Postby DIYDyno » Mon May 07, 2018 11:57 pm

Thanks to everyone's help, I got it working with the code below:

I used the voltage formula listed here.

https://www.phidgets.com/?tier=3&catid= ... &prodid=96

Running it in my home in Orange CA about 10:00pm the weather says the humidity is about 74% but the value I am getting is 44%.

I don't know enough about humidity to know if that could be right or wrong.


In case anyone has a need for a simple example that worked for me for and

Code: Select all

using Phidget22;
using Phidget22.Events;


namespace Phidgets_Basic
{
    public partial class Form1 : Form
    {

        VoltageInput humidity_voltage;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            humidity_voltage = new VoltageInput();
            humidity_voltage.DeviceSerialNumber = 496925;
            humidity_voltage.HubPort = 0;
            humidity_voltage.IsHubPortDevice = true;
            humidity_voltage.Channel = 0;
            humidity_voltage.Attach += humidty_voltage_attach;
            humidity_voltage.VoltageChange += humidty_voltage_change;
            humidity_voltage.SensorChange += humidty_sensor_change;
            humidity_voltage.Error += humidty_voltage_error;
            //humidity_voltage.Detach += humidty_voltage_detach;

            try
            {
                humidity_voltage.Open();
                         }
            catch (PhidgetException ex)
            {
                MessageBox.Show("Error: " + ex.Description);
            }
        }

                //Attach voltage sensor for humidity
        void humidty_voltage_attach(object sender, Phidget22.Events.AttachEventArgs e)
        {
            {
                try
                {
                    VoltageInput attachedDevice = (VoltageInput)sender;
                }
                catch (PhidgetException ex) { textBox1.Text = ("Error initializing device: " + ex.Message); }
            }
        }

        void humidty_voltage_change(object sender, Phidget22.Events.VoltageInputVoltageChangeEventArgs e)
        {
            textBox1.Text = ((humidity_voltage.Voltage*38.12) - 40.2) .ToString() + "%";
        }


        private void humidty_sensor_change(object sender, VoltageInputSensorChangeEventArgs e)
        {
           // textBox1.Text = e.SensorValue.ToString() + " " + e.SensorUnit.Symbol;
        }

        void humidty_voltage_error(object sender, Phidget22.Events.ErrorEventArgs e)
        {
            textBox1.Text = (e.Description);
            //errorBox.addMessage(e.Description);
        }
    }
}



Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 2 guests