Example #1
0
        /// <summary>
        /// 读取设备线程
        /// </summary>
        /// <param name="device">设备</param>
        private void TheadGetDeviceData(FmsAssetCommParam device)
        {
            if ((!bMonitor) || (device == null))
            {
                return;        //未开启监控
            }
            int readSpan = 0;  //设定采样周期的变量

            #region 获取基础参数

            int period = Convert.ToInt32(device.SAMPLING_PERIOD);  //采样周期
            if (period <= 0)
            {
                return;             //采样周期<=0时不采样
            }
            string deviceCode = device.ASSET_CODE;
            DeviceCommInterface interfaceType = EnumHelper.ParserEnumByValue(device.INTERFACE_TYPE, DeviceCommInterface.CNC_Fanuc);
            string commAddress = device.COMM_ADDRESS;

            List <FmsAssetTagSetting> tagSettings = _tagSettings.Where(c => c.ASSET_CODE == deviceCode).ToList();  //

            if (tagSettings.Count <= 0)
            {
                return;
            }

            #endregion

            DeviceManager deviceCommunication = new DeviceManager(device.ASSET_CODE,
                                                                  interfaceType, commAddress, period * 1000, "", "");

            List <DeviceTagParam> deviceTags = new List <DeviceTagParam>();

            foreach (var tagSetting in tagSettings)
            {
                DeviceTagParam deviceTag = new DeviceTagParam(tagSetting.PKNO, tagSetting.TAG_CODE,
                                                              tagSetting.TAG_NAME, tagSetting.TAG_ADDRESS,
                                                              EnumHelper.ParserEnumByValue(tagSetting.VALUE_TYPE, TagDataType.Default),
                                                              EnumHelper.ParserEnumByValue(tagSetting.SAMPLING_MODE, DataSimplingMode.ReadAndWrite),
                                                              deviceCommunication); //通讯参数

                deviceTags.Add(deviceTag);                                          //添加
            }

            deviceCommunication.InitialDevice(deviceTags, SaveData);

            while (!CBaseData.AppClosing)
            {
                #region 暂停

                if (bPause)
                {
                    System.Threading.Thread.Sleep(200);
                    continue;
                }

                #endregion

                try
                {
                    readSpan++;

                    if ((period > 0) && (readSpan % (period * 10) == 0)) //读取数据
                    {
                        #region 定期刷新数据 - 目前针对OPC订阅方式

                        if (deviceCommunication.DeviceCore is OpcClassicManager)  //如果是OPC读取方式
                        {
                            var ret = ((OpcClassicManager)deviceCommunication.DeviceCore).RefreshData();
                            if (!ret.IsSuccess) //错误时
                            {
                                EventLogger.Log("数据刷新失败,错误为:" + ret.Message);
                                continue;
                            }
                        }

                        #endregion

                        #region 正常读取

                        List <FmsAssetTagSetting> readTags =
                            tagSettings.Where(c => c.SAMPLING_MODE == 0 || c.SAMPLING_MODE == 10).ToList(); //这些需要读取的。

                        foreach (FmsAssetTagSetting curSetting in readTags)
                        {
                            try
                            {
                                if (curSetting.SAMPLING_MODE == 11)
                                {
                                    continue;                                                                   //暂时不读
                                }
                                Thread.Sleep(50);                                                               //防止读取过快

                                OperateResult read = deviceCommunication.AsyncReadData(curSetting.TAG_ADDRESS); //异步读取数据

                                if (!read.IsSuccess)                                                            //错误时
                                {
                                    curSetting.CUR_VALUE = "";
                                    EventLogger.Log($"读取[{curSetting.TAG_ADDRESS}]失败,错误为:" + read.Message);
                                    continue;
                                }

                                //读取成功,读取数据有结果时,一般为同步读取数据时
                                OperateResult <string> result = read as OperateResult <string>;
                                if (result != null)
                                {
                                    SaveData(curSetting.PKNO, result.Content);
                                }
                            }
                            catch (Exception ex)
                            {
                                EventLogger.Log($"读取设备[{device.ASSET_CODE}]的值错误,错误为:", ex);
                            }
                        }

                        #endregion

                        readSpan = 0;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("读取失败, 错误为:" + ex.Message);
                }

                System.Threading.Thread.Sleep(100);
            }
        }