HCErrorCode.cs
7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
}
}