示例#1
0
        static DaqHeaderType IsValidHeader(DaqClient parent, byte[] header)
        {
            var type = header[0];

            if (header[1] != 0)
            {
                //System.Diagnostics.Trace.WriteLine("Header[1]: " + header[1]);
                //return DaqHeaderType.Invalid;
            }

            if (type == (byte)DaqDataType.DAQ_DATA)
            {
                var samplerate = (header[3] << 8) | header[2];
                return(samplerate == 512 ? DaqHeaderType.Data : DaqHeaderType.Invalid);
            }
            else if (type == (byte)DaqDataType.GAP_DATA)
            {
                var dataCount = (header[3] << 8) | header[2];
                //System.Diagnostics.Trace.WriteLine("Gap: " + dataCount);
                return(dataCount == 12 ? DaqHeaderType.Data : DaqHeaderType.Invalid);
            }
            else if (type == (byte)DaqDataType.SYNC_AMP)
            {
                var dataCount = (header[3] << 8) | header[2];
                return(dataCount == 2 ? DaqHeaderType.Data : DaqHeaderType.Invalid); //2인지 불확실
            }
            else
            {
                return(DaqHeaderType.Invalid);
            }
        }
示例#2
0
 public DaqCommand(DaqClient parent, DaqCmdType cmd, int packetSize = DefaultPacketSize)
 {
     this.parent = parent;
     sendBuf     = new byte[packetSize];
     recvBuf     = new byte[packetSize];
     Cmd         = cmd;
 }
示例#3
0
 public DaqSetInputType(DaqClient parent, int ch, bool icp, DaqInputType acdc)
     : base(parent, DaqCmdType.DAQ_IN_TYPE)
 {
     ChannelIndex = ch;
     IcpEnable    = icp;
     AcDc         = acdc;
 }
示例#4
0
 public DaqSetGain(DaqClient parent, int ch, bool atten10, DaqGain gain)
     : base(parent, DaqCmdType.DAQ_GAIN)
 {
     ChannelIndex = ch;
     Atten10      = atten10;
     Gain         = gain;
 }
示例#5
0
        private void ConnectDaq()
        {
            CloseDaq();

            for (int i = 0; i < 100; i++)    //Connect 실패가 잦으니 여러번 시도
            {
                try
                {
                    var _daq = new DaqClient(Module.Channels.Select(x => x.ScaleFactors).ToArray())
                    {
                        PacketCountFor1Sec = Module.PacketCountFor1Sec
                    };
                    _daq.Connect(Module.Ip, 7000);
                    _daq.Stop(true);
                    Daq = _daq;
                    break;
                }
                catch { Thread.Sleep(100); }
            }
            if (Daq == null)
            {
                throw new Exception("Connect Failed - IP:" + Module.Ip);
            }

            foreach (var channel in Module.Channels)
            {
                Daq.SetInputType(channel.PhysicalIndex, channel.ICP, Module.InputType);
                Daq.SetGain(channel.PhysicalIndex, false, channel.HWGain);
            }
            Daq.SetRunVariable();
            Daq.SetSampleMode(Module.SamplingRate);

            Daq.Start();
        }
示例#6
0
        public static DaqDataPacket RecvPacket(DaqClient parent, NetworkStream ns, string commandNameForLog)
        {
            int nTryCount = 0;

_START:
            var byteQueue = new Queue <byte>(ReadBlock(ns, 4));

            while (IsValidHeader(parent, byteQueue.ToArray()) == DaqHeaderType.Invalid)
            {
                byteQueue.Dequeue();
                var ch = ns.ReadByte();
                if (ch < 0)
                {
                    throw new Exception("Header Read Failed - length:" + byteQueue.Count);
                }
                byteQueue.Enqueue((byte)ch);
            }

            var header    = byteQueue.ToArray();
            var type      = (DaqDataType)header[0];
            var wordCount = (ushort)(BitConverter.ToUInt16(header, 2) * 2);

            int readSize = 0;

            if (type == DaqDataType.DAQ_DATA)
            {
                readSize = 1024;
            }
            else if (type == DaqDataType.GAP_DATA)
            {
                //if (wordCount == 12)
                readSize = 48 - 4;
            }
            else if (type == DaqDataType.SYNC_AMP)
            {
                readSize = 2;
            }
            else if (Enum.IsDefined(typeof(DaqCmdType), (ushort)type))
            {
                readSize = DaqCommand.DefaultPacketSize;
            }
            //else
            //{
            //    if (nTryCount++ < 5)
            //    {
            //        System.Diagnostics.Trace.WriteLine("RecvPacket Retry " + nTryCount);
            //        goto _START;
            //    }
            //    System.Diagnostics.Trace.WriteLine("Wrong Data Type - " + type + ", command:" + commandNameForLog);
            //    throw new DaqException("Wrong Data Type - " + type + ", command:" + commandNameForLog);
            //}

            var data = ReadBlock(ns, readSize);

            return(new DaqDataPacket {
                DataType = type, Header = header, Bytes = data
            });
        }
