Exemple #1
0
    /// <summary>
    /// Method main
    ///
    /// </summary>
    /// <param name="args">
    ///  </param>
    public static void Main1(string[] args)
    {
        OneWireContainer owd;

        try
        {
            // get the default adapter
            DSPortAdapter adapter = OneWireAccessProvider.DefaultAdapter;

            Debug.WriteLine("");
            Debug.WriteLine("Adapter: " + adapter.AdapterName + " Port: " + adapter.PortName);
            Debug.WriteLine("");

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // clear any previous search restrictions
            adapter.setSearchAllDevices();
            adapter.targetAllFamilies();
            adapter.Speed = DSPortAdapter.SPEED_REGULAR;

            // enumerate through all the 1-Wire devices found
            for (System.Collections.IEnumerator owd_enum = adapter.AllDeviceContainers; owd_enum.MoveNext();)
            {
                owd = (OneWireContainer)owd_enum.Current;

                Debug.WriteLine(owd.AddressAsString);
            }

            // end exclusive use of adapter
            adapter.endExclusive();

            // free port used by adapter
            adapter.freePort();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }

        return;
    }
        private static void PollSensors()
        {
            com.dalsemi.onewire.container.OneWireContainer owd = default(com.dalsemi.onewire.container.OneWireContainer);
            object state = null;

            com.dalsemi.onewire.container.TemperatureContainer tc       = default(com.dalsemi.onewire.container.TemperatureContainer);
            com.dalsemi.onewire.adapter.DSPortAdapter          oAdapter = null;
            bool bFound1w = false;

            for (int i = 0; i < 16; i++)
            {
                try
                {
                    //Logger.Log(Logger.LOGLEVEL_INFO, "Looking for 1 wire on USB" + i.ToString());

                    oAdapter = null;
                    try { oAdapter = com.dalsemi.onewire.OneWireAccessProvider.getAdapter("{DS9490}", "USB" + i.ToString()); }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                        //Logger.Log(Logger.LOGLEVEL_INFO, "USB" + i.ToString() + " " + msg);
                    }

                    if (oAdapter != null)
                    {
                        bFound1w = true;
                        Logger.Log(Logger.LOGLEVEL_INFO, "Found 1 wire on USB" + i.ToString());
                        //java.util.Enumeration owd_enum = default(java.util.Enumeration);

                        // get exclusive use of 1-Wire network
                        oAdapter.beginExclusive(true);

                        // clear any previous search restrictions
                        oAdapter.setSearchAllDevices();
                        oAdapter.targetAllFamilies();
                        oAdapter.setSpeed(com.dalsemi.onewire.adapter.DSPortAdapter.SPEED_REGULAR);
                        //                        oAdapter.setSpeed(com.dalsemi.onewire.adapter.DSPortAdapter.SPEED_HYPERDRIVE);

                        // Get all device containers
                        //oAdapter.getAllDeviceContainers();

                        var owd_enum = getEnumerator(oAdapter);
                        //Logger.Log(Logger.LOGLEVEL_INFO, "Enumerating devices connected to adapter " + oAdapter.getAdapterName());

                        // enumerate through all the 1-Wire devices found (with Java-style enumeration)
                        while (owd_enum.hasMoreElements())
                        {
                            try
                            {
                                // retrieve OneWireContainer
                                owd = (com.dalsemi.onewire.container.OneWireContainer)owd_enum.nextElement();
                                // check to see if 1-Wire device supports temperatures, if so get address and temp.
                                if (owd is com.dalsemi.onewire.container.TemperatureContainer)
                                {
                                    // cast the OneWireContainer to TemperatureContainer
                                    tc = (com.dalsemi.onewire.container.TemperatureContainer)owd;
                                    // read the device
                                    state = tc.readDevice();
                                    // extract the temperature from previous read
                                    tc.doTemperatureConvert((sbyte[])state);
                                    SetValue(owd.getAddressAsString(), (int)tc.getTemperature((sbyte[])state));
                                }
                                else
                                {
                                    Logger.Log(Logger.LOGLEVEL_WARNING, "Non-thermometer device found");
                                }
                            }
                            catch (Exception ex)
                            {
                                string sError = "USB" + i.ToString() + " ";
                                try
                                {
                                    sError += owd.getAddressAsString() + " ";
                                }
                                catch (Exception ex2)
                                {
                                    sError += "NO ID: " + ex2.Message + " ";
                                }
                                sError += ex.ToString();

                                Logger.Log(Logger.LOGLEVEL_ERROR, sError);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(Logger.LOGLEVEL_ERROR, "USB" + i.ToString() + " " + ex.ToString());
                }
                finally
                {
                    try
                    {
                        if (oAdapter != null)
                        {
                            // end exclusive use of 1-Wire net adapter
                            oAdapter.freePort();
                            oAdapter.endExclusive();
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            if (!bFound1w)
            {
                Logger.Log(Logger.LOGLEVEL_INFO, "No 1 wire USB found");
            }
        }
Exemple #3
0
    /// <summary>
    /// Method main, that provides the dialog box prompts to create the
    /// 1-Wire XML tag.
    /// </summary>
    /// <param name="args"> command line arguments </param>
    public static void Main(string[] args)
    {
        OneWireContainer tag_owd;
        DSPortAdapter    adapter = null;
        ArrayList        owd_vect = new ArrayList(5);
        bool             get_min = false, get_max = false, get_channel = false, get_init = false, get_scale = false;
        string           file_type, label, tag_type, method_type = null, cluster;
        string           min = null, max = null, channel = null, init = null, scale = null;

        // connect now message
        JOptionPane.showMessageDialog(null, "Connect the 1-Wire device to Tag onto the Default 1-Wire port", "1-Wire Tag Creator", JOptionPane.INFORMATION_MESSAGE);

        try
        {
            // get the default adapter
            adapter = OneWireAccessProvider.DefaultAdapter;

            // get exclusive use of adapter
            adapter.beginExclusive(true);

            // find all parts
            owd_vect = findAllDevices(adapter);

            // select a device
            tag_owd = selectDevice(owd_vect, "Select the 1-Wire Device to Tag");

            // enter the label for this devcie
            label = JOptionPane.showInputDialog(null, "Enter a human readable label for this device: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            if (string.ReferenceEquals(label, null))
            {
                throw new InterruptedException("Aborted");
            }

            // enter the cluster for this devcie
            cluster = JOptionPane.showInputDialog(null, "Enter a cluster where this device will reside: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            if (string.ReferenceEquals(cluster, null))
            {
                throw new InterruptedException("Aborted");
            }

            // select the type of device
            string[] tag_types = new string[] { "sensor", "actuator", "branch" };
            tag_type = (string)JOptionPane.showInputDialog(null, "Select the Tag Type", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE, null, tag_types, tag_types[0]);
            if (string.ReferenceEquals(tag_type, null))
            {
                throw new InterruptedException("Aborted");
            }

            // check if branch selected
            if (string.ReferenceEquals(tag_type, "branch"))
            {
                get_init    = true;
                get_channel = true;
            }
            // sensor
            else if (string.ReferenceEquals(tag_type, "sensor"))
            {
                string[] sensor_types = new string[] { "Contact", "Humidity", "Event", "Thermal" };
                method_type = (string)JOptionPane.showInputDialog(null, "Select the Sensor Type", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE, null, sensor_types, sensor_types[0]);
                if (string.ReferenceEquals(method_type, null))
                {
                    throw new InterruptedException("Aborted");
                }

                // contact
                if (string.ReferenceEquals(method_type, "Contact"))
                {
                    get_min = true;
                    get_max = true;
                }
                // Event
                else if (string.ReferenceEquals(method_type, "Event"))
                {
                    get_channel = true;
                    get_max     = true;
                }
            }
            // actuator
            else
            {
                string[] actuator_types = new string[] { "Switch", "D2A" };
                method_type = (string)JOptionPane.showInputDialog(null, "Select the Actuator Type", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE, null, actuator_types, actuator_types[0]);
                if (string.ReferenceEquals(method_type, null))
                {
                    throw new InterruptedException("Aborted");
                }

                get_channel = true;
                get_init    = true;
                get_min     = true;
                get_max     = true;
            }

            // enter the tags required
            if (get_min)
            {
                min = JOptionPane.showInputDialog(null, "Enter the 'min' value: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            }
            if (string.ReferenceEquals(min, null))
            {
                get_min = false;
            }

            if (get_max)
            {
                max = JOptionPane.showInputDialog(null, "Enter the 'max' value: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            }
            if (string.ReferenceEquals(max, null))
            {
                get_max = false;
            }

            if (get_channel)
            {
                channel = JOptionPane.showInputDialog(null, "Enter the 'channel' value: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            }
            if (string.ReferenceEquals(channel, null))
            {
                get_channel = false;
            }

            if (get_init)
            {
                init = JOptionPane.showInputDialog(null, "Enter the 'init' value: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            }
            if (string.ReferenceEquals(init, null))
            {
                get_init = false;
            }

            if (get_scale)
            {
                scale = JOptionPane.showInputDialog(null, "Enter the 'scale' value: ", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE);
            }
            if (string.ReferenceEquals(scale, null))
            {
                get_scale = false;
            }

            // build the XML file
            ArrayList xml = new ArrayList(5);
            xml.Add("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xml.Add("<cluster name=\"" + cluster + "\">");
            xml.Add(" <" + tag_type + " addr=\"" + tag_owd.AddressAsString + "\" type=\"" + method_type + "\">");
            xml.Add("  <label>" + label + "</label>");
            if (get_max)
            {
                xml.Add("  <max>" + max + "</max>");
            }
            if (get_min)
            {
                xml.Add("  <min>" + min + "</min>");
            }
            if (get_channel)
            {
                xml.Add("  <channel>" + channel + "</channel>");
            }
            if (get_init)
            {
                xml.Add("  <init>" + init + "</init>");
            }
            if (get_scale)
            {
                xml.Add("  <scale>" + scale + "</scale>");
            }
            xml.Add(" </" + tag_type + ">");
            xml.Add("</cluster>");

            // display the XML file
            JList list = new JList(xml.ToArray());
            if (MessageBox.Show(null, list, "Is this correct?", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                throw new InterruptedException("Aborted");
            }

            // loop until file written
            bool file_written = false;
            do
            {
                // Check if doing desktop or 1-Wire file
                string[] file_types = new string[] { "Desktop File", "1-Wire File" };
                file_type = (string)JOptionPane.showInputDialog(null, "Select where to put this XML 1-Wire Tag file", "1-Wire tag Creator", JOptionPane.INFORMATION_MESSAGE, null, file_types, file_types[0]);
                if (string.ReferenceEquals(file_type, null))
                {
                    throw new InterruptedException("Aborted");
                }

                // save to a PC file
                if (string.ReferenceEquals(file_type, "Desktop File"))
                {
                    JFileChooser chooser   = new JFileChooser();
                    int          returnVal = chooser.showSaveDialog(null);
                    if (returnVal == JFileChooser.APPROVE_OPTION)
                    {
                        try
                        {
                            PrintWriter writer = new PrintWriter(new System.IO.FileStream(chooser.SelectedFile.CanonicalPath, true));
                            for (int i = 0; i < xml.Count; i++)
                            {
                                writer.println(xml[i]);
                            }
                            writer.flush();
                            writer.close();
                            JOptionPane.showMessageDialog(null, "XML File saved to: " + chooser.SelectedFile.CanonicalPath, "1-Wire Tag Creator", JOptionPane.INFORMATION_MESSAGE);
                            file_written = true;
                        }
                        catch (FileNotFoundException e)
                        {
                            Console.WriteLine(e);
                            JOptionPane.showMessageDialog(null, "ERROR saving XML File: " + chooser.SelectedFile.CanonicalPath, "1-Wire Tag Creator", JOptionPane.WARNING_MESSAGE);
                        }
                    }
                }
                // 1-Wire file
                else
                {
                    // search parts again in case the target device was just connected
                    owd_vect = findAllDevices(adapter);

                    // select the 1-Wire device to save the file to
                    tag_owd = selectDevice(owd_vect, "Select the 1-Wire Device to place XML Tag");

                    // attempt to write to the filesystem of this device
                    try
                    {
                        PrintWriter writer = new PrintWriter(new OWFileOutputStream(tag_owd, "TAGX.0"));
                        for (int i = 0; i < xml.Count; i++)
                        {
                            writer.println(xml[i]);
                        }
                        writer.flush();
                        writer.close();
                        JOptionPane.showMessageDialog(null, "XML File saved to: " + tag_owd.AddressAsString + "\\TAGX.000", "1-Wire Tag Creator", JOptionPane.INFORMATION_MESSAGE);
                        file_written = true;
                    }
                    catch (OWFileNotFoundException e)
                    {
                        Console.WriteLine(e);
                        JOptionPane.showMessageDialog(null, "ERROR saving XML File: " + tag_owd.AddressAsString + "\\TAGX.000", "1-Wire Tag Creator", JOptionPane.WARNING_MESSAGE);
                    }
                }

                // check if file not written
                if (!file_written)
                {
                    if (MessageBox.Show(null, "Try to save file again?", "1-Wire Tag Creator", MessageBoxButtons.OKCancel) != DialogResult.OK)
                    {
                        throw new InterruptedException("Aborted");
                    }
                }
            } while (!file_written);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
        finally
        {
            if (adapter != null)
            {
                // end exclusive use of adapter
                adapter.endExclusive();

                // free the port used by the adapter
                Console.WriteLine("Releasing adapter port");
                try
                {
                    adapter.freePort();
                }
                catch (OneWireException e)
                {
                    Console.WriteLine(e);
                }
            }
        }

        Environment.Exit(0);
    }