コード例 #1
0
ファイル: DaqClient.cs プロジェクト: lsw8724/Project
        public void Connect(string ip, int port, int timeout = 2000)
        {
            watch = new WinWatch("wDaq" + ip, "wDaq" + ip);
            trace = new WinTrace("tDaq" + ip, "tDaq" + ip);

            tcp = new TcpClient();
            try
            {
                var result = tcp.BeginConnect(ip, port, null, null);
                tcp.ReceiveTimeout = 5000;
                bool success = result.AsyncWaitHandle.WaitOne(timeout, true);
                if (!success)
                {
                    throw new DaqException("Connect Failed - WaitOne Failed");
                }

                if (!tcp.Connected)
                {
                    throw new DaqException("Connect Failed - Not Established");
                }

                //tcp.LingerState = new LingerOption(true, 0);
                //tcp.NoDelay = true;
                ns = tcp.GetStream();
                qs = new ByteQueuedStream(ns);
                WriteDebug("Connected");
            }
            catch (Exception)
            {
                tcp.Close();
                throw;
            }
        }
コード例 #2
0
        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
            });
        }