コード例 #1
0
        public void TestMethod()
        {
            var deviceHdl = new SNetDvcSensorHandler();

            deviceHdl.Config = new SNetDvcSensorCfg()
            {
                RemoteUri  = "tcp://192.168.123.201:5000",
                TxInterval = 0,
                //IsActivelyTx = true,
                TimeoutResponse = 5000,
                ProtoConnect    = SNetEnumProtoConnect.Rs232,
                ProtoFormat     = SNetEnumProtoFormat.SNetCmd,
                ProtoSession    = SNetEnumProtoSession.SNetCmd,
            };
            deviceHdl.Config.SignalCfgList.Add(new SNetSignalTransCfg()
            {
                Svid = 0,
            });
            deviceHdl.EhSignalCapture += (sender, ea) =>
            {
                fs.Write(ea);
            };



            using (deviceHdl)
            {
                deviceHdl.CfInit();
                deviceHdl.CfLoad();
                deviceHdl.CfRunLoop();
                deviceHdl.CfUnLoad();
                deviceHdl.CfFree();
                System.Threading.Thread.Sleep(100);
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        void UpdateHandlerStatus()
        {
            //是否存在, 不需dispose
            var dict = new Dictionary <string, bool>();

            //先全部設定為: 等待Dispose
            foreach (var kvhdl in this.handlers)
            {
                dict[kvhdl.Key] = false;
            }


            //Run過所有Config
            //有Config的會解除等待Dispoe
            //有Config的會執行CfRun
            foreach (var dictcfg in this.configs)
            {
                foreach (var kvcfg in dictcfg.Value)
                {
                    SNetDvcSensorHandler hdl = null;
                    if (!this.handlers.ContainsKey(kvcfg.Key))
                    {
                        hdl = new SNetDvcSensorHandler();
                        this.handlers.Add(kvcfg.Key, hdl);
                    }
                    else
                    {
                        hdl = this.handlers[kvcfg.Key];
                    }
                    hdl.Config = kvcfg.Value;

                    //解除等待Dispoe
                    dict[kvcfg.Key] = true;

                    if (hdl.Status == SNetEnumHandlerStatus.None)
                    {
                        hdl.CfInit();
                        hdl.EhSignalCapture += delegate(object sender, SNetSignalTransEventArgs e)
                        {
                            e.Sender = hdl;
                            this.OnSignalCapture(e);
                        };

                        hdl.Status = SNetEnumHandlerStatus.Init;
                    }
                    if (hdl.Status == SNetEnumHandlerStatus.Init)
                    {
                        hdl.CfLoad();
                        hdl.Status = SNetEnumHandlerStatus.Load;
                    }

                    //有Config的持續作業
                    if (hdl.Status == SNetEnumHandlerStatus.Load || hdl.Status == SNetEnumHandlerStatus.Run)
                    {
                        hdl.Status = SNetEnumHandlerStatus.Run;
                        if (!hdl.CfIsRunning)
                        {
                            hdl.CfRunLoopStart();
                        }
                    }
                }
            }


            //沒有Config的會關閉Device
            foreach (var kv in dict)
            {
                if (kv.Value)
                {
                    continue;
                }
                var hdl = this.handlers[kv.Key];

                if (hdl.Status == SNetEnumHandlerStatus.Run)
                {
                    hdl.CfUnLoad();
                    hdl.Status = SNetEnumHandlerStatus.Unload;
                }
                if (hdl.Status == SNetEnumHandlerStatus.Unload)
                {
                    hdl.CfFree();
                    hdl.Status = SNetEnumHandlerStatus.Free;
                }
                if (hdl.Status == SNetEnumHandlerStatus.Free)
                {
                    this.handlers.Remove(kv.Key);
                }
            }
        }
コード例 #4
0
        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);
        }