示例#7
0
        }                                                                                         //4비트값(max 15), 채널갯수인듯

        public DaqSetRunVariable(DaqClient parent, int mode, int kpRevol, int resample, int filterOrder = 0, int kpEdgeAdnSelect = 0, int channelCount = 8)
            : base(parent, DaqCmdType.DAQ_OPMODE)
        {
            Mode             = mode;
            KP_Revolution    = kpRevol;
            ResampleSize     = resample;
            FilterOrder      = filterOrder;
            KP_EdgeAndSelect = kpEdgeAdnSelect;
            ChannelCount     = channelCount;
        }
示例#8
0
 private void CloseDaq()
 {
     if (Daq != null)
     {
         try
         {
             Daq.Stop();
             Daq.Close();
         }
         catch (Exception) { }
         Daq = null;
     }
 }
示例#9
0
        public static DaqDataPacket RecvDataPacket(DaqClient parent, ByteQueuedStream ns, string commandNameForLog)
        {
_START:
            int invalidHeaderCount = 0;

            byte[] firstInvalidHeader = null;
            while (IsValidHeader(parent, ns.PullLast(4)) == DaqHeaderType.Invalid)
            {
                ns.PopFrontByte();
                if (invalidHeaderCount == 0)
                {
                    firstInvalidHeader = ns.byteQueue.ToArray();
                }
                invalidHeaderCount++;
            }

            var header    = ns.PopFront(4);
            var type      = (DaqDataType)header[0];
            var wordCount = (ushort)(BitConverter.ToUInt16(header, 2) * 2);

            int readSize = 0;

            if (type == DaqDataType.DAQ_DATA)
            {
                readSize = 1024;
            }
            else if (type == DaqDataType.GAP_DATA)
            {
                //if (wordCount == 12)
                readSize = 48 - 4;
            }
            else if (type == DaqDataType.SYNC_AMP)
            {
                readSize = 2;
            }
            else if (Enum.IsDefined(typeof(DaqCmdType), (ushort)type))
            {
                readSize = DaqCommand.DefaultPacketSize;
                ns.PullLast(readSize);
                var data_ = ns.PopFront(readSize);
                ns.RevertFront(header);
                ns.RevertFront(data_);
                ns.PopFrontByte();
                goto _START;
            }
            else
            {
                ns.RevertFront(header);
                ns.PopFrontByte();
                goto _START;
            }

            ns.PullLast(readSize);
            var data = ns.PopFront(readSize);

            if (IsValidHeader(parent, ns.PullLast(4)) == DaqHeaderType.Invalid)
            {
                Debug.WriteLine("====================");
                ns.RevertFront(header);
                ns.RevertFront(data);
                ns.PopFrontByte();
                goto _START;
            }

            //Debug.WriteLine(" " + type + "," + invalidHeaderCount);
            //if(invalidHeaderCount != 0)
            //    Debug.WriteLine(" " + type + "," + invalidHeaderCount + "," + firstInvalidHeader.Length);
            return(new DaqDataPacket {
                DataType = type, Header = header, Bytes = data
            });
        }
示例#10
0
 public DaqStop(DaqClient parent, bool waitCorrectResponse)
     : base(parent, DaqCmdType.DAQ_STOP)
 {
     this.waitCorrectResponse = waitCorrectResponse;
 }
示例#11
0
 public DaqStart(DaqClient parent, bool sense, bool gap)
     : base(parent, DaqCmdType.DAQ_START)
 {
     Sense = sense ? 1 : 0;
     Gap   = gap ? 1 : 0;
 }
示例#12
0
 public DaqSetSampleRate(DaqClient parent, DaqSamplingRate sampleRate)
     : base(parent, DaqCmdType.DAQ_SAMPLE)
 {
     SampleRate = sampleRate;
 }