GlobalTool.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. using EasyTemplate.Tool.Entity;
  2. using System.Collections.Concurrent;
  3. using System.Globalization;
  4. using System.Net.Sockets;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.IO.Ports;
  8. namespace EasyTemplate.Service
  9. {
  10. public class GlobalTool
  11. {
  12. public const int NozzleState_Offline = 0;
  13. public const int NozzleState_Idle = 1;
  14. public const int NozzleState_Filling = 2;
  15. public const int WarningState_Normal = 0;
  16. public const int WarningState_Prewarning = 1;
  17. public const int WarningState_Warning = 2;
  18. public static int vlr_sys_max = 1200; //3位小数; 气液比上限
  19. public static int vlr_sys_min = 1000; //3位小数; 气液比下限
  20. public static int vlr_sys_max_alert = 1500; //3位小数; 严重气液比上限
  21. public static int vlr_sys_min_alert = 600; //3位小数; 严重气液比下限
  22. public static int vlr_sys_max_gd_zsh = 1200; //3位小数; 气液比上限
  23. public static int vlr_sys_min_gd_zsh = 1000; //3位小数; 气液比下限
  24. public static Dictionary<int, NozzleState> g_mNozzleState = new Dictionary<int, NozzleState>();
  25. public static ConcurrentQueue<BufferData> g_dataQueue = new ConcurrentQueue<BufferData>();
  26. const int DIT_BUFSIZE = 1024;
  27. // 计算单个字节的CRC
  28. public static ushort calccrc(byte crcbuf, ushort crc)
  29. {
  30. byte i;
  31. crc = (ushort)(crc ^ crcbuf);
  32. for (i = 0; i < 8; i++)
  33. {
  34. byte chk;
  35. chk = (byte)(crc & 1);
  36. crc = (ushort)(crc >> 1);
  37. crc = (ushort)(crc & 0x7fff);
  38. if (chk == 1)
  39. crc = (ushort)(crc ^ 0xa001); /* CRC polynom=0xa001 */
  40. crc = (ushort)(crc & 0xffff);
  41. }
  42. return crc;
  43. }
  44. // 带自定义多项式的CRC计算
  45. public static ushort calccrc(byte crcbuf, ushort crc, ushort polynom)
  46. {
  47. byte i;
  48. crc = (ushort)(crc ^ crcbuf);
  49. for (i = 0; i < 8; i++)
  50. {
  51. byte chk;
  52. chk = (byte)(crc & 1);
  53. crc = (ushort)(crc >> 1);
  54. crc = (ushort)(crc & 0x7fff);
  55. if (chk == 1)
  56. crc = (ushort)(crc ^ polynom);
  57. crc = (ushort)(crc & 0xffff);
  58. }
  59. return crc;
  60. }
  61. // 标准CRC校验
  62. public static ushort chkcrc(byte[] buf, ushort len)
  63. {
  64. ushort i;
  65. ushort crc;
  66. crc = 0x0000; /* Initial crc value=0x0000 */
  67. for (i = 0; i < len; i++)
  68. {
  69. crc = calccrc(buf[i], crc);
  70. }
  71. return crc;
  72. }
  73. // 带多项式的CRC校验
  74. public static ushort chkcrc(byte[] buf, ushort len, ushort polynom)
  75. {
  76. ushort i;
  77. ushort crc;
  78. crc = 0x0000;
  79. for (i = 0; i < len; i++)
  80. {
  81. crc = calccrc(buf[i], crc, polynom);
  82. }
  83. return crc;
  84. }
  85. // Modbus CRC校验
  86. public static ushort chkcrc_modbus(byte[] buf, ushort len, ushort polynom)
  87. {
  88. ushort i;
  89. ushort crc;
  90. crc = 0xffff;
  91. for (i = 0; i < len; i++)
  92. {
  93. crc = calccrc(buf[i], crc, polynom);
  94. }
  95. return crc;
  96. }
  97. public static void crc_calc_sinopec(byte[] ptr_buffer, ushort length)
  98. {
  99. ushort crc_result;
  100. /* calculate CRC */
  101. crc_result = chkcrc(ptr_buffer, length);
  102. /* ... and store it */
  103. ptr_buffer[length] = (byte)(crc_result >> 8);
  104. ptr_buffer[length + 1] = (byte)crc_result;
  105. }
  106. public static string BytesToBcd(byte[] Bytes)
  107. {
  108. if (Bytes == null || Bytes.Length == 0)
  109. return string.Empty;
  110. StringBuilder result = new StringBuilder();
  111. foreach (byte b in Bytes)
  112. {
  113. // 提取高4位和低4位
  114. byte highNibble = (byte)((b >> 4) & 0x0F);
  115. byte lowNibble = (byte)(b & 0x0F);
  116. // 转换为字符
  117. result.Append((char)('0' + highNibble));
  118. result.Append((char)('0' + lowNibble));
  119. }
  120. return result.ToString();
  121. }
  122. public static T ByteToStructure<T>(Byte[] dataBuffer)
  123. {
  124. object structure = null;
  125. int size = Marshal.SizeOf(typeof(T));
  126. IntPtr allocIntPtr = Marshal.AllocHGlobal(size);
  127. try
  128. {
  129. Marshal.Copy(dataBuffer, 0, allocIntPtr, size);
  130. structure = Marshal.PtrToStructure(allocIntPtr, typeof(T));
  131. }
  132. catch (Exception e)
  133. {
  134. }
  135. finally
  136. {
  137. Marshal.FreeHGlobal(allocIntPtr);
  138. }
  139. return (T)structure;
  140. }
  141. public static byte[] StructToBytes(object structObj)
  142. {
  143. //得到结构体的大小
  144. int size = Marshal.SizeOf(structObj);
  145. //创建byte数组
  146. byte[] bytes = new byte[size];
  147. //分配结构体大小的内存空间
  148. IntPtr structPtr = Marshal.AllocHGlobal(size);
  149. //将结构体拷到分配好的内存空间
  150. Marshal.StructureToPtr(structObj, structPtr, false);
  151. //从内存空间拷到byte数组
  152. Marshal.Copy(structPtr, bytes, 0, size);
  153. //释放内存空间
  154. Marshal.FreeHGlobal(structPtr);
  155. //返回byte数组
  156. return bytes;
  157. }
  158. /// <summary>
  159. /// 将字节数组按大端序转换为整数
  160. /// </summary>
  161. /// <param name="bytes">字节数组</param>
  162. /// <returns>转换后的整数</returns>
  163. public static int BytesToInt(byte[] bytes)
  164. {
  165. if (bytes == null)
  166. throw new ArgumentNullException(nameof(bytes));
  167. if (bytes.Length == 0)
  168. return 0;
  169. if (bytes.Length > 4)
  170. throw new ArgumentException("字节数组长度不能超过4个字节");
  171. int result = 0;
  172. for (int i = 0; i < bytes.Length; i++)
  173. {
  174. result = (result << 8) | bytes[i];
  175. }
  176. return result;
  177. }
  178. public static bool checkEscapeCharacter(ref byte[] buf, ref int len, byte ch)
  179. {
  180. bool ischeck = false;
  181. byte[] tmpData = new byte[DIT_BUFSIZE];
  182. int j = 0;
  183. for (int i = 0; i < len && j < DIT_BUFSIZE; i++)
  184. {
  185. if (i < len - 1 && buf[i] == ch && buf[i + 1] == ch)
  186. {
  187. tmpData[j] = ch;
  188. i += 1; // 跳过下一个字符
  189. ischeck = true;
  190. j++;
  191. }
  192. else
  193. {
  194. tmpData[j] = buf[i];
  195. j++;
  196. }
  197. }
  198. // 更新长度和缓冲区内容
  199. len = j;
  200. Array.Copy(tmpData, buf, len);
  201. return ischeck;
  202. }
  203. public static bool calcEscapeLength(byte[] buf, ref int len, byte ch)
  204. {
  205. bool ischeck = false;
  206. // 确保数组足够大,如果不够大则调整循环次数
  207. int loopCount = Math.Min(DIT_BUFSIZE, buf.Length);
  208. for (int i = 0; i < loopCount; i++)
  209. {
  210. if (i < loopCount - 1 && buf[i] == ch && buf[i + 1] == ch)
  211. {
  212. i += 1; // 跳过下一个字符
  213. len--; // 长度减1
  214. ischeck = true;
  215. }
  216. }
  217. return ischeck;
  218. }
  219. public static int BCDtoDec(byte[] data, int length)
  220. {
  221. // BCD转十进制实现
  222. int result = 0;
  223. for (int i = 0; i < length; i++)
  224. {
  225. result = result * 100 + ((data[i] >> 4) * 10 + (data[i] & 0x0F));
  226. }
  227. return result;
  228. }
  229. /// <summary>
  230. /// 将BCD码转换回十进制数值
  231. /// </summary>
  232. /// <param name="bcdValue">BCD码值</param>
  233. /// <returns>对应的十进制数值</returns>
  234. public static int ConvertBCDToDecimalSingle(byte bcdValue)
  235. {
  236. // 提取高四位(十位数字)
  237. byte tens = (byte)((bcdValue >> 4) & 0x0F);
  238. // 提取低四位(个位数字)
  239. byte units = (byte)(bcdValue & 0x0F);
  240. // 组合得到十进制数值
  241. return tens * 10 + units;
  242. }
  243. /// <summary>
  244. /// 将多个BCD码转换为可变长度的十进制数值 如0x1234转成1234
  245. /// </summary>
  246. /// <param name="bcdBytes">BCD码字节数组</param>
  247. /// <returns>对应的十进制数值</returns>
  248. public static long ConvertBCDToDecimal(byte[] bcdBytes)
  249. {
  250. long result = 0;
  251. foreach (byte bcd in bcdBytes)
  252. {
  253. byte tens = (byte)((bcd >> 4) & 0x0F);
  254. byte units = (byte)(bcd & 0x0F);
  255. result = result * 100 + tens * 10 + units;
  256. }
  257. return result;
  258. }
  259. public static filling_nozzleup extractFillingNozzleUP(byte[] Buffer, int startIndex)
  260. {
  261. filling_nozzleup data = new filling_nozzleup();
  262. BufferHandler handler = new BufferHandler(Buffer, startIndex);
  263. data.board = handler.getData(1);
  264. data.noz = handler.getData(1);
  265. data.fip = data.board;
  266. data.time = handler.getDataString_BCD(7);
  267. return data;
  268. }
  269. public static filling_nozzledown extractFillingNozzleDown(byte[] Buffer, int startIndex)
  270. {
  271. filling_nozzledown data = new filling_nozzledown();
  272. BufferHandler handler = new BufferHandler(Buffer, startIndex);
  273. data.board = handler.getData(1);
  274. data.noz = handler.getData(1);
  275. data.fip = data.board;
  276. data.time = handler.getDataString_BCD(7);
  277. return data;
  278. }
  279. public static filling_process extractFillingProcess(byte[] Buffer, int startIndex)
  280. {
  281. filling_process data = new filling_process();
  282. BufferHandler handler = new BufferHandler(Buffer, startIndex);
  283. data.board = handler.getData(1);
  284. data.noz = handler.getData(1);
  285. data.fip = data.board;
  286. data.liquidVL = handler.getData(3);
  287. data.vaporVL = handler.getData(3);
  288. data.vaporFR = handler.getData(2);
  289. data.VLR = handler.getData(2);
  290. data.vaporPA = handler.getData(2);
  291. data.errornum = handler.getData(2);
  292. data.liquidFR = handler.getData(2);
  293. data.pwm = handler.getData(2);
  294. data.current = handler.getData(2);
  295. return data;
  296. }
  297. public static filling_record extractFillingRecord(byte[] Buffer, int startIndex)
  298. {
  299. int bufferlen = Buffer.Length;
  300. filling_record data = new filling_record();
  301. BufferHandler handler = new BufferHandler(Buffer, startIndex);
  302. data.board = handler.getData(1);
  303. data.noz = handler.getData(1);
  304. data.fip = data.board;
  305. if (bufferlen != 22)//兼容旧协议,解析顺序改变
  306. {
  307. data.ttc = handler.getData(4);
  308. }
  309. data.liquidVL = handler.getData(3);
  310. data.liquidFR = handler.getData(2);
  311. data.vaporVL = handler.getData(3);
  312. data.vaporFR = handler.getData(2);
  313. data.VLR = handler.getData(2);
  314. data.tmBegin = handler.getDataString_BCD(7);
  315. data.tmEnd = handler.getDataString_BCD(7);
  316. data.errornum = handler.getData(2);
  317. data.vaporPA = handler.getData(2);
  318. data.pwm = handler.getData(2);
  319. return data;
  320. }
  321. public static void vr_log(string log)
  322. {
  323. }
  324. public static void vr_log_error(string log)
  325. {
  326. }
  327. }
  328. public class BufferHandler
  329. {
  330. public byte[] buffer = Array.Empty<byte>();
  331. public int startIndex = 0;
  332. public int length = 0;
  333. public BufferHandler(byte[] buf, int startindex = 0)
  334. {
  335. buffer = buf;
  336. startIndex = startindex;
  337. length = buf.Length;
  338. }
  339. public string getDataString_BCD(int len)
  340. {
  341. if (startIndex + len > length)
  342. {
  343. return "";
  344. }
  345. byte[] segment = new byte[len];
  346. Array.Copy(buffer, startIndex, segment, 0, len);
  347. string result = GlobalTool.BytesToBcd(segment);
  348. startIndex += len;
  349. return result;
  350. }
  351. public int getData(int len)
  352. {
  353. if (startIndex + len > length)
  354. {
  355. return 0;
  356. }
  357. byte[] segment = new byte[len];
  358. Array.Copy(buffer, startIndex, segment, 0, len);
  359. int val = GlobalTool.BytesToInt(segment);
  360. startIndex += len;
  361. return val;
  362. }
  363. }
  364. public class NozzleState
  365. {
  366. public int noz = 0;
  367. public string VLR = "0";
  368. public string oil = string.Empty;
  369. public int nozzlestate = GlobalTool.NozzleState_Offline;
  370. public int warnstate = GlobalTool.WarningState_Normal;
  371. }
  372. public struct filling_nozzleup
  373. {
  374. public int fip;
  375. public int board;
  376. public int noz;
  377. public string time;
  378. /* byte cmd;
  379. byte fip;
  380. byte noz;
  381. byte time[7];
  382. byte product[4];*/
  383. /* auto& cmd = src->data.CMD1;
  384. dst.target = cmd.noz;
  385. _ubcd_to_str (dst.time, sizeof dst.time, cmd.time, sizeof cmd.time);
  386. _ubcd_to_str (dst.product, sizeof dst.product, cmd.product, sizeof cmd.product);*/
  387. }
  388. public struct filling_nozzledown
  389. {
  390. public int fip;
  391. public int board;
  392. public int noz;
  393. public string time;
  394. }
  395. public struct filling_record
  396. {
  397. public int fip;
  398. public int board;
  399. public int noz;
  400. public int liquidVL;
  401. public int vaporVL;
  402. public int liquidFR;
  403. public int vaporFR;
  404. public int VLR;
  405. public int vaporPA;
  406. public int ttc;
  407. public int VLR_BEFORE;
  408. public int VLR_OFFSET;
  409. public int pwm;
  410. public string tmBegin;
  411. public string tmEnd;
  412. public int overproof;
  413. public int uploadflag;
  414. public int uploadflag2;
  415. public int uploadflag3;
  416. public int downloadflag1;
  417. public int yz;
  418. public int tankpressure;
  419. public int refuelingseconds;
  420. public int errorcontrolvalue;
  421. public int errornum;
  422. public int callbackflag;
  423. public string vccerrorinfo;
  424. }
  425. public struct filling_process
  426. {
  427. public int fip;
  428. public int board;
  429. public int noz;
  430. public int liquidVL;
  431. public int vaporVL;
  432. public int liquidFR;
  433. public int vaporFR;
  434. public int VLR;
  435. public int vaporPA;
  436. public int ttc;
  437. public int VLR_BEFORE;
  438. public int VLR_OFFSET;
  439. public int errornum;
  440. public int pwm;
  441. public int current;
  442. }
  443. public class BufferData
  444. {
  445. public int type = 0;
  446. public byte[] buffer = Array.Empty<byte>();
  447. public System.Net.IPEndPoint endpoint;
  448. public UdpClient udpClient;
  449. public SerialPort serialPort;
  450. }
  451. }