Exemplo n.º 1
0
        /// <summary>
        /// 读取ModBus数据
        /// </summary>
        /// <returns></returns>
        public Task RunGetModBus()
        {
            bool IsRunning = true;

            return(Task.Factory.StartNew(() =>
            {
                // 设置线程名称
                Thread.CurrentThread.Name = "存储数据";
                Thread.CurrentThread.IsBackground = true;
                Thread.CurrentThread.Priority = ThreadPriority.Normal;

                while (IsRunning)
                {
                    Thread.Sleep(2000);
                    try
                    {
                        ModBusHelp.ReadModBus();
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("读取MOdBus数据失败,请检查IP地址是否正确");
                        IsRunning = false;
                    }
                }
            }));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 运行读取ModBus数据函数
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public Task RunGetModBus()
        {
            bool IsRunning = true;

            return(Task.Factory.StartNew(() =>
            {
                // 设置线程名称
                Thread.CurrentThread.Name = "存储数据";
                Thread.CurrentThread.IsBackground = true;
                Thread.CurrentThread.Priority = ThreadPriority.Normal;

                while (IsRunning)
                {
                    Thread.Sleep(2000);
                    try
                    {
                        // 运行读取ModBus函数
                        ModBusHelp.ReadModBus();
                    }
                    catch (Exception)
                    {
                        bool IsConnect = true;
                        while (IsConnect)
                        {
                            Thread.Sleep(5000);
                            try
                            {
                                ModBusHelp.ReadModBus();
                                IsConnect = false;
                                Console.WriteLine("重连成功");
                            }
                            catch (Exception)
                            {
                                Console.WriteLine("连接失败");
                            }
                        }
                    }
                }
            }, TaskCreationOptions.LongRunning));
        }