public DaqSetInputType(DaqClient parent, int ch, bool icp, DaqInputType acdc) : base(parent, DaqCmdType.DAQ_IN_TYPE) { ChannelIndex = ch; IcpEnable = icp; AcDc = acdc; }
public DaqSetGain(DaqClient parent, int ch, bool atten10, DaqGain gain) : base(parent, DaqCmdType.DAQ_GAIN) { ChannelIndex = ch; Atten10 = atten10; Gain = gain; }
public DaqCommand(DaqClient parent, DaqCmdType cmd, int packetSize = DefaultPacketSize) { this.parent = parent; sendBuf = new byte[packetSize]; recvBuf = new byte[packetSize]; Cmd = cmd; }
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); } }
private void ConnectDaq() { CloseDaq(); for (int i = 0; i < 5; i++) //Connect 실패가 잦으니 여러번 시도 { try { var _daq = new DaqClient(scaleFactors); _daq.PacketCountFor1Sec = PacketCountFor1Sec; _daq.Connect(moduleIp, 7000); _daq.Stop(true); //if (!_daq.Stop(true)) // throw new Exception("Stop Failed"); daq = _daq; break; } catch (Exception) { Thread.Sleep(500); } } if (daq == null) { throw new Exception("Connect Failed - IP:" + moduleIp); } foreach (var channel in channels) { daq.SetInputType(channel.PhysicalIndex, channel.ICP, InputType); //daq.SetInputType(channel.PhysicalIndex, false, DaqInputType.AC); daq.SetGain(channel.PhysicalIndex, false, channel.HWGain); } daq.SetRunVariable(); daq.SetSampleMode(nSamplingRate); daq.Start(); }
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 DaqException("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 }); }
} //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; }
private void CloseDaq() { if (daq != null) { try { daq.Stop(); daq.Close(); } catch (Exception) { } daq = null; } }
public DaqStop(DaqClient parent, bool waitCorrectResponse) : base(parent, DaqCmdType.DAQ_STOP) { this.waitCorrectResponse = waitCorrectResponse; }
public DaqStart(DaqClient parent, bool sense, bool gap) : base(parent, DaqCmdType.DAQ_START) { Sense = sense ? 1 : 0; Gap = gap ? 1 : 0; }
public DaqSetSampleRate(DaqClient parent, DaqSamplingRate sampleRate) : base(parent, DaqCmdType.DAQ_SAMPLE) { SampleRate = sampleRate; }
public static DaqDataPacket RecvDataPacket(DaqClient parent, ByteQueuedStream ns, string commandNameForLog, WinTrace trace = null) { _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); if (trace != null && type == DaqDataType.DAQ_DATA) { trace.Send("Daq DATA Packet - wordCount:" + wordCount); } 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 }); }