| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using AntDesign;
- using Microsoft.AspNetCore.Components;
- using System.Net.Sockets;
- using System.Text;
- namespace EasyTemplate.Service
- {
- public class HandleData
- {
- private Thread _thread;
- // [Inject] public UdpListenerService udpclient { get; set; }
- //public UdpListenerService udpclient { get; set; } = default!;
- //private readonly UdpListenerService _udpclient;
-
- public HandleData()//UdpListenerService myService)
- {
- //_udpclient = myService;
- // 初始化线程,但不立即启动
- _thread = new Thread(new ThreadStart(Loop));
- _thread.IsBackground = true; // 设置线程为后台线程,程序结束时自动结束
- _thread.Start(); // 启动线程
- }
- private void Loop()
- {
- while (true) // 无限循环
- {
- Console.WriteLine("Looping..."); // 执行一些任务
- try
- {
- BufferData bd = GlobalTool.g_dataQueue.TryDequeue(out BufferData result) ? result : null;
- if(bd==null)
- {
- continue;
- }
- //TQC dest-2;source-2;frame-1;length-2(HEX);cmd-1
- //VRC 包头(0xfa)+ 目标地址2+源地址2+控制/帧号1+数据长度2(BCD)+命令字1+数据 + CRC校验2
- if(bd.type == 0)
- {
- int datalength = bd.buffer[5] * 256 + bd.buffer[6];
- if (datalength + 9 == bd.buffer.Length)
- {
- // 计算crc
- int packlen = bd.buffer.Length;
- byte[] tmpbuf = bd.buffer;
- ushort nSum = GlobalTool.chkcrc(tmpbuf, (ushort)(packlen - 2), 0xA001);
- ushort newSum = nSum;
- newSum = (ushort)(newSum / 256 + newSum % 256 * 256);
- ushort oldSum = BitConverter.ToUInt16(tmpbuf, packlen - 2);
- if (oldSum == newSum)
- {
- Console.WriteLine("CRC校验成功");
- }
- else
- {
- Console.WriteLine("crc校验失败");
- continue;
- }
- }
- else if (datalength + 9 == bd.buffer.Length + 2)
- {
- //旧协议无crc
- }
- else
- {
- Console.WriteLine("数据长度不对");
- continue;
- }
- }
- else
- {
- int datalength = GlobalTool.ConvertBCDToDecimalSingle(bd.buffer[6]) * 100 + GlobalTool.ConvertBCDToDecimalSingle(bd.buffer[7]);
- if (datalength + 10 == bd.buffer.Length)
- {
- }
- else
- {
- Console.WriteLine("数据长度不对");
- continue;
- }
- }
- int cmdtype = bd.buffer[7];
- int startindex = 8;
- if (bd.type == 1)
- {
- cmdtype = bd.buffer[8];
- startindex = 9;
- }
- switch (cmdtype)
- {
- case 1:
- filling_nozzleup up = GlobalTool.extractFillingNozzleUP(bd.buffer, startindex);
- GlobalTool.g_mNozzleState[up.noz].nozzlestate = GlobalTool.NozzleState_Filling;
- break;
- case 2:
- filling_process process = GlobalTool.extractFillingProcess(bd.buffer, startindex);
- GlobalTool.g_mNozzleState[process.noz].nozzlestate = GlobalTool.NozzleState_Filling;
- GlobalTool.g_mNozzleState[process.noz].VLR = ((double)process.VLR / 1000).ToString("F2");
- break;
- case 3:
- filling_nozzledown down = GlobalTool.extractFillingNozzleDown(bd.buffer, startindex);
- GlobalTool.g_mNozzleState[down.noz].nozzlestate = GlobalTool.NozzleState_Idle;
- break;
- case 4:
- filling_record record = GlobalTool.extractFillingRecord(bd.buffer, startindex);
- GlobalTool.g_mNozzleState[record.noz].nozzlestate = GlobalTool.NozzleState_Idle;
- GlobalTool.g_mNozzleState[record.noz].VLR = ((double)record.VLR / 1000).ToString("F2");
- break;
- default:
- continue;
- }
- if(bd.type == 0)
- {
- var message = Encoding.UTF8.GetString(bd.buffer);
- //_logger.LogInformation($"接收到消息: {result.Buffer.ToString()}\r\n");
- byte[] responseData = Encoding.UTF8.GetBytes("Echo: " + message);
- bd.udpClient.SendAsync(responseData, responseData.Length, bd.endpoint);
- }
- else
- {
- var message = Encoding.UTF8.GetString(bd.buffer);
- //_logger.LogInformation($"接收到消息: {result.Buffer.ToString()}\r\n");
- byte[] responseData = Encoding.UTF8.GetBytes("Echo: " + message);
- bd.serialPort.Write(responseData, 0, responseData.Length);
- }
- }
- catch(Exception e)
- {
- }
- Thread.Sleep(50);
- }
- }
- }
- }
|