Parcourir la source

Merge branch 'master' of http://afc.siasun.com/gitlab/siasun.autopilot/siasun.autopilot.git

mengqi li il y a 4 ans
Parent
commit
640c922aa7

+ 51 - 9
SIASUN.Autopilot.BLL/Perception/BlCanBusPerception.cs

@@ -1,4 +1,5 @@
-using SIASUN.Autopilot.Device;
+using ProjectBase.Data.Logs;
+using SIASUN.Autopilot.Device;
 using SIASUN.Autopilot.Device.Command;
 using SIASUN.Autopilot.Device.Message;
 using System;
@@ -16,14 +17,19 @@ namespace SIASUN.Autopilot.BLL
 
 
         /// <summary>
-        /// 车辆状态1事件响应函数
+        /// 车辆状态1事件
         /// </summary>
-        VehicleStatus1 comVehicleStatus1;
+        private VehicleStatus1 comVehicleStatus1;
 
         /// <summary>
-        /// 车辆状态2事件响应函数
+        /// 车辆状态2事件
         /// </summary>
-        VehicleStatus2 comVehicleStatus2;
+        private VehicleStatus2 comVehicleStatus2;
+
+        /// <summary> 
+        /// 车辆状态3事件
+        /// </summary>
+        private VehicleStatus3 comVehicleStatus3;
 
         // 实际车速
         private long realSpeed = 0;
@@ -58,15 +64,28 @@ namespace SIASUN.Autopilot.BLL
             comVehicleStatus2 = new VehicleStatus2();
             comVehicleStatus2.ReceiveMessage += ComVehicleStatus2_ReceiveMessage;
             comVehicleStatus2.Subscribe();
+
+            comVehicleStatus3 = new VehicleStatus3();
+            comVehicleStatus3.ReceiveMessage += ComVehicleStatus3_ReceiveMessage;
+            comVehicleStatus3.Subscribe();
         }
 
+       
+
         /// <summary>
         /// 车辆状态1响应函数
         /// </summary>
         /// <param name="e"></param>
         private void ComVehicleStatus1_ReceiveMessage(ReceiveMessageEventArgs e)
         {
-            var m = e.Message as 车辆状态1;
+            try
+            {
+                var m = e.Message as 车辆状态1;
+            }
+            catch (Exception ex)
+            {
+                LogHelper.log.Error("接收到车辆状态1的数据感知层出现错误",ex);
+            }
         }
 
         /// <summary>
@@ -75,9 +94,32 @@ namespace SIASUN.Autopilot.BLL
         /// <param name="e"></param>
         private void ComVehicleStatus2_ReceiveMessage(ReceiveMessageEventArgs e)
         {
-            var m = e.Message as 车辆状态2;
-            realSpeed = Convert.ToInt64(m.实际车速);
-            realAng = Convert.ToInt64(m.实际方向盘转角);
+            try
+            {
+                var m = e.Message as 车辆状态2;
+                realSpeed = Convert.ToInt64(m.实际车速);
+                realAng = Convert.ToInt64(m.实际方向盘转角);
+            }
+            catch (Exception ex)
+            {
+                LogHelper.log.Error("接收到车辆状态2的数据感知层出现错误", ex);
+            }       
+        }
+
+        /// <summary>
+        /// 车辆状态3响应函数
+        /// </summary>
+        /// <param name="e"></param>
+        private void ComVehicleStatus3_ReceiveMessage(ReceiveMessageEventArgs e)
+        {
+            try
+            {
+                var m = e.Message as 车辆状态3;
+            }
+            catch (Exception ex)
+            {
+                LogHelper.log.Error("接收到车辆状态3的数据感知层出现错误", ex);
+            }
         }
 
 

+ 1 - 4
SIASUN.Autopilot.Device/Command/VehicleStatus1.cs

