HandleData.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using AntDesign;
  2. using Microsoft.AspNetCore.Components;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. namespace EasyTemplate.Service
  6. {
  7. public class HandleData
  8. {
  9. private Thread _thread;
  10. // [Inject] public UdpListenerService udpclient { get; set; }
  11. //public UdpListenerService udpclient { get; set; } = default!;
  12. //private readonly UdpListenerService _udpclient;
  13. public HandleData()//UdpListenerService myService)
  14. {
  15. //_udpclient = myService;
  16. // 初始化线程,但不立即启动
  17. _thread = new Thread(new ThreadStart(Loop));
  18. _thread.IsBackground = true; // 设置线程为后台线程,程序结束时自动结束
  19. _thread.Start(); // 启动线程
  20. }
  21. private void Loop()
  22. {
  23. while (true) // 无限循环
  24. {
  25. Console.WriteLine("Looping..."); // 执行一些任务
  26. try
  27. {
  28. BufferData bd = GlobalTool.g_dataQueue.TryDequeue(out BufferData result) ? result : null;
  29. if(bd==null)
  30. {
  31. continue;
  32. }
  33. //TQC dest-2;source-2;frame-1;length-2(HEX);cmd-1
  34. //VRC 包头(0xfa)+ 目标地址2+源地址2+控制/帧号1+数据长度2(BCD)+命令字1+数据 + CRC校验2
  35. if(bd.type == 0)
  36. {
  37. int datalength = bd.buffer[5] * 256 + bd.buffer[6];
  38. if (datalength + 9 == bd.buffer.Length)
  39. {
  40. // 计算crc
  41. int packlen = bd.buffer.Length;
  42. byte[] tmpbuf = bd.buffer;
  43. ushort nSum = GlobalTool.chkcrc(tmpbuf, (ushort)(packlen - 2), 0xA001);
  44. ushort newSum = nSum;
  45. newSum = (ushort)(newSum / 256 + newSum % 256 * 256);
  46. ushort oldSum = BitConverter.ToUInt16(tmpbuf, packlen - 2);
  47. if (oldSum == newSum)
  48. {
  49. Console.WriteLine("CRC校验成功");
  50. }
  51. else
  52. {
  53. Console.WriteLine("crc校验失败");
  54. continue;
  55. }
  56. }
  57. else if (datalength + 9 == bd.buffer.Length + 2)
  58. {
  59. //旧协议无crc
  60. }
  61. else
  62. {
  63. Console.WriteLine("数据长度不对");
  64. continue;
  65. }
  66. }
  67. else
  68. {
  69. int datalength = GlobalTool.ConvertBCDToDecimalSingle(bd.buffer[6]) * 100 + GlobalTool.ConvertBCDToDecimalSingle(bd.buffer[7]);
  70. if (datalength + 10 == bd.buffer.Length)
  71. {
  72. }
  73. else
  74. {
  75. Console.WriteLine("数据长度不对");
  76. continue;
  77. }
  78. }
  79. int cmdtype = bd.buffer[7];
  80. int startindex = 8;
  81. if (bd.type == 1)
  82. {
  83. cmdtype = bd.buffer[8];
  84. startindex = 9;
  85. }
  86. switch (cmdtype)
  87. {
  88. case 1:
  89. filling_nozzleup up = GlobalTool.extractFillingNozzleUP(bd.buffer, startindex);
  90. GlobalTool.g_mNozzleState[up.noz].nozzlestate = GlobalTool.NozzleState_Filling;
  91. break;
  92. case 2:
  93. filling_process process = GlobalTool.extractFillingProcess(bd.buffer, startindex);
  94. GlobalTool.g_mNozzleState[process.noz].nozzlestate = GlobalTool.NozzleState_Filling;
  95. GlobalTool.g_mNozzleState[process.noz].VLR = ((double)process.VLR / 1000).ToString("F2");
  96. break;
  97. case 3:
  98. filling_nozzledown down = GlobalTool.extractFillingNozzleDown(bd.buffer, startindex);
  99. GlobalTool.g_mNozzleState[down.noz].nozzlestate = GlobalTool.NozzleState_Idle;
  100. break;
  101. case 4:
  102. filling_record record = GlobalTool.extractFillingRecord(bd.buffer, startindex);
  103. GlobalTool.g_mNozzleState[record.noz].nozzlestate = GlobalTool.NozzleState_Idle;
  104. GlobalTool.g_mNozzleState[record.noz].VLR = ((double)record.VLR / 1000).ToString("F2");
  105. break;
  106. default:
  107. continue;
  108. }
  109. if(bd.type == 0)
  110. {
  111. var message = Encoding.UTF8.GetString(bd.buffer);
  112. //_logger.LogInformation($"接收到消息: {result.Buffer.ToString()}\r\n");
  113. byte[] responseData = Encoding.UTF8.GetBytes("Echo: " + message);
  114. bd.udpClient.SendAsync(responseData, responseData.Length, bd.endpoint);
  115. }
  116. else
  117. {
  118. var message = Encoding.UTF8.GetString(bd.buffer);
  119. //_logger.LogInformation($"接收到消息: {result.Buffer.ToString()}\r\n");
  120. byte[] responseData = Encoding.UTF8.GetBytes("Echo: " + message);
  121. bd.serialPort.Write(responseData, 0, responseData.Length);
  122. }
  123. }
  124. catch(Exception e)
  125. {
  126. }
  127. Thread.Sleep(50);
  128. }
  129. }
  130. }
  131. }