xiongzhu %!s(int64=6) %!d(string=hai) anos
pai
achega
cf1cb58d3a

+ 45 - 38
CamTool/MainWindow.xaml.cs

@@ -341,7 +341,7 @@ namespace CamTool
                     double sum = 0;
                     list.ForEach(i => sum += Math.Pow(i - avg, 2));
                     double st = Math.Sqrt(sum) / list.Count;
-                    if(st < 1 && currentWeight > 100)
+                    if (st < 1 && currentWeight > 100)
                     {
                         log.Info("稳定:" + currentWeight);
                         return currentWeight;
@@ -449,54 +449,61 @@ namespace CamTool
 
         private void DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
-            byte[] buffer = new byte[sp.BytesToRead];
-            sp.Read(buffer, 0, buffer.Length);
-
-            var hex = BitConverter.ToString(buffer, 0).Replace("-", string.Empty).ToLower();
-            foreach (byte b in buffer)
+            try
             {
-                string str = EncodingType.GetString(new byte[] { b });
-                log.Debug("serial byte: " + b.ToString() + ", " + str);
-            }
+                byte[] buffer = new byte[sp.BytesToRead];
+                sp.Read(buffer, 0, buffer.Length);
 
-            serialData.AddRange(buffer);
+                var hex = BitConverter.ToString(buffer, 0).Replace("-", string.Empty).ToLower();
+                foreach (byte b in buffer)
+                {
+                    string str = EncodingType.GetString(new byte[] { b });
+                    log.Debug("serial byte: " + b.ToString() + ", " + str);
+                }
 
-            int start = -1;
-            int end = serialData.FindLastIndex(b => b - 3 == 0);
-            if (end > -1)
-            {
-                List<byte> list = serialData.GetRange(0, end + 1);
-                start = list.FindLastIndex(b => b - 2 == 0);
-            }
-            if (end > -1)
-            {
-                if (start > -1)
+                serialData.AddRange(buffer);
+
+                int start = -1;
+                int end = serialData.FindLastIndex(b => b - 3 == 0);
+                if (end > -1)
                 {
-                    if (end - start == 11)
+                    List<byte> list = serialData.GetRange(0, end + 1);
+                    start = list.FindLastIndex(b => b - 2 == 0);
+                }
+                if (end > -1)
+                {
+                    if (start > -1)
                     {
-                        byte[] frame = serialData.GetRange(start, 12).ToArray();
-                        byte[] weightData = frame.Skip(1).Take(8).ToArray();
-                        log.Debug("raw weight data: " + EncodingType.GetString(weightData));
-
-                        string weight = EncodingType.GetString(weightData.Skip(1).Take(6).ToArray());
-                        int d = int.Parse(EncodingType.GetString(new byte[] { weightData.Last() }));
-                        if (d != 0)
+                        if (end - start == 11)
                         {
-                            weight = weight.Insert(weight.Length - d, ".");
-                        }
-                        log.Debug("weight str: " + weight);
-                        double weightValue = Convert.ToDouble(weight);
-                        if (weightValue != 0)
-                        {
-                            log.Info("weight value: " + weightValue);
-                        }
+                            byte[] frame = serialData.GetRange(start, 12).ToArray();
+                            byte[] weightData = frame.Skip(1).Take(8).ToArray();
+                            log.Debug("raw weight data: " + EncodingType.GetString(weightData));
 
-                        currentWeight = weightValue;
+                            string weight = EncodingType.GetString(weightData.Skip(1).Take(6).ToArray());
+                            int d = int.Parse(EncodingType.GetString(new byte[] { weightData.Last() }));
+                            if (d != 0)
+                            {
+                                weight = weight.Insert(weight.Length - d, ".");
+                            }
+                            log.Debug("weight str: " + weight);
+                            double weightValue = Convert.ToDouble(weight);
+                            if (weightValue != 0)
+                            {
+                                log.Info("weight value: " + weightValue);
+                            }
+
+                            currentWeight = weightValue;
 
-                        serialData.RemoveRange(0, end + 1);
+                            serialData.RemoveRange(0, end + 1);
+                        }
                     }
                 }
             }
+            catch (Exception ex)
+            {
+                log.Error("串口读取出错", ex);
+            }
 
         }
 

BIN=BIN
CamTool/bin/x86/Debug/CamTool.exe


BIN=BIN
CamTool/bin/x86/Debug/CamTool.pdb


+ 9 - 3
CamTool/obj/Debug/App.g.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\App.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "F09A3B0C43C1B5AEEFD297F4D43348624DF192BBFF4EC142FF5E2E5CAC3CD633"
+#pragma checksum "..\..\App.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "7549374B6636FB82FA6DFC33C5CF2330DBFED00B42343200CADDA91A12E1548C"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     此代码由工具生成。
@@ -47,13 +47,19 @@ namespace CamTool {
         [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
         public void InitializeComponent() {
             
-            #line 5 "..\..\App.xaml"
+            #line 6 "..\..\App.xaml"
             this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(this.Application_DispatcherUnhandledException);
             
             #line default
             #line hidden
             
-            #line 5 "..\..\App.xaml"
+            #line 7 "..\..\App.xaml"
+            this.Startup += new System.Windows.StartupEventHandler(this.Application_Startup);
+            
+            #line default
+            #line hidden
+            
+            #line 8 "..\..\App.xaml"
             this.Exit += new System.Windows.ExitEventHandler(this.Application_Exit);
             
             #line default

+ 9 - 3
CamTool/obj/Debug/App.g.i.cs

@@ -1,4 +1,4 @@
-#pragma checksum "..\..\App.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "F09A3B0C43C1B5AEEFD297F4D43348624DF192BBFF4EC142FF5E2E5CAC3CD633"
+#pragma checksum "..\..\App.xaml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "7549374B6636FB82FA6DFC33C5CF2330DBFED00B42343200CADDA91A12E1548C"
 //------------------------------------------------------------------------------
 // <auto-generated>
 //     此代码由工具生成。
@@ -47,13 +47,19 @@ namespace CamTool {
         [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
         public void InitializeComponent() {
             
-            #line 5 "..\..\App.xaml"
+            #line 6 "..\..\App.xaml"
             this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(this.Application_DispatcherUnhandledException);
             
             #line default
             #line hidden
             
-            #line 5 "..\..\App.xaml"
+            #line 7 "..\..\App.xaml"
+            this.Startup += new System.Windows.StartupEventHandler(this.Application_Startup);
+            
+            #line default
+            #line hidden
+            
+            #line 8 "..\..\App.xaml"
             this.Exit += new System.Windows.ExitEventHandler(this.Application_Exit);
             
             #line default

BIN=BIN
CamTool/obj/Debug/CamTool.csprojAssemblyReference.cache


BIN=BIN
CamTool/obj/Debug/CamTool.exe


BIN=BIN
CamTool/obj/Debug/CamTool.pdb


BIN=BIN
CamTool/obj/x86/Debug/CamTool.exe


BIN=BIN
CamTool/obj/x86/Debug/CamTool.pdb