@@ -26,8 +26,6 @@ namespace SIASUN.Autopilot.Device.Command
         private void ProcessPackage(PackageProcessingEventArgs e)
         {
             //接收到车辆状态1数据,转换常规数据后触发ReceiveMessage处理。
-
-            //拆包处理
             try
             {
                 //通知
@@ -39,8 +37,7 @@ namespace SIASUN.Autopilot.Device.Command
             }
             catch (Exception ex)
             {
-                LogHelper.log.Error("车辆状态1ProcessPackage事件出现未知异常。", ex);
-                //TODO,日志
+                LogHelper.log.Error("车辆状态1->ProcessPackage事件出现未知异常。", ex);
                 var args = new ReceiveMessageEventArgs(null);
                 ReceiveMessage(args);
             }

+ 2 - 5
SIASUN.Autopilot.Device/Command/VehicleStatus2.cs

@@ -25,9 +25,7 @@ namespace SIASUN.Autopilot.Device.Command
 
         private void ProcessPackage(PackageProcessingEventArgs e)
         {
-            //接收到车辆状态1数据,转换常规数据后触发ReceiveMessage处理。
-
-            //拆包处理
+            //接收到车辆状态2数据,转换常规数据后触发ReceiveMessage处理。
             try
             {
                 //通知
@@ -39,8 +37,7 @@ namespace SIASUN.Autopilot.Device.Command
             }
             catch (Exception ex)
             {
-                LogHelper.log.Error("车辆状态2ProcessPackage事件出现未知异常。", ex);
-                //TODO,日志
+                LogHelper.log.Error("车辆状态2->ProcessPackage事件出现未知异常。", ex);
                 var args = new ReceiveMessageEventArgs(null);
                 ReceiveMessage(args);
             }

+ 51 - 0
SIASUN.Autopilot.Device/Command/VehicleStatus3.cs

@@ -0,0 +1,51 @@
+using ProjectBase.Data.Logs;
+using SIASUN.Autopilot.Device.Message;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace SIASUN.Autopilot.Device.Command
+{
+    /// <summary>
+    /// 车辆状态3
+    /// </summary>
+    public class VehicleStatus3
+    {
+        /// <summary>
+        /// 接收到车辆状态触发
+        /// </summary>
+        public event ReceiveMessageEventHandler ReceiveMessage;
+        public void Subscribe()
+        {
+            EventManage.Instance.SubscribeEvent(车辆状态3.OperationCode, new EventProcessingHandler(ProcessPackage));
+        }
+
+        public void UnSubscribe()
+        {
+            EventManage.Instance.UnSubscribeEvent(车辆状态3.OperationCode, ProcessPackage);
+        }
+
+        private void ProcessPackage(PackageProcessingEventArgs e)
+        {
+            //接收到车辆状态3数据,转换常规数据后触发ReceiveMessage处理。
+            try
+            {
+                //通知
+                if (ReceiveMessage != null)
+                {
+                    var args = new ReceiveMessageEventArgs(e.PackageBytes[0] as 车辆状态3);
+                    ReceiveMessage(args);
+                }
+            }
+            catch (Exception ex)
+            {
+                LogHelper.log.Error("车辆状态3->ProcessPackage事件出现未知异常。", ex);
+                //TODO,日志
+                var args = new ReceiveMessageEventArgs(null);
+                ReceiveMessage(args);
+            }
+        }
+    }
+}

+ 50 - 23
SIASUN.Autopilot.Device/Common/MessageCoder.cs

@@ -89,32 +89,15 @@ namespace SIASUN.Autopilot.Device
          /// <param name="lengthBit"></param>
          /// <param name="bytedata"></param>
          /// <returns></returns>
-        private static long de_serializeField(int startBit, int lengthBit, byte[] bytedata)
+        private static string de_serializeField(int startBit, int lengthBit, byte[] bytedata)
         {
             int byteth = startBit / ByteLength; // 第几个字节
             int bytesLength = (startBit%ByteLength + lengthBit )/ ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0; 
             byte[] data = new byte[bytesLength];
+            byte[] resultByteData = new byte[bytesLength];
             Buffer.BlockCopy(bytedata, byteth, data, 0, bytesLength);
-            long result = 0;
-            for (int i =0; i< bytesLength; i++)
-            {
-                if (bytesLength >0)
-                {
-                    for (int j = 0; j < ByteLength; j++)
-                    {
-                        result += byteToBit(data[i], j + startBit % ByteLength);
-                    }
-                    bytesLength = bytesLength - 1;
-                }
-                else
-                {
-                    for (int j = 0; j < startBit % ByteLength + lengthBit - ByteLength; j++)
-                    {
-                        result += byteToBit(data[i], j);
-                    }
-                }             
-            }
-            return result;
+            BitOR(resultByteData, RightShift(data, startBit % ByteLength));
+            return  de_serialize_hex(resultByteData);
         }
 
 
@@ -123,7 +106,7 @@ namespace SIASUN.Autopilot.Device
         {
             int byteth = startBit / ByteLength; // 第几个字节
             int bytesLength = (startBit % ByteLength + lengthBit) / ByteLength + ((startBit % ByteLength + lengthBit) % ByteLength) > 0 ? 1 : 0;
-            byte[] byteArray = System.Text.Encoding.Default.GetBytes(value);
+            byte[] byteArray = serialize_hex(value, bytesLength);
             byte[] resultByteData = new byte[8] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00};
             if (byteth< 8)
             {
@@ -135,7 +118,51 @@ namespace SIASUN.Autopilot.Device
             }
             return resultByteData;
         }
-       
+
+        public static byte[] serialize_hex(string value, int length)
+        {
+            byte[] returnBytes = new byte[length];
+            bool ishexstring = false;
+
+            if (string.IsNullOrEmpty(value))
+            {
+                return returnBytes;
+            }
+
+            ishexstring = true;
+            value = value.Replace(" ", "");
+            if ((value.Length % 2) != 0)
+            {
+                value = "0" + value;
+            }
+
+            for (int i = 0; i < value.Length / 2; i++)
+            {
+                if (ishexstring)
+                {
+                    returnBytes[i] = Convert.ToByte(value.Substring(i * 2, 2), 16);
+                }
+                else
+                {
+                    returnBytes[i] = Convert.ToByte(value.Substring(i, 1), 16);
+                }
+            }
+            return returnBytes;
+        }
+
+        public static string de_serialize_hex(byte[] bsdata)
+        {
+            string returnStr = "";
+            if (bsdata != null)
+            {
+                for (int i = 0; i < bsdata.Length; i++)
+                {
+                    returnStr += bsdata[i].ToString("X2");
+                }
+            }
+            return returnStr;
+        }
+
 
         /// <summary>
         /// byte转化成bit

+ 1 - 0
SIASUN.Autopilot.Device/SIASUN.Autopilot.Device.csproj

@@ -53,6 +53,7 @@
     <Compile Include="CANbus\PCANBasic.cs" />
     <Compile Include="Command\VehicleStatus1.cs" />
     <Compile Include="Command\VehicleStatus2.cs" />
+    <Compile Include="Command\VehicleStatus3.cs" />
     <Compile Include="Common\DataAttribute.cs" />
     <Compile Include="Common\EventManage.cs" />
     <Compile Include="Common\MessageCoder.cs" />