Browse Source

feat(HengshanPaymentTerminal):crc改为CRC-16/IBM,心跳发送油枪状态

Zhenghanjv 6 months ago
parent
commit
835ed7c7e0

+ 4 - 10
HengshanPaymentTerminal/MessageEntity/Incoming/HeartBeatMessage.cs

@@ -27,13 +27,12 @@ namespace HengshanPaymentTerminal.MessageEntity.Incoming
             this.getBaseData(datas);
             this.GunCount = datas[7];
             List<HeartBeatNozzleState> nozzleStates = new List<HeartBeatNozzleState>();
-            for (int index = 8;index < datas.Length - 2;index += 3)
+            for (int index = 8;index < datas.Length - 2;index += 2)
             {
                 HeartBeatNozzleState nozzle = new HeartBeatNozzleState()
                 {
                     NozzleNum = datas[index],
-                    WorkState = datas[index + 1],
-                    AccreditState = datas[index + 2]
+                    STATU = datas[index + 1]
                 };
                 nozzleStates.Add(nozzle);
             }
@@ -56,13 +55,8 @@ namespace HengshanPaymentTerminal.MessageEntity.Incoming
         public int NozzleNum { get; set; }
 
         /// <summary>
-        /// 上下班状态 0:下班;1:上班
+        /// 油枪状态 01:离线;02:锁枪;03:空闲;04:提枪;06:开始加油;08:加油中
         /// </summary>
-        public int WorkState { get; set; }
-
-        /// <summary>
-        /// 授权模式状态 0表示前庭;1表示后台
-        /// </summary>
-        public int AccreditState { get; set; }
+        public int STATU { get; set; }
     }
 }

+ 10 - 11
HengshanPaymentTerminal/Parser.cs

@@ -7,6 +7,8 @@ using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using AutoMapper.Execution;
+using Microsoft.EntityFrameworkCore.Metadata.Internal;
+using Org.BouncyCastle.Utilities;
 
 
 namespace HengshanPaymentTerminal
@@ -139,10 +141,8 @@ namespace HengshanPaymentTerminal
         0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
         };
 
-        // CRC-16/CCITT-FALSE多项式  
-        private const ushort Polynomial = 0x1021;
-        // 初始值  
-        private const ushort InitialValue = 0xFFFF;
+        private const ushort Polynomial = 0xA001; // CRC-16/IBM 多项式
+        private const ushort InitialValue = 0x0000;// CRC-16/IBM 初始值  
 
         public static ushort ComputeCrc(byte[] data)
         {
@@ -163,24 +163,23 @@ namespace HengshanPaymentTerminal
             return BitConverter.GetBytes(result).Reverse().ToArray();
         }
 
-        // 计算CRC-16/CCITT-FALSE校验值  
+        // 计算CRC-16/IBM校验值  
         public static ushort ComputeChecksum(byte[] data)
         {
             ushort crc = InitialValue;
 
             foreach (byte b in data)
             {
-                crc ^= (ushort)((b << 8) & 0xFF00); // 将字节数据左移8位并与0xFF00进行与操作  
-
+                crc ^= b; // XOR 字节数据
                 for (int i = 0; i < 8; i++)
                 {
-                    if ((crc & 0x8000) != 0) // 检查最高位是否为1  
+                    if ((crc & 0x0001) != 0) // 检查最低位是否为 1
                     {
-                        crc = (ushort)((crc << 1) ^ Polynomial); // 左移1位并与多项式进行异或  
+                        crc = (ushort)((crc >> 1) ^ Polynomial); // 右移并异或多项式
                     }
                     else
                     {
-                        crc <<= 1; // 仅左移1位  
+                        crc >>= 1; // 直接右移
                     }
                 }
             }
@@ -188,7 +187,7 @@ namespace HengshanPaymentTerminal
             return crc;
         }
 
-        // 计算CRC-16/CCITT-FALSE校验值,返回数组
+        // 计算CRC-16/IBM校验值,返回数组
         public static byte[] ComputeChecksumToBytes(byte[] data)
         {
             byte[] result = new byte[2];

+ 1 - 1
HengshanPaymentTerminal/Test/CalculatorTest.cs

@@ -19,7 +19,7 @@ namespace HengshanPaymentTerminal.Test
             byte[] bytes = { 0x00, 0x00, 0x01, 0x00, 0x01, 0x30};
             ushort value = HengshanCRC16.ComputeChecksum(bytes);
             byte[] bytes1 = HengshanCRC16.ComputeChecksumToBytes(bytes);
-            Console.WriteLine($"CRC TEST RESULT:{value}");
+            Console.WriteLine($"CRC TEST RESULT:{value},hex:{BitConverter.ToString(bytes1).Replace("-"," ")}");
         }
 
         [Fact]