在上位机开发中,数据和log尤为重要,本文与大家交流一下
数据:生产数据 点位数据(一般采用数据库各个公司的不一样本文不讨论) DownTime 良率
CT 等.
生产数据一般来说采用CSV格式,带逗号分隔,可以直接用文本打开,也可以打开为EXCEL格式方便快捷,便于维护.(不支持写入时打开)
Log :通常Txt文本形式保存.简单明了(随时可以打开分析)
通常在项目中一般用一个函数搞定:
#region 信息记录
//加锁
private static object writeObj = new object();
///
/// 信息记录
///
/// 信息类型
/// 1:运行记录
/// 2:报警记录
/// 3:操作记录
/// 4:程序异常
/// 5:相机通信
/// 6:扫码通信
/// 7:MES通信记录
/// 8:时间统计
/// 9:生产数据
///
///
记录信息
public static void WriteRecord(int type, string str)
{
lock (writeObj)
{
try
{
//所有文件按日期进行保存,不然放在一起会很大,打开时会很慢
string sFilePath = null;
string sFileRootPath = "D:\DATA";
List title = null;
StringBuilder filename = new StringBuilder();
string recorddate = string.Format("{0:D4}年{1:D2}月{2:D2}日", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
string recordtime = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D3}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond);
switch (type)
{
case 1:
sFilePath = sFileRootPath + @"运行记录";
filename.Append(sFilePath + recorddate + ".txt");
break;
case 2:
sFilePath = sFileRootPath + @"报警记录";
filename.Append(sFilePath + recorddate + ".csv");
title = new List() { "日期", "时间", "错误代码", "错误编组", "消息", "持续时间","记录时间" };
break;
case 3:
sFilePath = sFileRootPath + @"操作记录";
filename.Append(sFilePath + recorddate + ".txt");
break;
case 4:
sFilePath = sFileRootPath + @"程序异常";
filename.Append(sFilePath + recorddate + ".txt");
break;
case 5:
sFilePath = sFileRootPath + @"相机通信";
filename.Append(sFilePath + recorddate + ".txt");
break;
case 6:
sFilePath = sFileRootPath + @"扫码通信";
filename.Append(sFilePath + recorddate + ".txt");
break;
case 7:
sFilePath = sFileRootPath + @"Mes通信记录";
filename.Append(sFilePath + recorddate + ".txt");
brea
case 8:
sFilePath = sFileRootPath + @"时间统计";
filename.Append(sFilePath + recorddate + ".csv");
title = new List() { "机台状态", "开始时间", "结束时间", "持续时间", "记录时间" };
break;
case 9:
sFilePath = sFileRootPath + @"生产数据";
filename.Append(sFilePath + recorddate + ".csv");
title = new List() { "Date", "Time", "data1","data2", "data3", "data4", "errCode"};
break;
}
if (!System.IO.Directory.Exists(sFilePath))
{
System.IO.Directory.CreateDirectory(sFilePath);
}
if (title != null)
{
if (!File.Exists(filename.ToString()))
{
WriteCSV(filename.ToString(), title);
}
}
using (StreamWriter file = new StreamWriter(filename.ToString(), true, Encoding.Default))
{
string sFile = filename.ToString();
if (sFile.Substring(sFile.Length - 3) == "csv")
file.WriteLine(recordtime + ", " + str);
}
}
catch (Exception m)
{
MessageBox.Show(m.Message);
}
}
}
///
/// 写入CSV文件
///
/// 路径
///
///
工控项目中用的非常广泛,一个函数搞定所有记录,希望与大家多交流一下!!!