Browse Source

PLCDEMO增加数据格式转换

Hcsy 6 years ago
parent
commit
c04e10f12e

+ 32 - 3
PLCDataReader/ClassFile/ModeBusTCPServer.cs

@@ -61,12 +61,12 @@ namespace ClassFile
 
         RichTextAutoBottom richtext;
         ComboBox SelectIP;
+        ComboBox Encodingbox;
 
         Button SEND;
         RichTextAutoBottom SENDMESSAGE;
         public List<Socket> list = new List<Socket>();
 
-        private Boolean messageflag = false;
 
         public ModeBusTCPServer()
         {
@@ -77,6 +77,7 @@ namespace ClassFile
         {
             try
             {
+                Encodingbox = Application.OpenForms["MainWindow"].Controls["Encoding"] as ComboBox;
                 SelectIP = Application.OpenForms["MainWindow"].Controls["SelectIP"] as ComboBox;
                 richtext = Application.OpenForms["MainWindow"].Controls["Result"] as RichTextAutoBottom;
                 SEND = Application.OpenForms["MainWindow"].Controls["SEND"] as Button;
@@ -118,11 +119,39 @@ namespace ClassFile
                     {
                         if (item.RemoteEndPoint.ToString() == SelectIP.Text)
                         {
-                            item.Send(Encoding.UTF8.GetBytes("\n" + SENDMESSAGE.Text));
+                            switch (Encodingbox.Text)
+                            {
+                                case "UTF-8":
+                                    item.Send(Encoding.UTF8.GetBytes(SENDMESSAGE.Text));
+                                    break;
+                                case "ASCII":
+                                    byte[] array = Encoding.ASCII.GetBytes(SENDMESSAGE.Text.Trim());
+                                    string str = null;
+                                    for (int i = 0; i < array.Length; i++)
+                                    {
+                                        int asciicode = (int)(array[i]);
+                                        str += Convert.ToString(asciicode) + " ";
+                                    }
+                                    item.Send(Encoding.ASCII.GetBytes(str));
+                                    break;
+                                case "Hexadecimal":
+                                    char[] values = SENDMESSAGE.Text.ToCharArray();
+                                    string strH = null;
+                                    foreach (char letter in values)
+                                    {
+                                        // 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);
+                                        strH += hexOutput + " ";
+                                    }
+                                    item.Send(Encoding.UTF8.GetBytes(strH));
+                                    break;
+                            }                        
                         }
                     }
                     else
-                    {
+                    {   
                         int index = SelectIP.Items.IndexOf(item.RemoteEndPoint.ToString());
                         SelectIP.Items.RemoveAt(index);
                         if (SelectIP.Items.Count > 0)

+ 30 - 3
PLCDataReader/MainWindow.Designer.cs

@@ -37,6 +37,8 @@
             this.SelectIP = new System.Windows.Forms.ComboBox();
             this.SEND = new System.Windows.Forms.Button();
             this.IPTEXT = new System.Windows.Forms.Label();
+            this.label1 = new System.Windows.Forms.Label();
+            this.Encoding = new System.Windows.Forms.ComboBox();
             this.SENDMESSAGE = new UAS_PLCDataReader.RichTextAutoBottom();
             this.Result = new UAS_PLCDataReader.RichTextAutoBottom();
             this.SuspendLayout();
@@ -99,7 +101,7 @@
             // SelectIP
             // 
             this.SelectIP.FormattingEnabled = true;
-            this.SelectIP.Location = new System.Drawing.Point(531, 64);
+            this.SelectIP.Location = new System.Drawing.Point(533, 39);
             this.SelectIP.Name = "SelectIP";
             this.SelectIP.Size = new System.Drawing.Size(159, 20);
             this.SelectIP.TabIndex = 7;
@@ -116,12 +118,33 @@
             // IPTEXT
             // 
             this.IPTEXT.AutoSize = true;
-            this.IPTEXT.Location = new System.Drawing.Point(531, 24);
+            this.IPTEXT.Location = new System.Drawing.Point(531, 15);
             this.IPTEXT.Name = "IPTEXT";
             this.IPTEXT.Size = new System.Drawing.Size(53, 12);
             this.IPTEXT.TabIndex = 10;
             this.IPTEXT.Text = "已连接IP";
             // 
+            // label1
+            // 
+            this.label1.AutoSize = true;
+            this.label1.Location = new System.Drawing.Point(533, 66);
+            this.label1.Name = "label1";
+            this.label1.Size = new System.Drawing.Size(53, 12);
+            this.label1.TabIndex = 11;
+            this.label1.Text = "编码格式";
+            // 
+            // Encoding
+            // 
+            this.Encoding.FormattingEnabled = true;
+            this.Encoding.Items.AddRange(new object[] {
+            "UTF-8",
+            "ASCII",
+            "Hexadecimal"});
+            this.Encoding.Location = new System.Drawing.Point(533, 89);
+            this.Encoding.Name = "Encoding";
+            this.Encoding.Size = new System.Drawing.Size(157, 20);
+            this.Encoding.TabIndex = 12;
+            // 
             // SENDMESSAGE
             // 
             this.SENDMESSAGE.Location = new System.Drawing.Point(531, 120);
@@ -143,6 +166,8 @@
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(702, 358);
+            this.Controls.Add(this.Encoding);
+            this.Controls.Add(this.label1);
             this.Controls.Add(this.IPTEXT);
             this.Controls.Add(this.SEND);
             this.Controls.Add(this.SENDMESSAGE);
@@ -159,7 +184,7 @@
             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
             this.Text = "PLCDataReader";
             this.Load += new System.EventHandler(this.Form1_Load);
-            this.ResumeLayout(true);
+            this.ResumeLayout(false);
             this.PerformLayout();
 
         }
@@ -177,6 +202,8 @@
         private RichTextAutoBottom SENDMESSAGE;
         private System.Windows.Forms.Button SEND;
         private System.Windows.Forms.Label IPTEXT;
+        private System.Windows.Forms.Label label1;
+        private System.Windows.Forms.ComboBox Encoding;
     }
 }
 

+ 1 - 0
PLCDataReader/MainWindow.cs

@@ -25,6 +25,7 @@ namespace UAS_PLCDataReader
                 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                     IP.Text = IpEntry.AddressList[i].ToString();
             }
+            Encoding.Text = Encoding.Items[0].ToString();
         }
 
         private void OpenServer_Click(object sender, EventArgs e)