166 lines
5.9 KiB
C#
166 lines
5.9 KiB
C#
using OfficeOpenXml;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.Data.Common;
|
||
using System.IO;
|
||
|
||
namespace 鞋子止滑性能测试仪
|
||
{
|
||
public class Data_Change
|
||
{
|
||
/// <summary>
|
||
/// 将ADC值转换为实际值
|
||
/// </summary>
|
||
/// <param name="shuruzhi">读取原始数据</param>
|
||
/// 读取原始数据
|
||
/// <param name="jiaozhunzhi">校准零点值</param>
|
||
/// <param name="xishu">压力系数</param>
|
||
/// <returns></returns>
|
||
public float Change_ADC_ToFloat(int shuruzhi, int jiaozhunzhi, float xishu)
|
||
{
|
||
int A = shuruzhi - jiaozhunzhi;
|
||
float output = Convert.ToSingle(A)/xishu; ;
|
||
return output;
|
||
}
|
||
/// <summary>
|
||
/// 修改配置文件
|
||
/// </summary>
|
||
/// <param name="duixiang"></param>
|
||
/// <param name="xiugaizhi"></param>
|
||
/// <returns></returns>
|
||
public bool ChangeData(string duixiang, string xiugaizhi)
|
||
{
|
||
bool isSuccess = false;
|
||
try
|
||
{
|
||
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
||
cfa.AppSettings.Settings[duixiang].Value = xiugaizhi;
|
||
cfa.Save();
|
||
ConfigurationManager.RefreshSection("appSettings");
|
||
string connStr = ConfigurationManager.AppSettings[duixiang].ToString();
|
||
if (connStr == xiugaizhi)
|
||
|
||
{
|
||
isSuccess = true;
|
||
|
||
}
|
||
else
|
||
{
|
||
isSuccess = false;
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
isSuccess = false;
|
||
|
||
}
|
||
|
||
return isSuccess;
|
||
|
||
}
|
||
/// <summary>
|
||
/// ushort转为float类型
|
||
/// </summary>
|
||
/// <param name="P1"></param>
|
||
/// <param name="P2"></param>
|
||
/// <returns>float型数据</returns>
|
||
public float UshortToFloat(ushort P1, ushort P2)
|
||
{
|
||
int intSign, intSignRest, intExponent, intExponentRest;
|
||
float faResult, faDigit;
|
||
intSign = P1 / 32768;
|
||
intSignRest = P1 % 32768;
|
||
intExponent = intSignRest / 128;
|
||
intExponentRest = intSignRest % 128;
|
||
faDigit = (float)(intExponentRest * 65536 + P2) / 8388608;
|
||
faResult = (float)Math.Pow(-1, intSign) * (float)Math.Pow(2, intExponent - 127) * (faDigit + 1);
|
||
return faResult;
|
||
}
|
||
/// <summary>
|
||
/// ushort转为int类型
|
||
/// </summary>
|
||
/// <param name="u1"></param>
|
||
/// <param name="u2"></param>
|
||
/// <returns>返回int型数据</returns>
|
||
public int UshortToInt1(ushort u1, ushort u2)
|
||
{
|
||
ushort[] maidong = new ushort[2] { u1, u2 };
|
||
byte[] bytes = new byte[maidong.Length * 2];
|
||
Buffer.BlockCopy(maidong, 0, bytes, 0, bytes.Length);
|
||
int result = BitConverter.ToInt32(bytes, 0);
|
||
// 将字节数组转换为32位无符号整数
|
||
return result;
|
||
|
||
}
|
||
/// <summary>
|
||
/// Float转为Ushort数组发送
|
||
/// </summary>
|
||
/// <param name="value"></param>
|
||
/// <returns>返回ushort数组</returns>
|
||
public ushort[] SplitFloatToUShortArray(float value)
|
||
{
|
||
byte[] floatBytes = BitConverter.GetBytes(value);
|
||
ushort[] ushortArray = new ushort[floatBytes.Length / 2];
|
||
|
||
for (int i = 0, j = 0; i < floatBytes.Length; i += 2, j++)
|
||
{
|
||
ushortArray[j] = BitConverter.ToUInt16(floatBytes, i);
|
||
}
|
||
|
||
return ushortArray;
|
||
}
|
||
/// <summary>
|
||
/// Int转为ushort数组发送
|
||
/// </summary>
|
||
/// <param name="res"></param>
|
||
/// <returns>返回ushort数组</returns>
|
||
public ushort[] intToushorts(int res)
|
||
{
|
||
ushort ust1 = (ushort)(res >> 16);
|
||
ushort ust2 = (ushort)res;
|
||
return new ushort[] { ust2, ust1 };
|
||
}
|
||
public void ExportDataTableToExcel(List<string> list1, List<float> list2, List<float> list3, List<float> list4, List<float> list5)
|
||
{
|
||
ExcelPackage.LicenseContext = OfficeOpenXml.LicenseContext.NonCommercial;
|
||
// 获取保存路径
|
||
string saveDirectory = @"D:\文件保存\" + DateTime.Now.ToString("yyyyMMdd") + @"\";
|
||
string fileName = "Export_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
|
||
string fullPath = Path.Combine(saveDirectory, fileName);
|
||
|
||
// 创建目录(如果不存在)
|
||
Directory.CreateDirectory(saveDirectory);
|
||
|
||
// 开始导出
|
||
using (ExcelPackage excelPackage = new ExcelPackage())
|
||
{
|
||
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1");
|
||
|
||
// 写入表头
|
||
int colIndex = 1;
|
||
worksheet.Cells[1, colIndex].Value = "时间(0.01s)";
|
||
worksheet.Cells[1, colIndex+1].Value = "正压力(N)";
|
||
worksheet.Cells[1, colIndex + 2].Value = "摩擦力(N)";
|
||
worksheet.Cells[1, colIndex + 3].Value = "位移量(mm)";
|
||
worksheet.Cells[1, colIndex + 4].Value = "摩擦系数";
|
||
|
||
// 写入数据
|
||
for(int i = 2; i < list1.Count; i++)
|
||
{
|
||
worksheet.Cells[i, 1].Value = list1[i];
|
||
worksheet.Cells[i, 2].Value = list2[i];
|
||
worksheet.Cells[i, 3].Value = list3[i];
|
||
worksheet.Cells[i, 4].Value = list4[i];
|
||
worksheet.Cells[i, 5].Value = list5[i];
|
||
}
|
||
// 保存文件
|
||
var file = new FileInfo(fullPath);
|
||
excelPackage.SaveAs(file);
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|