林豪 左 3 жил өмнө
parent
commit
05c4b15c5f

+ 47 - 3
WCS.Core/DataTrans/DataBlock.cs

@@ -6,6 +6,7 @@ using System.Diagnostics;
 using System.Linq;
 using System.Reflection;
 using System.Runtime.InteropServices;
+using System.Text;
 using WCS.Entity;
 
 namespace WCS.Core
@@ -71,7 +72,6 @@ namespace WCS.Core
             }
             finally
             {
-
                 if (sw.ElapsedMilliseconds > 500)
                 {
                     Console.ForegroundColor = ConsoleColor.Yellow;
@@ -156,6 +156,10 @@ namespace WCS.Core
                 var revers = Entity.PLC.TYPE == PLCType.西门子;
 
                 return ReadPrimitive(type, ref bitStart, revers);
+
+                //var revers = Entity.PLC.TYPE == PLCType.西门子;
+
+                //return ReadPrimitive(type, ref bitStart, false);
             }
             else
             {
@@ -235,7 +239,7 @@ namespace WCS.Core
             if (type == typeof(bool))
                 size = 1;
             var data = ReadBytes(ref bitStart, size);
-            if (reverse)
+            if (reverse && type != typeof(float))
                 data = data.Reverse().ToArray();
             if (type == typeof(byte))
             {
@@ -275,7 +279,8 @@ namespace WCS.Core
             }
             else if (type == typeof(float))
             {
-                return BitConverter.ToSingle(data, 0);
+                BitConverter.ToSingle(data, 0);
+                return GetToTwo(data);
             }
             else if (type == typeof(double))
             {
@@ -287,6 +292,45 @@ namespace WCS.Core
             }
         }
 
+        /// <summary>
+        /// 将字节转换成2进制的单精度浮点数
+        /// </summary>
+        /// <param name="ByteData"></param>
+        /// <returns></returns>
+        public float GetToTwo(byte[] ByteData)
+        {
+            try
+            {
+                int Length = ByteData.Length;
+                string Result = string.Empty;
+                for (int j = 0; j < Length; j++)
+                {
+                    string hexString = ByteData[j].ToString("X2");
+                    hexString = hexString.Replace(" ", "");
+                    if ((hexString.Length % 2) != 0)
+                    {
+                        hexString = hexString.PadRight(hexString.Length + 1);
+                    }
+                    StringBuilder builder = new StringBuilder();
+                    int len = hexString.Length / 2;
+                    for (int i = 0; i < len; i++)
+                    {
+                        string hex = hexString.Substring(i * 2, 2).Trim();
+                        int a = Convert.ToInt32(hex, 16);
+                        string str = Convert.ToString(a, 2).PadLeft(8, '0');
+                        builder = builder.Append(str);
+                    }
+                    Result += builder.ToString();
+                }
+                ;
+                return Convert.ToInt32(Result, 2);
+            }
+            catch (Exception ex)
+            {
+                throw new Exception("单精度浮点数转换失败");
+            }
+        }
+
         private byte[] ReadBytes(ref int bitStart, ushort length)
         {
             var start = GetByteStart(bitStart);

+ 1 - 1
WCS.Core/DebugPublisher.cs

@@ -15,7 +15,7 @@ namespace WCS.Core
         {
             try
             {
-                Redis.Publish(channel.Replace(".", "-"), msg);
+                Redis.Publish(channel, msg);
             }
             catch { }
         }