Example #1
0
        public void RunAsyn()
        {
            CtkLog.RegisterEveryLogWrite((ss, ea) =>
            {
                var now = DateTime.Now;
                var sb  = new StringBuilder();
                sb.AppendFormat("[{0}] ", now.ToString("yyyyMMdd HH:mm:ss"));
                sb.AppendFormat("{0} ", ea.Message);
                sb.AppendFormat("{0}", ea.Exception.StackTrace);
                CtkLog.InfoNs(this, sb.ToString());
            });



            DateTime?prevTime = DateTime.Now;

            this.listener = new CtkTcpListener("127.0.0.1", 5003);
            listener.NonStopRunAsyn();
            var rnd = new Random((int)DateTime.Now.Ticks);

            listener.EhFirstConnect += (ss, ee) =>
            {
                var myea = ee as CtkNonStopTcpStateEventArgs;
                var sb   = new StringBuilder();
                sb.Append("evtFirstConnect:\n");
                sb.Append(this.CmdState());
                CtkLog.InfoNs(this, sb.ToString());
            };
            listener.EhDataReceive += (ss, ee) =>
            {
                var myea      = ee as CtkNonStopTcpStateEventArgs;
                var ctkBuffer = myea.TrxMessageBuffer;
                var msg       = Encoding.UTF8.GetString(ctkBuffer.Buffer, ctkBuffer.Offset, ctkBuffer.Length);
                if (!msg.Contains("\n"))
                {
                    return;
                }
                var sb = new StringBuilder();
                sb.Append("cmd -respData -svid 1 -data ");
                sb.Append(rnd.NextDouble());



                sb.AppendLine();

                myea.WriteMsg(sb.ToString());
            };
        }
        public void TestMethod()
        {
            //設定旗標
            var startDt        = DateTime.Now;
            var isFinishDevice = false;



            var deviceHdl = new SNetDvcSensorHandler();

            deviceHdl.Config = new SNetDvcSensorCfg()
            {
                RemoteUri         = "tcp://127.0.0.1:5003",
                IsActivelyTx      = true,
                TxInterval        = 0,
                TimeoutResponse   = 5000,
                ProtoFormat       = SNetEnumProtoFormat.SNetCmd,
                IsActivelyConnect = false,
            };
            deviceHdl.Config.SignalCfgList.Add(new SNetSignalTransCfg()
            {
                Svid = 0,
            });
            deviceHdl.EhSignalCapture += (sender, ea) =>
            {
                fs.Write(ea);
            };


            Task.Run(() =>
            {
                using (deviceHdl)
                {
                    deviceHdl.CfInit();
                    deviceHdl.CfLoad();
                    deviceHdl.CfRunLoop();
                    deviceHdl.CfUnLoad();
                    deviceHdl.CfFree();
                }
                isFinishDevice = true;
            });



            var deviceListener = new CtkTcpListener("127.0.0.1", 5003);
            var seqval         = 0.000000001;

            deviceListener.EhDataReceive += (sender, ea) =>
            {
                var state = ea as CtkNonStopTcpStateEventArgs;
                var rnd   = new Random((int)DateTime.Now.Ticks);

                //System.Threading.Thread.Sleep(1);

                //記得加換行, cmd 結構以換行為分界
                state.WriteMsg(string.Format("cmd -respData -svid 0 -data {0} {1}\n"
                                             , seqval += 0.000000001
                                             , rnd.NextDouble()
                                             ));
            };
            deviceListener.NonStopRunAsyn();



            SpinWait.SpinUntil(() => (DateTime.Now - startDt).TotalSeconds >= 3);
            deviceHdl.CfIsRunning = false;
            SpinWait.SpinUntil(() => isFinishDevice);
        }
        public void RunAsyn()
        {
            CtkLog.RegisterEveryLogWrite((ss, ea) =>
            {
                var now = DateTime.Now;
                var sb  = new StringBuilder();
                sb.AppendFormat("[{0}] ", now.ToString("yyyyMMdd HH:mm:ss"));
                sb.AppendFormat("{0} ", ea.Message);
                sb.AppendFormat("{0}", ea.Exception.StackTrace);
                CtkLog.InfoNs(this, sb.ToString());
            });


            var len        = 512;
            var sampleRate = 512.0;

            var sin1      = new DenseVector(Generate.Sinusoidal(len, sampleRate, 10.0, 1.0));
            var sin2      = new DenseVector(Generate.Sinusoidal(len, sampleRate, 60.0, 0.5));
            var wave      = sin1 + sin2;
            var waveIndex = 0;


            DateTime?prevTime = DateTime.Now;

            this.listener = new CtkTcpListener("127.0.0.1", 5003);
            listener.NonStopRunAsyn();

            listener.EhFirstConnect += (ss, ee) =>
            {
                var myea = ee as CtkNonStopTcpStateEventArgs;
                var sb   = new StringBuilder();
                sb.Append("evtFirstConnect:\n");
                sb.Append(this.CmdState());
                CtkLog.InfoNs(this, sb.ToString());
            };
            listener.EhDataReceive += (ss, ee) =>
            {
                var myea      = ee as CtkNonStopTcpStateEventArgs;
                var ctkBuffer = myea.TrxMessageBuffer;
                var msg       = Encoding.UTF8.GetString(ctkBuffer.Buffer, ctkBuffer.Offset, ctkBuffer.Length);
                if (!msg.Contains("\n"))
                {
                    return;
                }
                var sb = new StringBuilder();
                sb.Append("cmd -respData -svid 0 -data");

                var now = DateTime.Now;
                var ts  = now - prevTime.Value;
                prevTime = now;

                var limit = ts.Ticks * 1.0 / TimeSpan.TicksPerSecond * sampleRate;
                if (limit <= 0)
                {
                    limit = 1;
                }
                if (ts.TotalMilliseconds > 500)
                {
                    limit = 1;
                }

                for (var idx = 0; idx < limit; idx++)
                {
                    sb.AppendFormat(" {0}", wave[waveIndex++]);
                    if (waveIndex >= wave.Count)
                    {
                        waveIndex = 0;
                    }
                }
                sb.AppendLine();

                myea.WriteMsg(sb.ToString());
            };
        }