499 lines
19 KiB
C#
499 lines
19 KiB
C#
using Modbus.Device;
|
||
using Sunny.UI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using HslCommunication.Algorithms.Fourier;
|
||
using 激光闪光法测试仪.DAQ;
|
||
using 激光闪光法测试仪.Data;
|
||
using 激光闪光法测试仪.PLC;
|
||
using 激光闪光法测试仪.Report;
|
||
using System.Linq;
|
||
using OfficeOpenXml;
|
||
using System.IO;
|
||
|
||
namespace 激光闪光法测试仪
|
||
{
|
||
public partial class FromMain : UIForm
|
||
{
|
||
DataChange dc = new DataChange();
|
||
string ip = null;
|
||
Modbus_PLC plc;
|
||
Function Fc;
|
||
ModbusMaster master;
|
||
int port = 0;
|
||
BoolSignal bool_Stop = new BoolSignal();
|
||
Result_Calculation result_Calculation = new Result_Calculation();
|
||
Calculate_Parameters calculate_Parameters = new Calculate_Parameters();
|
||
public static int _step = 1;
|
||
bool _is_Created = false;
|
||
public static bool isEnter = false;//是否输入了参数
|
||
DataTable dt = new DataTable();
|
||
public FromMain()
|
||
{
|
||
InitializeComponent();
|
||
//switch (int.Parse(ConfigurationManager.AppSettings["SampleRate"]))
|
||
//{
|
||
// case 1000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate1K;
|
||
// break;
|
||
// case 5000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate5K;
|
||
// break;
|
||
// case 10000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate10K;
|
||
// break;
|
||
// case 50000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate50K;
|
||
// break;
|
||
// case 100000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate100K;
|
||
// break;
|
||
// case 200000:
|
||
// SAMPLE_RATE = DAQSampleRate.SampleRate200K;
|
||
// break;
|
||
|
||
|
||
//}
|
||
|
||
formsPlot11.Plot.Title("BackTemperatur vs. Time");
|
||
formsPlot11.Plot.XLabel("Times(s)");
|
||
formsPlot11.Plot.YLabel("Voltage(V)");
|
||
formsPlot11.Refresh();
|
||
|
||
dt.Columns.Add("日期");
|
||
dt.Columns.Add("时间");
|
||
dt.Columns.Add("比热容(J/(kg*K))");
|
||
dt.Columns.Add("密度(kg/m³)");
|
||
dt.Columns.Add("厚度(mm)");
|
||
dt.Columns.Add("半升温时间(s)", typeof(DateTime));
|
||
dt.Columns.Add("热扩散系数(m2/s)");
|
||
dt.Columns.Add("导热系数(W/(m2*K))");
|
||
uiDataGridView1.DataSource = dt;
|
||
uiDataGridView1.Columns[2].Width = 200;
|
||
uiDataGridView1.Columns[3].Width = 150;
|
||
uiDataGridView1.Columns[4].Width = 150;
|
||
uiDataGridView1.Columns[5].Width = 200;
|
||
uiDataGridView1.Columns[6].Width = 200;
|
||
uiDataGridView1.Columns[7].Width = 200;
|
||
//// 设置所有单元格内容居中
|
||
uiDataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
||
|
||
// 设置行标题居中(可选)
|
||
uiDataGridView1.RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
|
||
bool_Stop.OnRisingEdge += bool_Stop_OnRisingEdge;
|
||
|
||
//获取IP地址和端口号
|
||
ip = "192.168.1.10";
|
||
port = 502;
|
||
Task.Run(() =>
|
||
{
|
||
plc = new Modbus_PLC(ip, port);
|
||
try
|
||
{
|
||
bool IsConnect = plc.ConnectPLC();
|
||
if (!IsConnect)
|
||
MessageBox.Show("Erro!");
|
||
else
|
||
{
|
||
master = plc.GetMaster();
|
||
Fc = new Function(master);
|
||
ReadPLCData();
|
||
tsBtn_系统设置.Enabled = true;
|
||
uiGroupBox1.Enabled = true;
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message);
|
||
}
|
||
});
|
||
daq122_ = new DAQ122();
|
||
Task.Run(() =>
|
||
{
|
||
try
|
||
{
|
||
// 有Create就要有DeleteDevice,否则可能造成内存泄露
|
||
daq122_.DeleteDevice();
|
||
daq122_.CreateDevice();
|
||
}
|
||
catch
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
UIMessageBox.ShowError("DAQ122设备初始化失败1");
|
||
return;
|
||
}));
|
||
}
|
||
// 初始化DAQ122设备
|
||
var result = daq122_.InitializeDevice();
|
||
if (result == false)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
UIMessageBox.ShowError("DAQ122设备初始化失败");
|
||
}));
|
||
Console.WriteLine("DAQ122设备初始化失败");
|
||
return;
|
||
}
|
||
Console.WriteLine("DAQ122设备初始化成功");
|
||
// 连接DAQ122设备
|
||
result = daq122_.ConnectedDevice();
|
||
if (result == false)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
UIMessageBox.ShowError("DAQ122设备连接失败");
|
||
}));
|
||
return;
|
||
}
|
||
Console.WriteLine("DAQ122连接成功");
|
||
// 配置DAQ122采样率
|
||
var config_result = daq122_.ConfigureADCSamplingParameters(VOLTAGE_RANGE, SAMPLE_RATE);
|
||
if (config_result == false)
|
||
{
|
||
Console.WriteLine("DAQ122配置电压失败");
|
||
return;
|
||
}
|
||
Console.WriteLine("DAQ122设备配置成功");
|
||
// 配置使用哪些ADC通道
|
||
daq122_.ConfigADCChannel(OPEN_CHANNEL);
|
||
_is_Created = true;
|
||
});
|
||
Task.Run(() =>
|
||
{
|
||
while (true)
|
||
{
|
||
bool_Stop.Value = bools[1];
|
||
bool_Stop.CheckRisingEdge();
|
||
Thread.Sleep(10);
|
||
}
|
||
});
|
||
|
||
_step = ConfigurationManager.AppSettings["step"].ToInt();
|
||
}
|
||
void bool_Stop_OnRisingEdge()
|
||
{
|
||
timer1.Stop();
|
||
if (_is_Created)
|
||
daq122_.StopADCCollection();
|
||
}
|
||
#region 连接采集器配置定义
|
||
// 新建DAQ122控制类
|
||
private DAQ122 daq122_;
|
||
|
||
// 配置采样率为200K
|
||
//后续更改为可设置
|
||
private static DAQSampleRate SAMPLE_RATE = DAQSampleRate.SampleRate50K;
|
||
|
||
// 配置电压量程为5V
|
||
private static DAQVoltage VOLTAGE_RANGE = DAQVoltage.Voltage10V;
|
||
|
||
// 配置采集只采集前1个通道
|
||
private static UInt32 OPEN_CHANNEL = (UInt32)(DAQADCChannel.AIN1);
|
||
|
||
|
||
// 配置8个通道
|
||
// SignalPlot[] plt_list = new SignalPlot[8];
|
||
// 配置8个通道的颜色
|
||
Color[] color_list = {
|
||
Color.Red , Color.Black, Color.Blue , Color.DarkGreen ,
|
||
Color.Turquoise,Color.Magenta ,Color.SaddleBrown,Color.Orange
|
||
};
|
||
|
||
// 设置缓冲区大小为采样率大小
|
||
private static UInt32 RECEIVE_BUFFER_SIZE = (UInt32)SAMPLE_RATE;
|
||
// 设置缓冲区
|
||
double[][] receive_data_ = {
|
||
new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE],
|
||
new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE], new double[RECEIVE_BUFFER_SIZE]
|
||
};
|
||
|
||
public delegate void RefreshFomrsplot();
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 温度高速读取并存入列表中
|
||
/// </summary>
|
||
List<double> dataList = new List<double>();
|
||
private void timer1_Tick(object sender, EventArgs e)
|
||
{
|
||
// 采集数据
|
||
// 判断当前缓冲区是否符合读取的要求
|
||
if (!daq122_.ADCDataBufferIsValid(RECEIVE_BUFFER_SIZE))
|
||
{
|
||
return;
|
||
}
|
||
// 读取数据
|
||
bool read_result = daq122_.TryReadData((UInt32)0, ref receive_data_[0][0], RECEIVE_BUFFER_SIZE, 250);
|
||
if (!read_result)
|
||
{
|
||
MessageBox.Show("数据读取失败!");
|
||
return;
|
||
}
|
||
dataList.AddRange(receive_data_[0]);
|
||
double[][] tempReceive = { dataList.ToArray() };
|
||
formsPlot11.Plot.AddSignal(tempReceive[0], RECEIVE_BUFFER_SIZE, color_list[0]);
|
||
//formsPlot11.Plot.XAxis.
|
||
formsPlot11.Refresh();
|
||
}
|
||
/// <summary>
|
||
/// 开始采集温度数据
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
var satrt_result = daq122_.StartADCCollection();
|
||
if (satrt_result == false)
|
||
{
|
||
MessageBox.Show("satrt_result is {0}", satrt_result.ToString());
|
||
// Console.WriteLine("satrt_result is {0}", satrt_result);
|
||
return;
|
||
}
|
||
formsPlot11.Plot.Clear();
|
||
dataList.Clear();
|
||
timer1.Start();
|
||
//计算耗时
|
||
// 创建Stopwatch实例
|
||
Stopwatch sw = Stopwatch.StartNew();
|
||
// Fc.BtnClickFunction(Function.ButtonType.复归型, 100);
|
||
// 停止计时并输出
|
||
sw.Stop();
|
||
sw_time = sw.Elapsed.TotalMilliseconds;
|
||
lb_状态.Text = "测试中";
|
||
formsPlot11.Plot.XLabel("Times(s)");
|
||
|
||
}
|
||
double sw_time = 0.0;
|
||
/// <summary>
|
||
/// 停止采集温度数据
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
// Fc.BtnClickFunction(Function.ButtonType.复归型, 103);
|
||
timer1.Stop();
|
||
daq122_.StopADCCollection();
|
||
}
|
||
//平滑曲线后显示
|
||
List<double> list_ForHalf = new List<double>();
|
||
List<List<double>> list_All = new List<List<double>>();
|
||
List<string> pic_List = new List<string>();
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
if (isEnter)
|
||
{
|
||
formsPlot11.Plot.Clear();
|
||
formsPlot11.Plot.XLabel("Index");
|
||
Task.Run(() =>
|
||
{
|
||
double[] dataHalf = dataList.ToArray();
|
||
// double[] dataHalf_FFT= FFTFilter.FilterFFT(dataHalf,uiTextBox1.Text.ToDouble());
|
||
|
||
list_ForHalf = result_Calculation.Smoothing(dataList, _step);
|
||
|
||
Thread.Sleep(500);
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
uiProcessBar1.Value = 25;
|
||
}));
|
||
|
||
//
|
||
double[][] tempReceive = { list_ForHalf.ToArray() };
|
||
// double[][] tempReceive = { dataHalf };
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
formsPlot11.Plot.AddSignal(tempReceive[0], list_ForHalf.Count, color_list[0]);
|
||
Thread.Sleep(500);
|
||
uiProcessBar1.Value = 50;
|
||
|
||
formsPlot11.Refresh();
|
||
//得到半升温时间
|
||
// list_ForHalf.AddRange(dataHalf_FFT);
|
||
lb_halfTime.Text = (result_Calculation.CalculateHalfRiseTime(list_ForHalf, Convert.ToDouble(RECEIVE_BUFFER_SIZE))).ToString("F5");
|
||
calculate_Parameters.HalfTime = lb_halfTime.Text.ToDouble();
|
||
//计算热扩散系数
|
||
calculate_Parameters.Thermal_diffusivity = 0.13879 * (((calculate_Parameters.Thickness / 1000.0) * (calculate_Parameters.Thickness / 1000.0)) / calculate_Parameters.HalfTime) * calculate_Parameters.Xishu;
|
||
lb_热扩散系数.Text = calculate_Parameters.Thermal_diffusivity.ToString("F2");
|
||
//计算导热系数
|
||
calculate_Parameters.Thermal_conductivity = calculate_Parameters.Density *
|
||
calculate_Parameters.Thermal_diffusivity *
|
||
calculate_Parameters.Specific_heat_capacity;
|
||
dt.Rows.Add(DateTime.Now.ToShortDateString(),
|
||
DateTime.Now.ToString("HH:mm:ss"),
|
||
calculate_Parameters.Specific_heat_capacity,
|
||
calculate_Parameters.Density,
|
||
calculate_Parameters.Thickness,
|
||
calculate_Parameters.HalfTime,
|
||
calculate_Parameters.Thermal_diffusivity.ToString("F7"),
|
||
calculate_Parameters.Thermal_conductivity.ToString("F3"));
|
||
lb_热扩散系数.Text = calculate_Parameters.Thermal_diffusivity.ToString("F7");
|
||
lb_导热系数.Text = calculate_Parameters.Thermal_conductivity.ToString("F3");
|
||
#region 数据结果存入内存
|
||
List<double> tempList = new List<double>(list_ForHalf);
|
||
list_All.Add(tempList);
|
||
//存Picture
|
||
string pic_name = "D:\\Picture\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
|
||
formsPlot11.Plot.SaveFig(pic_name);
|
||
pic_List.Add(pic_name);
|
||
#endregion
|
||
uiProcessBar1.Value = 100;
|
||
lb_状态.Text = "测试完成";
|
||
}));
|
||
|
||
});
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("请先录入样品信息!", "提示", MessageBoxButtons.OK);
|
||
return;
|
||
}
|
||
}
|
||
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK);
|
||
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region PLC数据读取
|
||
ushort[] ushorts = new ushort[2];
|
||
ushort[] ushorts_lu = new ushort[2];
|
||
bool[] bools = new bool[2];
|
||
float _backTempture = 0.0f;
|
||
float _luTempture = 0.0f;
|
||
void ReadPLCData()
|
||
{
|
||
while (true)
|
||
{
|
||
try
|
||
{
|
||
ushorts = master.ReadHoldingRegisters(1, 1354, 2);
|
||
_backTempture = dc.UshortToFloat(ushorts[1], ushorts[0]);
|
||
|
||
ushorts_lu = master.ReadHoldingRegisters(1, 2504, 2);
|
||
_luTempture = dc.UshortToFloat(ushorts_lu[1], ushorts_lu[0]);
|
||
bools = master.ReadCoils(1, 101, 2);
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
lb_炉温.Text = _luTempture.ToString("F1");
|
||
lb_背温.Text = _backTempture.ToString("F2");
|
||
if (bools[0])
|
||
btn_测试开始.Text = "测试中";
|
||
else
|
||
btn_测试开始.Text = "测试";
|
||
}));
|
||
Thread.Sleep(10);
|
||
}
|
||
catch { break; }
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
private void toolStripLabel1_Click(object sender, EventArgs e)
|
||
{
|
||
frm_Setting _Setting = new frm_Setting(master);
|
||
_Setting.ShowDialog();
|
||
}
|
||
|
||
private void tsBtn_样品信息_Click(object sender, EventArgs e)
|
||
{
|
||
frm_Add _Add = new frm_Add(calculate_Parameters);
|
||
_Add.ShowDialog();
|
||
}
|
||
Report_Save report = new Report_Save();
|
||
private void button1_Click(object sender, EventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void button1_Click_1(object sender, EventArgs e)
|
||
{
|
||
report.WriteAndSaveExcel
|
||
("123",
|
||
"DeviceName",
|
||
"890",
|
||
"123",
|
||
"0.1",
|
||
"0.333",
|
||
"D:\\Picture\\20250513095914237.png");
|
||
}
|
||
|
||
private void uiButton1_Click_1(object sender, EventArgs e)
|
||
{
|
||
//将list中的数据导出到Excle
|
||
//list_ForHalf
|
||
ExportListsToExcel(dataList, list_ForHalf);
|
||
}
|
||
public static void ExportListsToExcel(List<double> list1, List<double> list2)
|
||
{
|
||
// 设置EPPlus非商业许可证(如果是商业用途需要购买授权)
|
||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||
|
||
try
|
||
{
|
||
// 1. 准备存储路径
|
||
string basePath = @"D:\文件存储";
|
||
string fileName = $"数据导出_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
|
||
string fullPath = Path.Combine(basePath, fileName);
|
||
|
||
// 确保目录存在
|
||
Directory.CreateDirectory(basePath);
|
||
|
||
// 2. 创建Excel文件
|
||
using (ExcelPackage package = new ExcelPackage())
|
||
{
|
||
// 添加工作表
|
||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("数据");
|
||
|
||
// 3. 写入数据
|
||
// 列标题
|
||
worksheet.Cells[1, 1].Value = "数据集1";
|
||
worksheet.Cells[1, 2].Value = "数据集2";
|
||
|
||
// 写入List1到第一列(从第2行开始)
|
||
for (int i = 0; i < list1.Count; i++)
|
||
{
|
||
worksheet.Cells[i + 2, 1].Value = list1[i];
|
||
}
|
||
|
||
// 写入List2到第二列(从第2行开始)
|
||
for (int i = 0; i < list2.Count; i++)
|
||
{
|
||
worksheet.Cells[i + 2, 2].Value = list2[i];
|
||
}
|
||
|
||
// 4. 自动调整列宽
|
||
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
|
||
|
||
// 5. 保存文件
|
||
package.SaveAs(new FileInfo(fullPath));
|
||
}
|
||
MessageBox.Show("$\"导出成功!文件已保存到:{fullPath}\"");
|
||
Console.WriteLine($"导出成功!文件已保存到:{fullPath}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"导出失败:{ex.Message}");
|
||
throw;
|
||
}
|
||
}
|
||
}
|
||
}
|