Exemplo n.º 1
0
        public void TestMethod()
        {
            CtkLog.RegisterEveryLogWrite((ss, ee) =>
            {
                System.Diagnostics.Debug.WriteLine(ee.Message);
            });

            var deviceHdl = new SNetDvcSensorHandler();

            deviceHdl.Config = new SNetDvcSensorCfg()
            {
                RemoteUri       = "tcp://192.168.123.201:5000",
                TxInterval      = 0,
                TimeoutResponse = 5000,
                ProtoConnect    = SNetEnumProtoConnect.Tcp,
                ProtoFormat     = SNetEnumProtoFormat.SNetCmd,
                ProtoSession    = SNetEnumProtoSession.SNetCmd,
                SignalTran      = SNetEnumSignalTrans.SNetCmd,
            };
            for (var idx = 0; idx < 8; idx++)
            {
                deviceHdl.Config.SignalCfgList.Add(new SNetSignalTransCfg()
                {
                    Svid = 0x00010000 + 0x0100 * (ulong)idx,
                    //Svid = 0x00000000,
                });
            }
            for (var idx = 0; idx < 8; idx++)
            {
                deviceHdl.Config.SignalCfgList.Add(new SNetSignalTransCfg()
                {
                    Svid = 0x00020000 + 0x0100 * (ulong)idx,
                    //Svid = 0x00000000,
                });
            }

            deviceHdl.EhSignalCapture += (sender, ea) =>
            {
                fs.Write(ea);
                //if (ea.Data.Count > 0)
                //System.Diagnostics.Debug.WriteLine("{0}={1}", ea.Svid, ea.Data[0]);
            };



            using (deviceHdl)
            {
                deviceHdl.CfInit();
                deviceHdl.CfLoad();
                deviceHdl.CfRunLoop();
                deviceHdl.CfUnLoad();
                deviceHdl.CfFree();
                System.Threading.Thread.Sleep(100);
            }
        }
        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 CtkNonStopTcpListener("127.0.0.1", 5003);
            listener.NonStopConnectAsyn();
            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());
            };
        }
Exemplo n.º 3
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());
            });


            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 CtkNonStopTcpListener("127.0.0.1", 5003);
            listener.NonStopConnectAsyn();

            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());
            };
        }