Browse Source

展示历史数据RichText

章政 7 years ago
parent
commit
9e9c7bbc80

+ 0 - 21
PLCDataReader/Form1.cs

@@ -1,21 +0,0 @@
-using System;
-using System.Windows.Forms;
-using HslCommunication.ModBus;
-using HslCommunication.Profinet;
-
-namespace UAS_PLCDataReader
-{
-    public partial class Form1 : Form
-    {
-
-        public Form1()
-        {
-            InitializeComponent();
-        }
-
-        private void Form1_Load(object sender, EventArgs e)
-        {
-
-        }
-    }
-}

+ 134 - 0
PLCDataReader/MainWindow.Designer.cs

@@ -0,0 +1,134 @@
+namespace UAS_PLCDataReader
+{
+    partial class MainWindow
+    {
+        /// <summary>
+        /// 必需的设计器变量。
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// 清理所有正在使用的资源。
+        /// </summary>
+        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows 窗体设计器生成的代码
+
+        /// <summary>
+        /// 设计器支持所需的方法 - 不要修改
+        /// 使用代码编辑器修改此方法的内容。
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.IP = new System.Windows.Forms.TextBox();
+            this.IP_label = new System.Windows.Forms.Label();
+            this.Port_label = new System.Windows.Forms.Label();
+            this.Port = new System.Windows.Forms.TextBox();
+            this.OpenServer = new System.Windows.Forms.Button();
+            this.CloseServer = new System.Windows.Forms.Button();
+            this.Result = new UAS_PLCDataReader.RichTextAutoBottom();
+            this.SuspendLayout();
+            // 
+            // IP
+            // 
+            this.IP.Enabled = false;
+            this.IP.Location = new System.Drawing.Point(111, 328);
+            this.IP.Name = "IP";
+            this.IP.Size = new System.Drawing.Size(133, 21);
+            this.IP.TabIndex = 0;
+            // 
+            // IP_label
+            // 
+            this.IP_label.AutoSize = true;
+            this.IP_label.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.IP_label.Location = new System.Drawing.Point(12, 328);
+            this.IP_label.Name = "IP_label";
+            this.IP_label.Size = new System.Drawing.Size(85, 21);
+            this.IP_label.TabIndex = 1;
+            this.IP_label.Text = "IPAddress";
+            // 
+            // Port_label
+            // 
+            this.Port_label.AutoSize = true;
+            this.Port_label.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
+            this.Port_label.Location = new System.Drawing.Point(255, 328);
+            this.Port_label.Name = "Port_label";
+            this.Port_label.Size = new System.Drawing.Size(42, 21);
+            this.Port_label.TabIndex = 3;
+            this.Port_label.Text = "Port";
+            // 
+            // Port
+            // 
+            this.Port.Location = new System.Drawing.Point(314, 328);
+            this.Port.Name = "Port";
+            this.Port.Size = new System.Drawing.Size(133, 21);
+            this.Port.TabIndex = 2;
+            // 
+            // OpenServer
+            // 
+            this.OpenServer.Location = new System.Drawing.Point(496, 328);
+            this.OpenServer.Name = "OpenServer";
+            this.OpenServer.Size = new System.Drawing.Size(65, 23);
+            this.OpenServer.TabIndex = 4;
+            this.OpenServer.Text = "Open";
+            this.OpenServer.UseVisualStyleBackColor = true;
+            this.OpenServer.Click += new System.EventHandler(this.OpenServer_Click);
+            // 
+            // CloseServer
+            // 
+            this.CloseServer.Location = new System.Drawing.Point(597, 328);
+            this.CloseServer.Name = "CloseServer";
+            this.CloseServer.Size = new System.Drawing.Size(65, 23);
+            this.CloseServer.TabIndex = 5;
+            this.CloseServer.Text = "Close";
+            this.CloseServer.UseVisualStyleBackColor = true;
+            this.CloseServer.Click += new System.EventHandler(this.CloseServer_Click);
+            // 
+            // Result
+            // 
+            this.Result.Location = new System.Drawing.Point(12, 12);
+            this.Result.Name = "Result";
+            this.Result.Size = new System.Drawing.Size(678, 310);
+            this.Result.TabIndex = 6;
+            this.Result.Text = "";
+            // 
+            // MainWindow
+            // 
+            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.Result);
+            this.Controls.Add(this.CloseServer);
+            this.Controls.Add(this.OpenServer);
+            this.Controls.Add(this.Port_label);
+            this.Controls.Add(this.Port);
+            this.Controls.Add(this.IP_label);
+            this.Controls.Add(this.IP);
+            this.Name = "MainWindow";
+            this.Text = "PLCDataReader";
+            this.Load += new System.EventHandler(this.Form1_Load);
+            this.ResumeLayout(false);
+            this.PerformLayout();
+
+        }
+
+        #endregion
+
+        private System.Windows.Forms.TextBox IP;
+        private System.Windows.Forms.Label IP_label;
+        private System.Windows.Forms.Label Port_label;
+        private System.Windows.Forms.TextBox Port;
+        private System.Windows.Forms.Button OpenServer;
+        private System.Windows.Forms.Button CloseServer;
+        private RichTextAutoBottom Result;
+    }
+}
+

