/*
         * /// <summary>
         * /// This class contains both the analog and the digital tasks returned from a specific device.
         * /// The reason for its existence is that if a device is to have both its analog and its digital outputs used,
         * /// then they must exist in separate tasks.
         * /// </summary>
         * ///
         * public class TaskCollection
         * {
         *  public Task analogTask;
         *  public Task digitalTask;
         * }
         */

        /*
         * public static Task createVariableTimebaseTask(string digitalTimebaseOutLine, string analogTimebaseOutLine)
         * {
         *
         * }*/

        ///
        /// This method creates a daqMX task for an "output now" command, and starts the output.
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="settings"></param>
        /// <param name="output"></param>
        /// <returns></returns>


        public static Task createDaqMxTaskAndOutputNow(string deviceName, DeviceSettings deviceSettings, SingleOutputFrame output,
                                                       SettingsData settings, Dictionary <int, HardwareChannel> usedDigitalChannels, Dictionary <int, HardwareChannel> usedAnalogChannels)
        {
            Task task = new Task(deviceName + " output task");

            List <int>              analogIDs;
            List <HardwareChannel>  analogs;
            Dictionary <int, int[]> port_digital_IDs;
            List <int>              usedPortNumbers;

            // Parse and create channels.
            parseAndCreateChannels(deviceName, deviceSettings, usedDigitalChannels, usedAnalogChannels, task, out analogIDs, out analogs, out port_digital_IDs, out usedPortNumbers);


            // now create buffer.

            task.Timing.SampleTimingType = SampleTimingType.OnDemand;

            // analog output
            if (analogIDs.Count != 0)
            {
                // extract a list of analog values corresponding to the list analodIDs. This is
                // sorted in the same way as the channels were created in parseAndCreateChannels
                List <double> outputValues = new List <double>();
                foreach (int analogID in analogIDs)
                {
                    double val;
                    if (output.analogValues.ContainsKey(analogID))
                    {
                        val = output.analogValues[analogID];
                    }
                    else
                    {
                        val = 0;
                    }
                    outputValues.Add(val);
                }

                AnalogMultiChannelWriter writer = new AnalogMultiChannelWriter(task.Stream);
                writer.WriteSingleSample(true, outputValues.ToArray());
            }

            // digital output
            if (usedPortNumbers.Count != 0)
            {
                List <byte> outputValues = new List <byte>();

                foreach (int portNumber in usedPortNumbers)
                {
                    byte digitalMask = 1;
                    byte value       = 0;
                    for (int lineNum = 0; lineNum < 8; lineNum++)
                    {
                        int digitalID = port_digital_IDs[portNumber][lineNum];
                        if (digitalID != -1)
                        {
                            bool val = false;
                            if (output.digitalValues.ContainsKey(digitalID))
                            {
                                val = output.digitalValues[digitalID];
                            }

                            if (val)
                            {
                                value |= digitalMask;
                            }
                        }
                        digitalMask = (byte)(digitalMask << 1);
                    }

                    outputValues.Add(value);
                }

                DigitalMultiChannelWriter writer = new DigitalMultiChannelWriter(task.Stream);
                writer.WriteSingleSamplePort(true, outputValues.ToArray());
            }

            return(task);
        }
        /*
        /// <summary>
        /// This class contains both the analog and the digital tasks returned from a specific device.
        /// The reason for its existence is that if a device is to have both its analog and its digital outputs used,
        /// then they must exist in separate tasks.
        /// </summary>
        ///
        public class TaskCollection
        {
            public Task analogTask;
            public Task digitalTask;
        }
        */
        /*
        public static Task createVariableTimebaseTask(string digitalTimebaseOutLine, string analogTimebaseOutLine)
        {

        }*/
        ///
        /// This method creates a daqMX task for an "output now" command, and starts the output.
        /// </summary>
        /// <param name="deviceName"></param>
        /// <param name="settings"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public static Task createDaqMxTaskAndOutputNow(string deviceName, DeviceSettings deviceSettings, SingleOutputFrame output, 
            SettingsData settings, Dictionary<int, HardwareChannel> usedDigitalChannels, Dictionary<int, HardwareChannel> usedAnalogChannels)
        {
            Task task = new Task(deviceName + " output task");

            List<int> analogIDs;
            List<HardwareChannel> analogs;
            Dictionary<int, int[]> port_digital_IDs;
            List<int> usedPortNumbers;

            // Parse and create channels.
            parseAndCreateChannels(deviceName,deviceSettings, usedDigitalChannels, usedAnalogChannels, task, out analogIDs, out analogs, out port_digital_IDs, out usedPortNumbers);

            // now create buffer.

            task.Timing.SampleTimingType = SampleTimingType.OnDemand;

            // analog output
            if (analogIDs.Count != 0)
            {

                // extract a list of analog values corresponding to the list analodIDs. This is
                // sorted in the same way as the channels were created in parseAndCreateChannels
                List<double> outputValues = new List<double>();
                foreach (int analogID in analogIDs)
                {
                    double val;
                    if (output.analogValues.ContainsKey(analogID))
                        val = output.analogValues[analogID];
                    else
                        val = 0;
                    outputValues.Add(val);
                }

                AnalogMultiChannelWriter writer = new AnalogMultiChannelWriter(task.Stream);
                writer.WriteSingleSample(true, outputValues.ToArray());
            }

            // digital output
            if (usedPortNumbers.Count != 0)
            {
                List<byte> outputValues = new List<byte>();

                foreach (int portNumber in usedPortNumbers)
                {
                    byte digitalMask = 1;
                    byte value=0;
                    for (int lineNum = 0; lineNum < 8; lineNum++)
                    {
                        int digitalID = port_digital_IDs[portNumber][lineNum];
                        if (digitalID != -1)
                        {
                            bool val = false;
                            if (output.digitalValues.ContainsKey(digitalID))
                                val = output.digitalValues[digitalID];

                            if (val)
                                value |= digitalMask;
                        }
                        digitalMask = (byte) (digitalMask << 1);
                    }

                    outputValues.Add(value);
                }

                DigitalMultiChannelWriter writer = new DigitalMultiChannelWriter(task.Stream);
                writer.WriteSingleSamplePort(true, outputValues.ToArray());
            }

            return task;
        }
