/// <summary> /// 获取主轴刀号信息 /// </summary> private void GetMainToolsInfo() { TmsDeviceToolsPos toolsPos = gridChangeTools.SelectedItem as TmsDeviceToolsPos; if (toolsPos == null) { return; } //获取主轴刀号 FmsAssetTagSetting tagSetting = wsFMS.UseService( s => s.GetFmsAssetTagSettings( $"USE_FLAG = 1 AND ASSET_CODE = '{toolsPos.DEVICE_PKNO}' AND TAG_NAME = '主轴刀号'")) .FirstOrDefault(); if (tagSetting == null) { tbMainToolsNO.Text = "【主轴刀号】的Tag地址未设置!"; return; } FmsAssetCommParam device = wsFMS.UseService(s => s.GetFmsAssetCommParams($"USE_FLAG = 1 AND ASSET_CODE = '{toolsPos.DEVICE_PKNO}'")) .FirstOrDefault(); if (device == null) { tbMainToolsNO.Text = "设备未设置通讯信息!"; return; } string commAddress = device.COMM_ADDRESS; DeviceCommInterface interfaceType = EnumHelper.ParserEnumByValue(device.INTERFACE_TYPE, DeviceCommInterface.CNC_Fanuc); int period = Convert.ToInt32(device.SAMPLING_PERIOD); //采样周期 DeviceManager deviceCommunication = new DeviceManager(device.PKNO, interfaceType, commAddress, period * 1000); List <DeviceTagParam> deviceTags = new List <DeviceTagParam>(); 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.AutoReadDevice), deviceCommunication); //通讯参数 deviceTags.Add(deviceTag); //添加 deviceCommunication.InitialDevice(deviceTags, null); string error = ""; string sResult = deviceCommunication.SyncReadData(tagSetting.TAG_ADDRESS, out error); if (error != "") { mainToolsNO = sResult; } tbMainToolsNO.Text = (error == "") ? "主轴刀号:T" + sResult : "读取主轴刀号错误,代码 " + error; }
/// <summary> /// 读取DA数据 /// </summary> /// <param name="device"></param> private void ThreadGetDaMonitor(FmsAssetCommParam device) { if ((!bMonitor) || (device == null)) { return; //未开启监控 } int readSpan = 0; //设定采样周期的变量 #region 获取基础参数 string deviceCode = device.ASSET_CODE; DeviceCommInterface interfaceType = EnumHelper.ParserEnumByValue(device.INTERFACE_TYPE, DeviceCommInterface.CNC_Fanuc); string commAddress = device.COMM_ADDRESS; int period = Convert.ToInt32(device.SAMPLING_PERIOD); //采样周期 List <FmsAssetTagSetting> tagSettings = _tagSettings.Where(c => c.ASSET_CODE == deviceCode && c.SAMPLING_MODE == 3).ToList(); //DA采集模式 if (tagSettings.Count <= 0) { return; } #endregion DeviceManager deviceCommunication = new DeviceManager(CBaseData.NewGuid(), 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)) //读取数据 { string error = ""; lock (locakDA) { List <FmsAssetTagSetting> daTags = tagSettings.Where(c => c.SAMPLING_MODE == 3).ToList(); //这些为Focas的 if (daTags.Any()) { #region 采集实时信息 DAMachineRealTimeInfo realTimeInfo = ws_DA.UseService( s => s.GetDAMachineRealTimeInfos("ASSET_CODE = " + device.ASSET_CODE + "")) .FirstOrDefault(); if (realTimeInfo != null) { //实况信息采集和录入 realTimeInfo.STATUS = int.Parse(deviceCommunication.SyncReadData("状态", out error)); realTimeInfo.MAIN_PROG = deviceCommunication.SyncReadData("程序号", out error); realTimeInfo.SPINDLE_OVERRIDE = deviceCommunication.SyncReadData("主轴负载", out error); realTimeInfo.SPINDLE_SPPED = deviceCommunication.SyncReadData("主轴转速", out error); realTimeInfo.FEED_SPEED = deviceCommunication.SyncReadData("进给速度", out error); realTimeInfo.FEED_RATE = deviceCommunication.SyncReadData("进给倍率", out error); realTimeInfo.DA_TIME = DateTime.Now; ws_DA.UseService(s => s.UpdateDAMachineRealTimeInfo(realTimeInfo)); //实时更新 } #endregion #region 采集产量信息 //设备产量记录采集 DAProductRecord productRecord = ws_DA.UseService(s => s.GetDAProductRecords($"ASSET_CODE = '{device.ASSET_CODE}'")) .OrderByDescending(c => c.END_TIME) .FirstOrDefault(); //获取生产记录 if (productRecord != null) { int part_num = 0; int.TryParse(deviceCommunication.SyncReadData("工件数", out error), out part_num); int total_num = 0; int.TryParse(deviceCommunication.SyncReadData("工件总数", out error), out total_num); if (part_num == productRecord.PART_NUM) { if (productRecord.END_TIME <= DateTime.Now.AddMinutes(-5)) //每5分钟,更新数据库 { productRecord.END_TIME = DateTime.Now; ws_DA.UseService(s => s.UpdateDAProductRecord(productRecord)); } } else //不一致 { productRecord.END_TIME = DateTime.Now; ws_DA.UseService(s => s.UpdateDAProductRecord(productRecord)); //插入新的纪录 DAProductRecord newProductRecord = new DAProductRecord(); newProductRecord.ASSET_CODE = deviceCode; newProductRecord.PKNO = Guid.NewGuid().ToString("N"); newProductRecord.START_TIME = DateTime.Now; newProductRecord.END_TIME = DateTime.Now.AddMilliseconds(100); newProductRecord.PART_NUM = part_num; newProductRecord.TOTAL_PART_NUM = total_num; ws_DA.UseService(s => s.AddDAProductRecord(newProductRecord)); //新增当前值到数据库 } } #endregion #region 采集报警信息 string a = deviceCommunication.SyncReadData("报警信息", out error); #endregion } } } readSpan = 0; } catch (Exception ex) { Console.WriteLine("ThreadGetDAMonitor, 错误为:" + ex.Message); } System.Threading.Thread.Sleep(100); } }
/// <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); } }