+ 54 - 0
PLCDataReader/MainWindow.cs

@@ -0,0 +1,54 @@
+using System;
+using System.Windows.Forms;
+using System.Net;
+using System.Net.Sockets;
+
+namespace UAS_PLCDataReader
+{
+    public partial class MainWindow : Form
+    {
+
+        ModeBusTCPServer md = new ModeBusTCPServer();
+
+     
+
+        public MainWindow()
+        {
+            InitializeComponent();
+          
+        }
+
+        private void Form1_Load(object sender, EventArgs e)
+        {
+            CheckForIllegalCrossThreadCalls = false;
+            IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
+            for (int i = 0; i < IpEntry.AddressList.Length; i++)
+            {
+                if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
+                    IP.Text = IpEntry.AddressList[i].ToString();
+            }
+        }
+
+        private void OpenServer_Click(object sender, EventArgs e)
+        {
+            if (!md.IsOpen)
+            {
+                md.IP = IP.Text;
+                md.Port = Port.Text;
+                md.Open();
+                OpenServer.Enabled = false;
+                Port.Enabled = false;
+            }
+        }
+
+        private void CloseServer_Click(object sender, EventArgs e)
+        {
+            if (md.IsOpen)
+            {
+                OpenServer.Enabled = true;
+                Port.Enabled = true;
+                md.Close();
+            }
+        }
+    }
+}

+ 0 - 0
PLCDataReader/Form1.resx → PLCDataReader/MainWindow.resx


+ 8 - 60
PLCDataReader/ModeBusTCPServer.cs

@@ -1,30 +1,21 @@
 using System;
-using System.Collections.Generic;
-using System.Linq;
 using System.Net;
 using System.Net.Sockets;
 using System.Text;
 using System.Threading;
 using System.Windows.Forms;
 
-namespace UAS_MES.PublicMethod
+namespace UAS_PLCDataReader
 {
     class ModeBusTCPServer
     {
-        //M501指令OK,M502指令OK
-        byte[] M501_ON = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0xFF, 0x00, 0x98, 0xE5 };
-        byte[] M501_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF5, 0x00, 0x00, 0xD9, 0x15 };
-        byte[] M502_ON = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0xFF, 0x00, 0x68, 0xE5 };
-        byte[] M502_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF6, 0x00, 0x00, 0x29, 0x15 };
-        byte[] M503_READ = new byte[] { 0x00, 0x01, 0x11, 0xF7, 0x00, 0x01, 0x48, 0xD5 };
-        byte[] M503_OFF = new byte[] { 0x00, 0x05, 0x11, 0xF7, 0x00, 0x00, 0x78, 0xD5 };
         private bool isOpen = false;
 
         Thread threadWatch = null; //负责监听客户端的线程
         Socket socketWatch = null; //负责监听客户端的套接字
         Socket socConnection;