Esempio n. 3
0
        private void Line2Valve_StateChanged(int port, int line, byte value)
        {
            //if (!resettingSwitches)
            //{
            try
            {
                // Get the task name and load from MAX
                string taskName = "";

                switch (port)
                {
                case 0:
                    taskName = "";
                    break;

                case 1:
                    taskName = "";
                    break;

                case 2:
                    taskName = "Port2DigitalOutTask";
                    break;

                default:
                    break;
                }

                using (NationalInstruments.DAQmx.Task digitalWriteTask = DaqSystem.Local.LoadTask(taskName))
                {
                    // Get switch values
                    switch (line)
                    {
                    case 0:
                        dataArray[0] = 1;
                        break;

                    case 1:
                        dataArray[1] = 0;
                        break;

                    case 2:
                        dataArray[2] = 0;
                        break;

                    case 3:
                        dataArray[3] = 0;
                        break;

                    case 4:
                        dataArray[4] = 0;
                        break;

                    case 5:
                        dataArray[5] = 0;
                        break;

                    case 6:
                        dataArray[6] = 0;
                        break;

                    case 7:
                        dataArray[7] = 0;
                        break;

                    default:
                        break;
                    }
                    //int dataValue = 0;

                    // Convert switch values (0/1) into a decimal value
                    //for (int i = 0; i < 8; i++)
                    //{
                    //    if (dataArray[i] == 1)
                    //        dataValue += Convert.ToInt32(Math.Pow(2, (double)i));
                    //}

                    // Write data to the port
                    DigitalMultiChannelWriter writer = new DigitalMultiChannelWriter(digitalWriteTask.Stream);
                    writer.WriteSingleSamplePort(true, dataArray);
                }
            }
            catch (DaqException ex)
            {
                MessageBox.Show(ex.Message);
            }
            //}
        }