From 17e15356d82ea9367746be720807cd697951a6df Mon Sep 17 00:00:00 2001 From: Tyrone CT Date: Fri, 10 Oct 2025 23:08:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BA=86RtScanDeviceStart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CapMachine.Wpf/Services/PPCService.cs | 192 +++++++++----------------- 1 file changed, 66 insertions(+), 126 deletions(-) diff --git a/CapMachine.Wpf/Services/PPCService.cs b/CapMachine.Wpf/Services/PPCService.cs index 015016b..a62726b 100644 --- a/CapMachine.Wpf/Services/PPCService.cs +++ b/CapMachine.Wpf/Services/PPCService.cs @@ -223,49 +223,57 @@ namespace CapMachine.Wpf.Services //textBox5.Text = ""; - string hpath = SuperHeatCoolConfig.FluidsPath; - long size = hpath.Length; - - hpath += new String(' ', 255 - (int)size); - IRefProp64.SETPATHdll(hpath, ref size); - - long numComps = 1;//冷媒个数 - //string hfld = "R1234YF.FLD"; R1234YF - string hfld = ""; - if (SuperHeatCoolConfig.Cryogen == "R134a") + // 统一初始化(仅必要时),避免循环内重复 SETUP/SETPATH + if (!EnsureRefpropInitialized(out string initErrLoop)) { - hfld = "R134A.FLD"; + // 初始化失败:保护性置零并进入下一轮 + Superheat.PVModel.EngValue = 0; + Subcool.PVModel.EngValue = 0; + // 可选:记录 initErrLoop } else { - // 与其它实现保持一致:非 R134a 时使用 R1234YF - hfld = "R1234YF.FLD"; + // 在统一的锁内进行 REFPROP 计算,保证并发安全与状态一致性 + lock (_refpropLock) + { + // 建立纯工质组分(摩尔分数) + double[] xLoc = new double[20]; xLoc[0] = 1.0; + double[] xliqLoc = new double[20]; + double[] xvapLoc = new double[20]; + double DlLoc = 0, DvLoc = 0; + string herr2 = new string(' ', 255); + long herrLen2 = 255; + + // 计算过热度:吸气压力的饱和温度 + double pForSat = (InhPressTag.PVModel.EngValue) * 1000.0; // MPa->kPa + double teSat = 0; + IRefProp64.SATPdll(ref pForSat, xLoc, ref kph, ref teSat, ref DlLoc, ref DvLoc, xliqLoc, xvapLoc, ref iErr, ref herr2, ref herrLen2); + if (iErr == 0) + Superheat.PVModel.EngValue = InhTempTag.PVModel.EngValue - (teSat - 273.15); + else + Superheat.PVModel.EngValue = 0; + + // 计算过冷度:膨胀阀前压力的饱和温度 + double p1ForSat = (TxvFrPressTag.PVModel.EngValue) * 1000.0; // MPa->kPa + double te1Sat = 0; iErr = 0; herr2 = new string(' ', 255); herrLen2 = 255; DlLoc = 0; DvLoc = 0; + IRefProp64.SATPdll(ref p1ForSat, xLoc, ref kph, ref te1Sat, ref DlLoc, ref DvLoc, xliqLoc, xvapLoc, ref iErr, ref herr2, ref herrLen2); + if (iErr == 0) + Subcool.PVModel.EngValue = TxvFrTempTag.PVModel.EngValue - (te1Sat - 273.15); + else + Subcool.PVModel.EngValue = 0; + + // 干度计算(可重入锁,Monitor 为可重入锁,不会死锁) + try + { + double dryness = GetDrynessByTP(InhPressTag.PVModel.EngValue, InhTempTag.PVModel.EngValue, true); + if (DrynessTag != null) + { + DrynessTag.PVModel.EngValue = double.IsNaN(dryness) ? 0.0 : dryness; + } + } + catch { /* 不中断主循环 */ } + } } - //string hfld = "R134A.FLD"; - //string hfld = Convert.ToString(comboBox1.Text);//从控件获取数据 - size = hfld.Length; - - hfld += new String(' ', 10000 - (int)size); - - //string hfmix = "hmx.bnc" + new String(' ', 255);//原来的 - string hfmix = "hmx.bnc" + new String(' ', 255);//248 - string hrf = "DEF"; - string herr = new String(' ', 255); - - iErr = 0; - long hfldLen = hfld.Length, hfmixLen = hfmix.Length, hrfLen = hrf.Length, herrLen = herr.Length; - - //numComps = -1; - IRefProp64.SETUPdll(ref numComps, ref hfld, ref hfmix, ref hrf, ref iErr, ref herr, ref hfldLen, ref hfmixLen, ref hrfLen, ref herrLen); - - double version = iErr / 10000.0;//错误信息 - if (iErr != 0) - { - //MessageBox.Show(herr, "RefProp SETUPdll() Error ", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - IRefProp64.WMOLdll(x, ref wm); //p = Convert.ToDouble(textBox2.Text) * 1000.0;//textBox2 Comp.吸气压力(Mpa) p = (InhPressTag.PVModel.EngValue) * 1000.0;//textBox2 Comp.吸气压力(Mpa) @@ -273,35 +281,7 @@ namespace CapMachine.Wpf.Services p1 = (TxvFrPressTag.PVModel.EngValue) * 1000.0;//textBox3 Evap.膨胀阀前压力(Mpa) //p1 = Convert.ToDouble(textBox3.Text) * 1000.0;//textBox3 Evap.膨胀阀前压力(Mpa) - IRefProp64.SATPdll(ref p, x, ref kph, ref te, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); - - if (iErr == 0) - Superheat.PVModel.EngValue = InhTempTag.PVModel.EngValue - (te - 273.15); - //textBox5.Text = String.Format("{0:n4}", Convert.ToDouble(textBox1.Text) - (te - 273.15));//textBox1 Comp.吸气温度(℃) - else - Superheat.PVModel.EngValue = 0; - IRefProp64.SATPdll(ref p1, x, ref kph, ref te1, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); - if (iErr == 0) - Subcool.PVModel.EngValue = TxvFrTempTag.PVModel.EngValue - (te1 - 273.15);//textBox4 Evap.膨胀阀前温度(℃) - //GuoLengDu = (te1 - 273.15) - ListKRLogCellValue.Find(a => a.Name == "膨胀阀前温度").Value;//textBox4 Evap.膨胀阀前温度(℃) - //textBox6.Text = String.Format("{0:n4}", Convert.ToDouble(textBox4.Text) - (te1 - 273.15));//textBox4 Evap.膨胀阀前温度(℃) - else - Subcool.PVModel.EngValue = 0; - - // 干度计算与写入:保持与当前标签单位一致(MPa/℃ 转换在内部完成) - // 优先返回质量基干度,其次摩尔基;若均无效则置 0,避免出现 NaN 传入 UI - try - { - double dryness = GetDrynessByTP(InhPressTag.PVModel.EngValue, InhTempTag.PVModel.EngValue, true); - if (DrynessTag != null) - { - DrynessTag.PVModel.EngValue = double.IsNaN(dryness) ? 0.0 : dryness; - } - } - catch (Exception) - { - // 干度计算异常时不影响主循环,必要时可接入日志系统 - } + // 重复的 SATP 与干度计算已在上方 lock(_refpropLock) 中统一完成,此处移除重复调用 } catch (Exception ex) { @@ -334,51 +314,11 @@ namespace CapMachine.Wpf.Services //textBox5.Text = ""; - string hpath = @".\PPCalculation\REFPROP\FLUIDS"; - long size = hpath.Length; - - hpath += new String(' ', 255 - (int)size); - IRefProp64.SETPATHdll(hpath, ref size); - - long numComps = 1;//冷媒个数 - //string hfld = "R1234YF.FLD"; - //string hfld = "R134A.FLD"; - string hfld = ""; - if (SuperHeatCoolConfig.Cryogen == "R134a") + // 统一初始化(必要时) + if (!EnsureRefpropInitialized(out string initErr)) { - hfld = "R134A.FLD"; + return; // 初始化失败直接返回,避免无意义计算 } - else - { - hfld = "R1234YF.FLD"; - - } - - //string hfld = Convert.ToString(comboBox1.Text);//从控件获取数据 - size = hfld.Length; - - hfld += new String(' ', 10000 - (int)size); - - //string hfmix = "hmx.bnc" + new String(' ', 255);//原来的 - string hfmix = "hmx.bnc" + new String(' ', 248); - string hrf = "DEF"; - string herr = new String(' ', 255); - - iErr = 0; - long hfldLen = hfld.Length, hfmixLen = hfmix.Length, hrfLen = hrf.Length, herrLen = herr.Length; - - //numComps = -1; - IRefProp64.SETUPdll(ref numComps, ref hfld, ref hfmix, ref hrf, ref iErr, ref herr, ref hfldLen, ref hfmixLen, ref hrfLen, ref herrLen); - - - double version = iErr / 10000.0;//错误信息 - if (iErr != 0) - { - //MessageBox.Show(herr, "RefProp SETUPdll() Error ", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - IRefProp64.WMOLdll(x, ref wm); //p = Convert.ToDouble(textBox2.Text) * 1000.0;//textBox2 Comp.吸气压力(Mpa) //p = (ListRtKPMeter.Find(a => a.MeterName == "吸入压力").RtPV) * 1000.0;//textBox2 Comp.吸气压力(Mpa) @@ -390,25 +330,25 @@ namespace CapMachine.Wpf.Services //p1 = Convert.ToDouble(textBox3.Text) * 1000.0;//textBox3 Evap.膨胀阀前压力(Mpa) - IRefProp64.SATPdll(ref p, x, ref kph, ref te, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); + // 在同一把锁中执行 REFPROP 计算,保证并发安全 + lock (_refpropLock) + { + string herr = new string(' ', 255); long herrLen = 255; + IRefProp64.SATPdll(ref p, x, ref kph, ref te, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); - if (iErr == 0) + if (iErr == 0) + Superheat.PVModel.EngValue = InhTempTag.PVModel.EngValue - (te - 273.15); + else + Superheat.PVModel.EngValue = 0; - //GuoReDu = ListRtKPMeter.Find(a => a.MeterName == "吸入温度").RtPV - (te - 273.15); - Superheat.PVModel.EngValue = InhTempTag.PVModel.EngValue - (te - 273.15); - //textBox5.Text = String.Format("{0:n4}", Convert.ToDouble(textBox1.Text) - (te - 273.15));//textBox1 Comp.吸气温度(℃) - else - Superheat.PVModel.EngValue = 0; + herr = new string(' ', 255); herrLen = 255; iErr = 0; + IRefProp64.SATPdll(ref p1, x, ref kph, ref te1, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); + if (iErr == 0) + Subcool.PVModel.EngValue = TxvFrTempTag.PVModel.EngValue - (te1 - 273.15); - IRefProp64.SATPdll(ref p1, x, ref kph, ref te1, ref Dl, ref Dv, xliq, xvap, ref iErr, ref herr, ref herrLen); - if (iErr == 0) - //GuoLengDu = ListKRLogCellValue.Find(a => a.Name == "膨胀阀前温度").Value - (te1 - 273.15);//textBox4 Evap.膨胀阀前温度(℃) - Subcool.PVModel.EngValue = TxvFrTempTag.PVModel.EngValue - (te1 - 273.15);//textBox4 Evap.膨胀阀前温度(℃) - - //textBox6.Text = String.Format("{0:n4}", Convert.ToDouble(textBox4.Text) - (te1 - 273.15));//textBox4 Evap.膨胀阀前温度(℃) - - else - Subcool.PVModel.EngValue = 0; + else + Subcool.PVModel.EngValue = 0; + } } catch (Exception ex)