-        //等待机械臂完成动作
-        bool WaitFinish = true;
+
+        
 
         public bool IsOpen
         {
@@ -69,16 +60,18 @@ namespace UAS_MES.PublicMethod
 
         private string port;
 
+        RichTextAutoBottom richtext;
+
         public ModeBusTCPServer()
         {
 
         }
 
-
         public bool Open()
         {
             try
             {
+                richtext = Application.OpenForms["MainWindow"].Controls["Result"] as RichTextAutoBottom;
                 //定义一个套接字用于监听客户端发来的信息  包含3个参数(IP4寻址协议,流式连接,TCP协议)
                 socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                 //服务端发送信息 需要1个IP地址和端口号
@@ -118,19 +111,8 @@ namespace UAS_MES.PublicMethod
                 {
                     datashow[i] = data[i];
                 }
-                string str = System.Text.Encoding.ASCII.GetString(datashow);
-                Console.WriteLine(str);
-                string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
-                //Console.WriteLine(stringdata);
-                stringdata = stringdata.Replace("-", "");
-                //503口打开
-                if (stringdata.Substring(7, 1) == "1")
-                {
-                    socConnection.Send(M501_OFF);
-                    socConnection.Send(M502_OFF);
-                    socConnection.Send(M503_OFF);
-                    WaitFinish = false;
-                }
+                string str = Encoding.ASCII.GetString(datashow);
+                richtext.AppendText(str + "\n");
             }
         }
 
@@ -145,40 +127,6 @@ namespace UAS_MES.PublicMethod
         {
             switch (Code)
             {
-                case "M501_ON":
-                    socConnection.Send(M501_ON);
-                    WaitFinish = true;
-                    int LoopTime = 0;
-                    while (WaitFinish && LoopTime <= 40)
-                    {
-                        Thread.Sleep(100);
-                        LoopTime += LoopTime + 1;
-                        socConnection.Send(M503_READ);
-                    }
-                    break;
-                case "M501_OFF":
-                    socConnection.Send(M501_OFF);
-                    break;
-                case "M502_ON":
-                    socConnection.Send(M502_ON);
-                    WaitFinish = true;
-                    int LoopTime1 = 0;
-                    while (WaitFinish && LoopTime1 <= 40)
-                    {
-                        Thread.Sleep(100);
-                        LoopTime1 += LoopTime1 + 1;
-                        socConnection.Send(M503_READ);
-                    }
-                    break;
-                case "M502_OFF":
-                    socConnection.Send(M502_OFF);
-                    break;
-                case "M503_READ":
-                    socConnection.Send(M503_READ);
-                    break;
-                case "M503_OFF":
-                    socConnection.Send(M503_OFF);
-                    break;
                 default:
                     break;
             }

+ 1 - 1
PLCDataReader/Program.cs

@@ -15,7 +15,7 @@ namespace UAS_PLCDataReader
         {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
-            Application.Run(new Form1());
+            Application.Run(new MainWindow());
         }
     }
 }

+ 6 - 18
PLCDataReader/Form1.Designer.cs → PLCDataReader/RichTextAutoBottom.Designer.cs

@@ -1,13 +1,13 @@
 namespace UAS_PLCDataReader
 {
-    partial class Form1
+    partial class RichTextAutoBottom
     {
-        /// <summary>
+        /// <summary> 
         /// 必需的设计器变量。
         /// </summary>
         private System.ComponentModel.IContainer components = null;
 
-        /// <summary>
+        /// <summary> 
         /// 清理所有正在使用的资源。
         /// </summary>
         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
@@ -20,29 +20,17 @@
             base.Dispose(disposing);
         }
 
-        #region Windows 窗体设计器生成的代码
+        #region 组件设计器生成的代码
 
-        /// <summary>
+        /// <summary> 
         /// 设计器支持所需的方法 - 不要修改
         /// 使用代码编辑器修改此方法的内容。
         /// </summary>
         private void InitializeComponent()
         {
-            this.SuspendLayout();
-            // 
-            // Form1
-            // 
-            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
-            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(284, 261);
-            this.Name = "Form1";
-            this.Text = "Form1";
-            this.Load += new System.EventHandler(this.Form1_Load);
-            this.ResumeLayout(false);
-
+            components = new System.ComponentModel.Container();
         }
 
         #endregion
     }
 }
-

+ 20 - 0
PLCDataReader/RichTextAutoBottom.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Windows.Forms;
+
+namespace UAS_PLCDataReader
+{
+    public partial class RichTextAutoBottom : RichTextBox
+    {
+        public RichTextAutoBottom()
+        {
+            InitializeComponent();
+            TextChanged += RichTextBox_TextChange;
+        }
+
+        private void RichTextBox_TextChange(object sender, EventArgs e)
+        {
+            SelectionStart = Text.Length;
+            ScrollToCaret();
+        }
+    }
+}

+ 11 - 5
PLCDataReader/UAS_PLCDataReader.csproj

@@ -55,17 +55,23 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Form1.cs">
+    <Compile Include="MainWindow.cs">
       <SubType>Form</SubType>
     </Compile>
-    <Compile Include="Form1.Designer.cs">
-      <DependentUpon>Form1.cs</DependentUpon>
+    <Compile Include="MainWindow.Designer.cs">
+      <DependentUpon>MainWindow.cs</DependentUpon>
     </Compile>
     <Compile Include="ModeBusTCPServer.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
-    <EmbeddedResource Include="Form1.resx">
-      <DependentUpon>Form1.cs</DependentUpon>
+    <Compile Include="RichTextAutoBottom.cs">
+      <SubType>Component</SubType>
+    </Compile>
+    <Compile Include="RichTextAutoBottom.Designer.cs">
+      <DependentUpon>RichTextAutoBottom.cs</DependentUpon>
+    </Compile>
+    <EmbeddedResource Include="MainWindow.resx">
+      <DependentUpon>MainWindow.cs</DependentUpon>
     </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>