Commit 1458b22a LN

增加错误码解析

1 个父辈 c5a89bd1
...@@ -45,6 +45,7 @@ namespace HuichuanLibrary ...@@ -45,6 +45,7 @@ namespace HuichuanLibrary
SystemConfigPath = systemConfigPath; SystemConfigPath = systemConfigPath;
} }
HCLogUtil.LOGGER = LogManager.GetLogger(logName); HCLogUtil.LOGGER = LogManager.GetLogger(logName);
HCErrorCode.LoadConfig();
} }
private static string BoardName() private static string BoardName()
...@@ -102,7 +103,7 @@ namespace HuichuanLibrary ...@@ -102,7 +103,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetCardsNum(ref nCardNum); UInt32 ret = ImcApi.IMC_GetCardsNum(ref nCardNum);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" GetCardsNum FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" GetCardsNum FAIL, ",ret);
return false; return false;
} }
else else
...@@ -122,7 +123,7 @@ namespace HuichuanLibrary ...@@ -122,7 +123,7 @@ namespace HuichuanLibrary
ret = ImcApi.IMC_OpenCardHandle(nCardNo, ref nCardHandle); ret = ImcApi.IMC_OpenCardHandle(nCardNo, ref nCardHandle);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" OpenCardHandle FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" OpenCardHandle FAIL, ",ret);
return false; return false;
} }
else else
...@@ -134,7 +135,7 @@ namespace HuichuanLibrary ...@@ -134,7 +135,7 @@ namespace HuichuanLibrary
ret = ImcApi.IMC_DownLoadDeviceConfig(nCardHandle, DeviceConfigPath); ret = ImcApi.IMC_DownLoadDeviceConfig(nCardHandle, DeviceConfigPath);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" DownLoadDeviceConfig FAIL, ErrorCode=0x" + ret.ToString("x8") + ",请检查是否有" + DeviceConfigPath + "文件"); ShowErrorLog(" DownLoadDeviceConfig FAIL, " + ",请检查是否有" + DeviceConfigPath + "文件", ret );
return false; return false;
} }
else else
...@@ -150,7 +151,7 @@ namespace HuichuanLibrary ...@@ -150,7 +151,7 @@ namespace HuichuanLibrary
ret = ImcApi.IMC_GetECATMasterSts(nCardHandle, ref masterStatus); ret = ImcApi.IMC_GetECATMasterSts(nCardHandle, ref masterStatus);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" GetECATMasterSts FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" GetECATMasterSts FAIL, ",ret);
return false; return false;
} }
else else
...@@ -168,7 +169,7 @@ namespace HuichuanLibrary ...@@ -168,7 +169,7 @@ namespace HuichuanLibrary
ret = ImcApi.IMC_ScanCardECAT(nCardHandle, blockFlag); ret = ImcApi.IMC_ScanCardECAT(nCardHandle, blockFlag);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" ScanCardECAT FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" ScanCardECAT FAIL, ",ret);
return false; return false;
} }
} }
...@@ -207,7 +208,7 @@ namespace HuichuanLibrary ...@@ -207,7 +208,7 @@ namespace HuichuanLibrary
uint ret = ImcApi.IMC_DownLoadSystemConfig(nCardHandle, SystemConfigPath); uint ret = ImcApi.IMC_DownLoadSystemConfig(nCardHandle, SystemConfigPath);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" DownLoadSystemConfig FAIL, ErrorCode=0x" + ret.ToString("x8") + ",请检查是否有" + SystemConfigPath + "文件"); ShowErrorLog(" DownLoadSystemConfig FAIL, " + ",请检查是否有" + SystemConfigPath + "文件,", ret );
return false; return false;
} }
else else
...@@ -225,7 +226,7 @@ namespace HuichuanLibrary ...@@ -225,7 +226,7 @@ namespace HuichuanLibrary
uint ret = ImcApi.IMC_GetCardResource(nCardHandle, ref tResource); uint ret = ImcApi.IMC_GetCardResource(nCardHandle, ref tResource);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" GetCardResource FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" GetCardResource FAIL, ",ret);
return false; return false;
} }
else else
...@@ -255,7 +256,7 @@ namespace HuichuanLibrary ...@@ -255,7 +256,7 @@ namespace HuichuanLibrary
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" GetECATMasterSts FAIL, ErrorCode=0x" + ret.ToString("x8") + " "); ShowErrorLog(" GetECATMasterSts FAIL, ",ret );
} }
else if (pStatus.Equals(6)) else if (pStatus.Equals(6))
{ {
...@@ -301,7 +302,7 @@ namespace HuichuanLibrary ...@@ -301,7 +302,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_CloseCard(nCardHandle); UInt32 ret = ImcApi.IMC_CloseCard(nCardHandle);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog("CloseCard FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog("CloseCard FAIL, ",ret);
return false; return false;
} }
else else
...@@ -313,7 +314,7 @@ namespace HuichuanLibrary ...@@ -313,7 +314,7 @@ namespace HuichuanLibrary
ret = ImcApi.IMC_CloseCardHandle(nCardHandle); ret = ImcApi.IMC_CloseCardHandle(nCardHandle);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog("CloseCardHandle FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog("CloseCardHandle FAIL, ",ret);
return false; return false;
} }
else else
...@@ -358,7 +359,7 @@ namespace HuichuanLibrary ...@@ -358,7 +359,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_SetEmgTrigLevelInv(nCardHandle, 0); UInt32 ret = ImcApi.IMC_SetEmgTrigLevelInv(nCardHandle, 0);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" EmgStop FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" EmgStop FAIL, ",ret);
return false; return false;
} }
else else
...@@ -381,7 +382,7 @@ namespace HuichuanLibrary ...@@ -381,7 +382,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_SetEmgTrigLevelInv(nCardHandle, 1); UInt32 ret = ImcApi.IMC_SetEmgTrigLevelInv(nCardHandle, 1);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" CancelEmg FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" CancelEmg FAIL, ",ret);
return false; return false;
} }
else else
...@@ -411,7 +412,7 @@ namespace HuichuanLibrary ...@@ -411,7 +412,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_SetEcatDoBit(nCardHandle, bitNo, bitValue); UInt32 ret = ImcApi.IMC_SetEcatDoBit(nCardHandle, bitNo, bitValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" bitNo["+bitNo+"]bitValue["+bitValue+ "] IMC_SetEcatDoBit FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" bitNo["+bitNo+"]bitValue["+bitValue+ "] IMC_SetEcatDoBit FAIL, ",ret);
return false; return false;
} }
return true; return true;
...@@ -431,7 +432,7 @@ namespace HuichuanLibrary ...@@ -431,7 +432,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatDoBit(nCardHandle, bitNo, ref bitValue); UInt32 ret = ImcApi.IMC_GetEcatDoBit(nCardHandle, bitNo, ref bitValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" bitNo[" + bitNo + "] IMC_GetEcatDoBit FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" bitNo[" + bitNo + "] IMC_GetEcatDoBit FAIL, ",ret);
return 0; return 0;
} }
...@@ -453,7 +454,7 @@ namespace HuichuanLibrary ...@@ -453,7 +454,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatDiBit(nCardHandle, bitNo, ref bitValue); UInt32 ret = ImcApi.IMC_GetEcatDiBit(nCardHandle, bitNo, ref bitValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" bitNo[" + bitNo + "] IMC_GetEcatDiBit, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" bitNo[" + bitNo + "] IMC_GetEcatDiBit, ",ret);
return 0; return 0;
} }
return bitValue; return bitValue;
...@@ -468,7 +469,7 @@ namespace HuichuanLibrary ...@@ -468,7 +469,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_SetEcatGrpDo(nCardHandle, groupNo, groupValue); UInt32 ret = ImcApi.IMC_SetEcatGrpDo(nCardHandle, groupNo, groupValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" groupNo[" + groupNo + "] IMC_SetEcatGrpDo FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" groupNo[" + groupNo + "] IMC_SetEcatGrpDo FAIL, ",ret);
return false; return false;
} }
return true; return true;
...@@ -484,7 +485,7 @@ namespace HuichuanLibrary ...@@ -484,7 +485,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatGrpDo(nCardHandle, groupNo, ref groupValue); UInt32 ret = ImcApi.IMC_GetEcatGrpDo(nCardHandle, groupNo, ref groupValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" groupNo[" + groupNo + "] IMC_GetEcatGrpDo FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" groupNo[" + groupNo + "] IMC_GetEcatGrpDo FAIL, ",ret);
return 0; return 0;
} }
return groupValue; return groupValue;
...@@ -500,7 +501,7 @@ namespace HuichuanLibrary ...@@ -500,7 +501,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatGrpDi(nCardHandle, groupNo, ref groupValue); UInt32 ret = ImcApi.IMC_GetEcatGrpDi(nCardHandle, groupNo, ref groupValue);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" groupNo[" + groupNo + "] IMC_GetEcatGrpDi FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" groupNo[" + groupNo + "] IMC_GetEcatGrpDi FAIL, ",ret);
return 0; return 0;
} }
return groupValue; return groupValue;
...@@ -561,7 +562,7 @@ namespace HuichuanLibrary ...@@ -561,7 +562,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatAdVal(nCardHandle, adNo, ref adVal); UInt32 ret = ImcApi.IMC_GetEcatAdVal(nCardHandle, adNo, ref adVal);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" adNo[" + adNo + "] IMC_GetEcatAdVal FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" adNo[" + adNo + "] IMC_GetEcatAdVal FAIL, ",ret);
return 0; return 0;
} }
return adVal; return adVal;
...@@ -581,7 +582,7 @@ namespace HuichuanLibrary ...@@ -581,7 +582,7 @@ namespace HuichuanLibrary
UInt32 ret = ImcApi.IMC_GetEcatAdSts(nCardHandle, adNo, ref adSts); UInt32 ret = ImcApi.IMC_GetEcatAdSts(nCardHandle, adNo, ref adSts);
if (ret != 0) if (ret != 0)
{ {
ShowErrorLog(" adNo[" + adNo + "] IMC_GetEcatAdSts FAIL, ErrorCode=0x" + ret.ToString("x8")); ShowErrorLog(" adNo[" + adNo + "] IMC_GetEcatAdSts FAIL, ",ret);
return 0; return 0;
} }
return adSts; return adSts;
...@@ -603,11 +604,18 @@ namespace HuichuanLibrary ...@@ -603,11 +604,18 @@ namespace HuichuanLibrary
} }
#endregion #endregion
private static void ShowErrorLog(string msg) private static void ShowErrorLog(string msg,UInt32 ret=0)
{ {
CardMsg = BoardName() + msg; if (ret > 0)
Console.WriteLine(BoardName() + msg); {
HCLogUtil.error(BoardName() + msg); CardMsg = BoardName() +msg+ HCErrorCode.GetCodeMsg(ret);
}
else
{
CardMsg = BoardName() + msg;
}
Console.WriteLine(CardMsg);
HCLogUtil.error(CardMsg);
} }
private static void ShowLog(string msg) private static void ShowLog(string msg)
......
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HuichuanLibrary
{
public class HCErrorCode
{
private static Dictionary<string, string> CmdMap = new Dictionary<string, string>();
private static Dictionary<string, string> ErrorCodeMap = new Dictionary<string, string>();
public static void LoadConfig(string cmdConfigPath = "", string errCodeConfigPath = "" )
{
if (cmdConfigPath.Equals(""))
{
cmdConfigPath = Application.StartupPath+ @"\huichuan_config\commandDef.txt";
}
if (errCodeConfigPath.Equals(""))
{
errCodeConfigPath = Application.StartupPath + @"\huichuan_config\errorcode.h";
}
LoadCmdConfig(cmdConfigPath);
LoadErrConfig(errCodeConfigPath);
}
public static string GetCodeMsg(UInt32 ret)
{
string str = ret.ToString("x8").PadLeft(8,'0');
string cmdCode = "0x" + str.Substring(0, 4);
string errCode = "0x" + str.Substring(4, 4);
CmdMap.TryGetValue(cmdCode, out string cmdStr);
ErrorCodeMap.TryGetValue(errCode, out string errStr);
string result = " ErrorCode = 0x" + str + ",CMD=" + cmdStr + ",ErrorMsg=" + errStr;
return result;
}
private static void LoadCmdConfig (string cmdPath)
{
try
{
CmdMap = new Dictionary<string, string>();
if (!File.Exists(cmdPath))
{
return;
}
Encoding encoding = Encoding.UTF8;
if (!GetType(cmdPath).Equals(Encoding.UTF8))
{
encoding = Encoding.GetEncoding("gb2312");
}
string[] lines = File.ReadAllLines(cmdPath, encoding);
foreach (string line in lines)
{
if (line.Trim().StartsWith("#define"))
{
string newS = line.Trim().Replace("#define", "").Replace(")","");
string[] array = newS.Trim().Split('(');
if (array.Length >= 2)
{
//#define ID_IMC_GetCardsNum (0x8000)
string cmdName = array[0].Trim();
string cmdCode = array[1].Trim().ToLower();
if(!String.IsNullOrEmpty(cmdName) && (!String.IsNullOrEmpty(cmdCode))){
if (!CmdMap.ContainsKey(cmdCode))
{
CmdMap.Add(cmdCode, cmdName);
}
}
}
}
}
}
catch (Exception ex)
{
HCLogUtil.error("加载:" + cmdPath + "错误:" + ex.ToString());
}
}
private static void LoadErrConfig(string errPath)
{
try
{
ErrorCodeMap = new Dictionary<string, string>();
if (!File.Exists(errPath))
{
return;
}
Encoding encoding = Encoding.UTF8;
if (!GetType(errPath).Equals(Encoding.UTF8))
{
encoding = Encoding.GetEncoding("gb2312");
}
string[] lines = File.ReadAllLines(errPath, encoding);
foreach (string line in lines)
{
if (line.Trim().StartsWith("#define"))
{
string newS = line.Trim().Replace("#define", "").Replace(")", "").Replace("//","/");
string[] array = newS.Trim().Split('(');
if (array.Length >= 2)
{
string[] array2 = array[1].Split('/');
if (array2.Length >= 2)
{
//#define ERR_BGVEL_OUTRANG (0x0010) // 起始速度设置超出范围
string cmdName = array2[0].Trim();
string cmdCode = array2[1].Trim().ToLower();
if (!String.IsNullOrEmpty(cmdName) && (!String.IsNullOrEmpty(cmdCode))){
if (!ErrorCodeMap.ContainsKey(cmdName))
{
ErrorCodeMap.Add(cmdName, cmdCode);
}
}
}
}
}
}
}
catch (Exception ex)
{
HCLogUtil.error("加载:" + errPath + "错误:" + ex.ToString());
}
}
#region 判断编码格式
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;
}
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;
}
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;
}
#endregion
}
}
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
</Compile> </Compile>
<Compile Include="HCBoardManager.cs" /> <Compile Include="HCBoardManager.cs" />
<Compile Include="HCBoardManager_Axis.cs" /> <Compile Include="HCBoardManager_Axis.cs" />
<Compile Include="HCErrorCode.cs" />
<Compile Include="ImcApi.cs" /> <Compile Include="ImcApi.cs" />
<Compile Include="HCLogUtil.cs" /> <Compile Include="HCLogUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
...@@ -66,6 +67,12 @@ ...@@ -66,6 +67,12 @@
<Content Include="device_config.xml"> <Content Include="device_config.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<EmbeddedResource Include="huichuan_config\commandDef.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="huichuan_config\errorcode.h">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
<Content Include="huichuan_x64\IMC_API_x64.dll"> <Content Include="huichuan_x64\IMC_API_x64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!