Commit eced8df3 LN

同时出多工单修改。托盘信息保存到文件

1 个父辈 0de6a9c9
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
<add key="Brush_Delay_Seconds" value="5" /> <add key="Brush_Delay_Seconds" value="5" />
<add key ="SaveErrorImageToFile" value ="1"/> <add key ="SaveErrorImageToFile" value ="1"/>
<add key ="UseTrayCheck" value ="8,"/> <add key ="UseTrayCheck" value ="8,"/>
<add key ="ConfigPath_TrayList" value ="\LineConfig\TrayList.data"/>
</appSettings> </appSettings>
<!-- <log4net> --> <!-- <log4net> -->
<!-- <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> --> <!-- <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> -->
......
...@@ -110,6 +110,7 @@ namespace OnlineStore.AssemblyLine ...@@ -110,6 +110,7 @@ namespace OnlineStore.AssemblyLine
Application.Exit(); Application.Exit();
} }
TrayManager.InitFileData();
formLineStatus(false); formLineStatus(false);
string title = ConfigAppSettings.GetValue(Setting_Init.App_Title); string title = ConfigAppSettings.GetValue(Setting_Init.App_Title);
this.Text = title; this.Text = title;
...@@ -142,6 +143,14 @@ namespace OnlineStore.AssemblyLine ...@@ -142,6 +143,14 @@ namespace OnlineStore.AssemblyLine
{ {
启用蜂鸣器ToolStripMenuItem.Text = "启用蜂鸣器"; 启用蜂鸣器ToolStripMenuItem.Text = "启用蜂鸣器";
} }
if (TrayManager.DisTraySave)
{
自动保存托盘信息ToolStripMenuItem.Text = "自动保存托盘信息";
}
else
{
自动保存托盘信息ToolStripMenuItem.Text = gouStr + "自动保存托盘信息";
}
cmbTrayType.SelectedIndex = 0; cmbTrayType.SelectedIndex = 0;
cmbTray.SelectedIndex = 0; cmbTray.SelectedIndex = 0;
LogUtil.logBox = this.logBox; LogUtil.logBox = this.logBox;
...@@ -1024,14 +1033,15 @@ namespace OnlineStore.AssemblyLine ...@@ -1024,14 +1033,15 @@ namespace OnlineStore.AssemblyLine
DialogResult resut = MessageBox.Show("确定手动清空所有托盘信息?", "提示", MessageBoxButtons.YesNo); DialogResult resut = MessageBox.Show("确定手动清空所有托盘信息?", "提示", MessageBoxButtons.YesNo);
if (resut.Equals(DialogResult.Yes)) if (resut.Equals(DialogResult.Yes))
{ {
List<TrayInfo> tray = TrayManager.getTrayList(); //List<TrayInfo> tray = TrayManager.getTrayList();
LogUtil.info("-------------------点击:清空托盘。清空前打印托盘信息:"); //LogUtil.info("-------------------点击:清空托盘。清空前打印托盘信息:");
foreach (TrayInfo t in tray) //foreach (TrayInfo t in tray)
{ //{
LogUtil.info(t.ToStr()); // LogUtil.info(t.ToStr());
} //}
//TrayManager.ClearTrayInfo();
//LogUtil.info("-------------------点击:清空托盘。托盘内容已清空");
TrayManager.ClearTrayInfo(); TrayManager.ClearTrayInfo();
LogUtil.info("-------------------点击:清空托盘。托盘内容已清空");
} }
} }
...@@ -1303,5 +1313,30 @@ namespace OnlineStore.AssemblyLine ...@@ -1303,5 +1313,30 @@ namespace OnlineStore.AssemblyLine
} }
LogUtil.info(Name + " 点击:" + 启用分流横移2ToolStripMenuItem.Text+ ",lineBean.DisableShunt2="+ lineBean.DisableShunt2); LogUtil.info(Name + " 点击:" + 启用分流横移2ToolStripMenuItem.Text+ ",lineBean.DisableShunt2="+ lineBean.DisableShunt2);
} }
private void 自动保存托盘信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!LoadOk)
{
return;
}
bool result = 自动保存托盘信息ToolStripMenuItem.Text.Contains(gouStr);
if (result.Equals(TrayManager.DisTraySave))
{
return;
}
TrayManager.DisTraySave = result;
ConfigAppSettings.SaveValue(Setting_Init.DisTraySave, (TrayManager.DisTraySave ? 1 : 0));
if (TrayManager.DisTraySave)
{
自动保存托盘信息ToolStripMenuItem.Text = "自动保存托盘信息";
}
else
{
自动保存托盘信息ToolStripMenuItem.Text = gouStr + " 自动保存托盘信息";
}
LogUtil.info(Name + " 点击:" + 自动保存托盘信息ToolStripMenuItem.Text);
}
} }
} }
2020120325 20210609
出库改为可以出多个工单。
流水线料盘移动到皮带线时进行判断
地址:
/rest/api/qisda/device/canReelToBelt
参数:
barcode: 料盘条码
返回:
{"code":0,"msg":"ok","data":true}
code: 0为正常,其他为异常,(未传参数, 未找到有效条码,多个有效条码)
msg:消息,
data: true 可以放上皮带线 false 继续留在环形线
20210603
扫码新算法更新。
20210325
等待托盘超时报警时,蜂鸣器响。 等待托盘超时报警时,蜂鸣器响。
...@@ -8,12 +26,12 @@ ...@@ -8,12 +26,12 @@
2.入料机构点击暂停时只暂停入料,托盘继续处理。 2.入料机构点击暂停时只暂停入料,托盘继续处理。
3.入料机构提前获取库位号,提前拦截托盘。 3.入料机构提前获取库位号,提前拦截托盘。
20200305 20210305
进仓升降轴增加P3详细位置配置。需要修改配置文件。 进仓升降轴增加P3详细位置配置。需要修改配置文件。
扫码dll替换。 扫码dll替换。
20200226 20210226
环形线更新文件 20210226-AssemblyLine-更新,能重启时帮忙更新下。 环形线更新文件 20210226-AssemblyLine-更新,能重启时帮忙更新下。
修改内容: 修改内容:
1.有料托盘需要入库时,若检测信号不亮,直接禁用托盘。 1.有料托盘需要入库时,若检测信号不亮,直接禁用托盘。
...@@ -24,7 +42,7 @@ ...@@ -24,7 +42,7 @@
5.料仓出库和料串入库 放料到托盘 时,如果是大于7寸的料盘,夹爪放松后夹紧一次,再放松。 5.料仓出库和料串入库 放料到托盘 时,如果是大于7寸的料盘,夹爪放松后夹紧一次,再放松。
20200225 20210225
1.托盘增加禁用功能,禁用的托盘可在(设备调试->查看禁用托盘)界面手动启用。 1.托盘增加禁用功能,禁用的托盘可在(设备调试->查看禁用托盘)界面手动启用。
2.进仓界面增加启用托盘检测的勾选配置。 2.进仓界面增加启用托盘检测的勾选配置。
3.进仓启用托盘检测功能后,若拦截到入库托盘,需要等待托盘检测信号亮。拦截空托盘出库时,如果托盘检测信号亮,直接禁用托盘。 3.进仓启用托盘检测功能后,若拦截到入库托盘,需要等待托盘检测信号亮。拦截空托盘出库时,如果托盘检测信号亮,直接禁用托盘。
......
...@@ -99,5 +99,9 @@ namespace OnlineStore.Common ...@@ -99,5 +99,9 @@ namespace OnlineStore.Common
public static string SaveErrorImageToFile = "SaveErrorImageToFile"; public static string SaveErrorImageToFile = "SaveErrorImageToFile";
public static string UseTrayCheck = "UseTrayCheck"; public static string UseTrayCheck = "UseTrayCheck";
public static string ConfigPath_TrayList = "ConfigPath_TrayList";
public static string DisTraySave = "DisTraySave";
} }
} }
...@@ -165,6 +165,13 @@ namespace OnlineStore.DeviceLibrary ...@@ -165,6 +165,13 @@ namespace OnlineStore.DeviceLibrary
} }
else if (trayInfo.InOrOutStore.Equals(ReelType.OutStore) && inoup.urgentReel.Equals(false) && inoup.cutReel.Equals(false)) else if (trayInfo.InOrOutStore.Equals(ReelType.OutStore) && inoup.urgentReel.Equals(false) && inoup.cutReel.Equals(false))
{ {
bool needToBielt = SServerManager.canReelToBelt(Name, inoup.WareCode);
if (!needToBielt)
{
LogUtil.info(Name + $"工单料:{inoup.WareCode} canReelToBelt={needToBielt} ,暂不移到皮带线");
return null;
}
List<int> mustLine = new List<int>(); List<int> mustLine = new List<int>();
List<int> firstLine = new List<int>(); List<int> firstLine = new List<int>();
//urgentReel: true 表示紧急料,需要出到料串上 //urgentReel: true 表示紧急料,需要出到料串上
...@@ -329,6 +336,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -329,6 +336,7 @@ namespace OnlineStore.DeviceLibrary
} }
//出料中,需要拦盘 //出料中,需要拦盘
string outMsg = ""; string outMsg = "";
SecondMoveInfo.WaitList.Add(WaitResultInfo.WaitTime(5000));
InOutParam param = CheckIsNeedOutStore(out outMsg); InOutParam param = CheckIsNeedOutStore(out outMsg);
if (param != null) if (param != null)
{ {
......
...@@ -88,7 +88,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -88,7 +88,7 @@ namespace OnlineStore.DeviceLibrary
if (serverResult == null) if (serverResult == null)
{ {
msg = deviceName + " 【" + codeStr + "】结果:没有收到服务器反馈,调用 cancelPutInTask "; msg = deviceName + " 【" + codeStr + "】结果:没有收到服务器反馈,调用 cancelPutInTask ";
cancelPutInTask(deviceName, codeStr,false); cancelPutInTask(deviceName, codeStr, false);
return msg; return msg;
} }
else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false)) else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false))
...@@ -303,7 +303,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -303,7 +303,7 @@ namespace OnlineStore.DeviceLibrary
{ {
if (robotIndex <= 0) if (robotIndex <= 0)
{ {
return "robotIndex="+robotIndex; return "robotIndex=" + robotIndex;
} }
DateTime startTime = DateTime.Now; DateTime startTime = DateTime.Now;
Dictionary<string, string> paramMap = new Dictionary<string, string>(); Dictionary<string, string> paramMap = new Dictionary<string, string>();
...@@ -450,7 +450,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -450,7 +450,7 @@ namespace OnlineStore.DeviceLibrary
if (data == null) if (data == null)
{ {
return msg = deviceName + " cancelPutInTask【 " + barcode + ","+ disablePos + "】 没有收到服务器反馈"; return msg = deviceName + " cancelPutInTask【 " + barcode + "," + disablePos + "】 没有收到服务器反馈";
} }
else if (data.code.Equals(0).Equals(false)) else if (data.code.Equals(0).Equals(false))
{ {
...@@ -509,7 +509,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -509,7 +509,7 @@ namespace OnlineStore.DeviceLibrary
if (serverResult == null) if (serverResult == null)
{ {
result.Msg = deviceName + " 【" + codeStr + "】结果:没有收到服务器反馈,调用 cancelPutInTask "; result.Msg = deviceName + " 【" + codeStr + "】结果:没有收到服务器反馈,调用 cancelPutInTask ";
cancelPutInTask(deviceName, codeStr,false ); cancelPutInTask(deviceName, codeStr, false);
result.Param = new InOutParam(0, codeStr, "", height, width, true); result.Param = new InOutParam(0, codeStr, "", height, width, true);
result.Param.rfid = rfid; result.Param.rfid = rfid;
result.Param.InStoreNg = true; result.Param.InStoreNg = true;
...@@ -519,7 +519,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -519,7 +519,7 @@ namespace OnlineStore.DeviceLibrary
else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false)) else if ((!string.IsNullOrEmpty(serverResult.msg)) || serverResult.result.Equals(0).Equals(false))
{ {
result.Result = serverResult.result; result.Result = serverResult.result;
result.Msg = serverResult.msg; result.Msg = serverResult.msg;
//result.Msg = deviceName + " 【" + codeStr + "】结果:" + serverResult.msg; //result.Msg = deviceName + " 【" + codeStr + "】结果:" + serverResult.msg;
result.Param = new InOutParam(0, codeStr, "", height, width, true); result.Param = new InOutParam(0, codeStr, "", height, width, true);
result.Param.rfid = rfid; result.Param.rfid = rfid;
...@@ -546,7 +546,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -546,7 +546,7 @@ namespace OnlineStore.DeviceLibrary
result.Param.rfid = rfid; result.Param.rfid = rfid;
int storeId = result.Param.GetStoreId(); int storeId = result.Param.GetStoreId();
MoveEquip moveEquip = LineManager.Line.GetMoveByDId(storeId); MoveEquip moveEquip = LineManager.Line.GetMoveByDId(storeId);
if (moveEquip!=null) if (moveEquip != null)
{ {
if (LineManager.Line.IsReviceInPosId(moveEquip, posId)) if (LineManager.Line.IsReviceInPosId(moveEquip, posId))
{ {
...@@ -632,7 +632,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -632,7 +632,7 @@ namespace OnlineStore.DeviceLibrary
paramMap.Add("deviceAlarmList", msgListStr); paramMap.Add("deviceAlarmList", msgListStr);
string server = GetAddr(Addr_updateDeviceAlarmMsg, paramMap); string server = GetAddr(Addr_updateDeviceAlarmMsg, paramMap);
DateTime startTime = DateTime.Now; DateTime startTime = DateTime.Now;
string resultStr = HttpHelper.Post(server, "",5000); string resultStr = HttpHelper.Post(server, "", 5000);
LogUtil.debug("updateDeviceAlarmMsg " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】"); LogUtil.debug("updateDeviceAlarmMsg " + FormUtil.GetSpanStr(DateTime.Now - startTime) + " 【" + server + "】【" + resultStr + "】");
RfidData data = JsonHelper.DeserializeJsonToObject<RfidData>(resultStr); RfidData data = JsonHelper.DeserializeJsonToObject<RfidData>(resultStr);
...@@ -653,6 +653,57 @@ namespace OnlineStore.DeviceLibrary ...@@ -653,6 +653,57 @@ namespace OnlineStore.DeviceLibrary
} }
return msg; return msg;
} }
// 流水线料盘移动到皮带线时进行判断: 地址: /rest/api/qisda/device/canReelToBelt 参数:barcode: 料盘条码
private static string Addr_canReelToBelt = "/rest/api/qisda/device/canReelToBelt";
public static bool canReelToBelt(string deviceName, string barcode)
{
// 返回: // { "code":0,"msg":"ok","data":true}
// code: 0为正常,其他为异常,(未传参数, 未找到有效条码, 多个有效条码)
// msg:消息,
//data: true 可以放上皮带线 false 继续留在环形线。默认直接放到皮带线
try
{
Dictionary<string, string> paramMap = new Dictionary<string, string>();
paramMap.Add("barcode", barcode);
string server = GetAddr(Addr_canReelToBelt, paramMap);
DateTime startTime = DateTime.Now;
string resultStr = HttpHelper.Post(server, "");
LogUtil.debug($"{deviceName }canReelToBelt {FormUtil.GetSpanStr(DateTime.Now - startTime) } 【{ server }】【{resultStr }】");
ReturnData data = JsonHelper.DeserializeJsonToObject<ReturnData>(resultStr);
if (data != null)
{
bool result = Convert.ToBoolean(data.data);
if (data.code.Equals(0) && (!result))
{
return result;
} else if (!data.code.Equals(0))
{
LogUtil.error($"{deviceName }canReelToBelt【 { barcode }】 返回错误 :{ data.msg}={data.msg}");
}
}
}
catch (Exception ex)
{
LogUtil.error(deviceName + " " + ex.ToString());
}
return true;
}
}
public class ReturnData
{
// { "code":0,"msg":"ok","data":true}
public int code { get; set; }
public string msg { get; set; }
public object data { get; set; }
} }
public class AlarmMsg public class AlarmMsg
{ {
......
...@@ -5,15 +5,18 @@ using System; ...@@ -5,15 +5,18 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary namespace OnlineStore.DeviceLibrary
{ {
public class TrayManager public class TrayManager
{ {
public static bool DisTraySave = ConfigAppSettings.GetIntValue(Setting_Init.DisTraySave).Equals(1);
/// <summary> /// <summary>
/// 进仓5RFID缓存 /// 进仓5RFID缓存
/// </summary> /// </summary>
...@@ -110,6 +113,7 @@ namespace OnlineStore.DeviceLibrary ...@@ -110,6 +113,7 @@ namespace OnlineStore.DeviceLibrary
TrayInfo fixture = new TrayInfo(trayNum, isFull, inOrOut, param,ngMsg); TrayInfo fixture = new TrayInfo(trayNum, isFull, inOrOut, param,ngMsg);
TrayInfoMap.TryAdd(trayNum, fixture); TrayInfoMap.TryAdd(trayNum, fixture);
} }
SaveMapToFile();
} }
public static void UpdateInStoreNG(int trayNum, bool isNg, string msg) public static void UpdateInStoreNG(int trayNum, bool isNg, string msg)
...@@ -120,6 +124,8 @@ namespace OnlineStore.DeviceLibrary ...@@ -120,6 +124,8 @@ namespace OnlineStore.DeviceLibrary
TrayInfoMap[trayNum].InoutPar.InStoreNg = isNg; TrayInfoMap[trayNum].InoutPar.InStoreNg = isNg;
TrayInfoMap[trayNum].ShowMsg = msg; TrayInfoMap[trayNum].ShowMsg = msg;
TrayInfoMap[trayNum].LastUpdateTime = DateTime.Now; TrayInfoMap[trayNum].LastUpdateTime = DateTime.Now;
SaveMapToFile();
} }
} }
...@@ -188,7 +194,15 @@ namespace OnlineStore.DeviceLibrary ...@@ -188,7 +194,15 @@ namespace OnlineStore.DeviceLibrary
public static void ClearTrayInfo() public static void ClearTrayInfo()
{ {
List<TrayInfo> tray = TrayManager.getTrayList();
LogUtil.info("-------------------点击:清空托盘。清空前打印托盘信息:");
foreach (TrayInfo t in tray)
{
LogUtil.info(t.ToStr());
}
TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>(); TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>();
SaveMapToFile();
LogUtil.info("-------------------点击:清空托盘。托盘内容已清空");
} }
/// <summary> /// <summary>
/// 只清空托盘的 入库任务,会取消入库任务 /// 只清空托盘的 入库任务,会取消入库任务
...@@ -268,6 +282,69 @@ namespace OnlineStore.DeviceLibrary ...@@ -268,6 +282,69 @@ namespace OnlineStore.DeviceLibrary
} }
return false; return false;
} }
private static string FilePath = "";
public static void InitFileData()
{
TrayInfoMap = new ConcurrentDictionary<int, TrayInfo>();
if (DisTraySave)
{
LogUtil.error("禁用了托盘保存功能,不需要从文件夹加载托盘");
return;
}
FilePath = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.ConfigPath_TrayList);
if (File.Exists(FilePath))
{
LogUtil.info("开始加载文件缓存托盘:" + FilePath);
string[] lines = FileEncoding.GetFileLines(FilePath);
foreach (string line in lines)
{
TrayInfo tray = JsonHelper.DeserializeJsonToObject<TrayInfo>(line);
if (tray != null && tray.TrayCode > 0 && tray.IsFull)
{
LogUtil.info("加载到缓存托盘:" + tray.ToStr());
TrayInfoMap.TryAdd(tray.TrayCode, tray);
if (tray.InOrOutStore.Equals(1) && tray.InoutPar.InStoreNg.Equals(false) && (!tray.InoutPar.PosId.Equals("")))
{
int storeId = tray.InoutPar.GetStoreId();
MoveEquip moveEquip = LineManager.Line.GetMoveByDId(storeId);
if (storeId > 0 && moveEquip != null)
{
lock (moveEquip.waitInListLock)
{
//如果当前正在出入库中,需要记录下来,等待空闲时执行
LogUtil.info(moveEquip.Name + " 将托盘入库任务[" + tray.TrayCode + "]: " + tray.InoutPar.ToStr() + "加入等待列表中!");
moveEquip.waitInStoreList.Add(tray.InoutPar);
}
}
}
}
}
LogUtil.info("托盘加载完成");
}
}
public static void SaveMapToFile()
{
try
{
List<TrayInfo> trayList = new List<TrayInfo>(TrayInfoMap.Values);
List<string> lineList = new List<string>();
foreach (TrayInfo tray in trayList)
{
string line = JsonHelper.SerializeObject(tray);
if (!string.IsNullOrEmpty(line))
{
lineList.Add(line);
}
}
FileEncoding.WritteFile(FilePath, lineList.ToArray());
}
catch (Exception ex)
{
LogUtil.error("SaveTrayToFile出错:" + ex.ToString());
}
}
} }
} }
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace OnlineStore.LoadCSVLibrary
{
public class FileEncoding
{
public static bool WritteFile(string filePath, string[] lines)
{
try
{
//备份保存
try
{
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
string date = DateTime.Now.ToString("yyyy-MM-dd");
string targetBackPath = @"C:\configBack\" + date + @"\";
if (!Directory.Exists(targetBackPath))
{
Directory.CreateDirectory(targetBackPath);
}
string fileName = file.Name;
string backFile = targetBackPath + fileName;
if (File.Exists(backFile))
{
File.Delete(backFile);
}
File.Copy(filePath, backFile, true);
}
}
catch (Exception e)
{
LogUtil.error(" WritteFile备份文件 filePath 出错:", e);
}
if (File.Exists(filePath))
{
File.Delete(filePath);
}
File.WriteAllLines(filePath, lines, Encoding.UTF8);
}
catch (Exception ex)
{
LogUtil.error(ex.ToString());
return false;
}
return true;
}
public static string[] GetFileLines(string filePath)
{
if (!File.Exists(filePath))
{
return new string[] { };
}
Encoding encoding = Encoding.UTF8;
if (!FileEncoding.GetType(filePath).Equals(Encoding.UTF8))
{
encoding = Encoding.GetEncoding("gb2312");
}
string[] lines = File.ReadAllLines(filePath, encoding);
return lines;
}
/// <summary>
/// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型
/// </summary>
/// <param name=“FILE_NAME“>文件路径</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
Encoding r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 通过给定的文件流,判断文件的编码类型
/// </summary>
/// <param name=“fs“>文件流</param>
/// <returns>文件的编码类型</returns>
public static System.Text.Encoding GetType(FileStream fs)
{
byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 };
byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 };
byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM
Encoding reVal = Encoding.Default;
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
int i;
int.TryParse(fs.Length.ToString(), out i);
byte[] ss = r.ReadBytes(i);
if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF))
{
reVal = Encoding.UTF8;
}
else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00)
{
reVal = Encoding.BigEndianUnicode;
}
else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41)
{
reVal = Encoding.Unicode;
}
r.Close();
return reVal;
}
/// <summary>
/// 判断是否是不带 BOM 的 UTF8 格式
/// </summary>
/// <param name=“data“></param>
/// <returns></returns>
private static bool IsUTF8Bytes(byte[] data)
{
int charByteCounter = 1; //计算当前正分析的字符应还有的字节数
byte curByte; //当前分析的字节.
for (int i = 0; i < data.Length; i++)
{
curByte = data[i];
if (charByteCounter == 1)
{
if (curByte >= 0x80)
{
//判断当前
while (((curByte <<= 1) & 0x80) != 0)
{
charByteCounter++;
}
//标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X
if (charByteCounter == 1 || charByteCounter > 6)
{
return false;
}
}
}
else
{
//若是UTF-8 此时第一位必须为1
if ((curByte & 0xC0) != 0x80)
{
return false;
}
charByteCounter--;
}
}
if (charByteCounter > 1)
{
throw new Exception("非预期的byte格式");
}
return true;
}
}
}
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CSVExection.cs" /> <Compile Include="CSVExection.cs" />
<Compile Include="FileEncoding.cs" />
<Compile Include="storeConfig\ConfigItemBase.cs" /> <Compile Include="storeConfig\ConfigItemBase.cs" />
<Compile Include="storeConfig\ConfigProAttribute.cs" /> <Compile Include="storeConfig\ConfigProAttribute.cs" />
<Compile Include="storeConfig\config\Line_Config.cs" /> <Compile Include="storeConfig\config\Line_Config.cs" />
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!