using System;
using NationalInstruments.DAQmx;

namespace CRVM.Driver
{
    public class ZDSate
    {
        private Task task;
        private DigitalMultiChannelWriter writer;
        private DOChannel[] chanels;
        private bool[] data;
        public int linesCount;

        public ZDSate()
        {
            task = new Task();
            writer = new DigitalMultiChannelWriter(task.Stream);
            string[] lineNames = DaqSystem.Local.GetPhysicalChannels(PhysicalChannelTypes.DOLine, PhysicalChannelAccess.External);
            linesCount = lineNames.Length;
            chanels = new DOChannel[linesCount];
            for (int i = 0; i < lineNames.Length; i++)
            {
                chanels[i] = task.DOChannels.CreateChannel(lineNames[i], "", ChannelLineGrouping.OneChannelForEachLine);
            }
            data = new bool[linesCount];
            Start();
        }

        public string WriteData(bool[] data)//写入各个通道的标志
        {
            if (data.Length != linesCount)
            {
                return "data length is not equals dataLength";
            }
            try
            {
                writer.WriteSingleSampleSingleLine(true, data);
                return null;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string Start()//开始任务
        {
            try
            {
                task.Start();
            }
            catch (DaqException ex)
            {
                return ex.Message;
            }
            return null;
        }

        public string Stop()//停止任务
        {
            try
            {
                task.Stop();
                return null;
            }
            catch (DaqException ex)
            {
                return ex.Message;
            }
        }
    }
}

