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
{
///
/// 将ADC值转换为实际值
///
/// 读取原始数据
/// 读取原始数据
/// 校准零点值
/// 压力系数
///
public float Change_ADC_ToFloat(int shuruzhi, int jiaozhunzhi, float xishu)
{
int A = shuruzhi - jiaozhunzhi;
float output = Convert.ToSingle(A)/xishu; ;
return output;
}
///
/// 修改配置文件
///
///
///
///
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;
}
///
/// ushort转为float类型
///
///
///
/// float型数据
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;
}
///
/// ushort转为int类型
///
///
///
/// 返回int型数据
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;
}
///
/// Float转为Ushort数组发送
///
///
/// 返回ushort数组
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;
}
///
/// Int转为ushort数组发送
///
///
/// 返回ushort数组
public ushort[] intToushorts(int res)
{
ushort ust1 = (ushort)(res >> 16);
ushort ust2 = (ushort)res;
return new ushort[] { ust2, ust1 };
}
public void ExportDataTableToExcel(List list1, List list2, List list3, List list4, List 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);
}
}
}
}