Browse Source

增加LRC校验位运算方法

Hcsy 6 years ago
parent
commit
07e9b4f114
2 changed files with 31 additions and 3 deletions
  1. 28 1
      PLCDataReader/ClassFile/ModeBusTCPServer.cs
  2. 3 2
      PLCDataReader/MainWindow.Designer.cs

+ 28 - 1
PLCDataReader/ClassFile/ModeBusTCPServer.cs

@@ -142,11 +142,38 @@ namespace ClassFile
                                         // Get the integral value of the character.
                                         int value = Convert.ToInt32(letter);
                                         // Convert the decimal value to a hexadecimal value in string form.
-                                        string hexOutput = String.Format("{0:X}", value);
+                                        string hexOutput = string.Format("{0:X}", value);
                                         strH += hexOutput + " ";
                                     }
                                     item.Send(Encoding.UTF8.GetBytes(strH));
                                     break;
+                                case "LRC":
+                                    string message = SENDMESSAGE.Text.Trim();
+                                    if (message.Length % 2 != 0) {
+                                        message = message + "0";
+                                    }
+
+                                    int LRC = 0x0;
+                                    for (int i = 0; i < message.Length; i=i+2)
+                                    {
+                                        int inside =int.Parse(message.Substring(i,1).ToString()+ message.Substring(i+1,1).ToString(), System.Globalization.NumberStyles.HexNumber);
+                                        LRC += inside;
+                                    }
+                                    string _LRC = string.Format("{0:X2}", LRC);
+                                    string LRCre = "";
+                                    for (int i = 0; i < _LRC.Length; i++)
+                                    {
+                                        int index;
+                                        if (i != _LRC.Length - 1)
+                                        {
+                                             index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber);
+                                        }else
+                                             index = 0xF - int.Parse(_LRC.Substring(i, 1).ToString(), System.Globalization.NumberStyles.HexNumber)+1;
+                                        LRCre += string.Format("{0:X}", index);
+                                    }
+
+                                    item.Send(Encoding.UTF8.GetBytes(LRCre.ToString()));
+                                    break;
                             }                        
                         }
                     }

+ 3 - 2
PLCDataReader/MainWindow.Designer.cs

@@ -141,7 +141,8 @@
             this.Encoding.Items.AddRange(new object[] {
             "UTF-8",
             "ASCII",
-            "Hexadecimal"});
+            "Hexadecimal",
+            "LRC"});
             this.Encoding.Location = new System.Drawing.Point(533, 89);
             this.Encoding.Name = "Encoding";
             this.Encoding.Size = new System.Drawing.Size(157, 20);
@@ -186,7 +187,7 @@
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
             this.Text = "PLCDataReader";
             this.Load += new System.EventHandler(this.Form1_Load);
-            this.ResumeLayout(false);
+            this.ResumeLayout(true);
             this.PerformLayout();
 
         }