From fd31694420db3e5fc7ee5609c98a55e90d903b9b Mon Sep 17 00:00:00 2001 From: SoulStar Date: Wed, 13 Nov 2024 17:47:42 +0800 Subject: [PATCH] =?UTF-8?q?change=20-=20=E5=BC=80=E5=8F=91=E4=B8=AD=20-=20?= =?UTF-8?q?=E5=87=86=E5=A4=87=E5=BC=95=E5=85=A5TCPClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HighWayIot.TouchSocket.csproj | 2 +- HighWayIot.TouchSocket/TcpClientServer.cs | 43 +++++- HighWayIot.TouchSocket/TcpServer.cs | 8 +- RFIDSocket/Configuration.xml | 8 +- RFIDSocket/RFIDBinAudlt.Designer.cs | 125 +++++++++++++++++- RFIDSocket/RFIDBinAudlt.cs | 108 +++++++++++++-- RFIDSocket/RFIDBinAudlt.resx | 60 +++++++++ RFIDSocket/RFIDSocket.csproj | 2 +- 8 files changed, 326 insertions(+), 30 deletions(-) diff --git a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj index 7cd9982..5e946b4 100644 --- a/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj +++ b/HighWayIot.TouchSocket/HighWayIot.TouchSocket.csproj @@ -102,7 +102,7 @@ - + diff --git a/HighWayIot.TouchSocket/TcpClientServer.cs b/HighWayIot.TouchSocket/TcpClientServer.cs index b69e3f6..d2afb03 100644 --- a/HighWayIot.TouchSocket/TcpClientServer.cs +++ b/HighWayIot.TouchSocket/TcpClientServer.cs @@ -1,7 +1,9 @@ using HighWayIot.Log4net; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using TouchSocket.Core; @@ -19,7 +21,10 @@ namespace HighWayIot.TouchSocket TcpClient tcpClient = new TcpClient(); - + public bool ClientState + { + get => tcpClient.Online; + } /// /// 客户端连接 @@ -66,11 +71,43 @@ namespace HighWayIot.TouchSocket Result result = await tcpClient.TryConnectAsync();//或者可以调用TryConnectAsync if (result.IsSuccess()) { + tcpClient.Logger.Info("客户端成功连接"); + } + else + { + tcpClient.Logger.Info("客户端连接失败"); + } + return result.IsSuccess(); + } + public bool SendMessage(string message) + { + try + { + tcpClient.SendAsync(message); + return true; + } + catch (Exception e) + { + logHelper.Error("发送数据发生错误" + e); + return false; } + } - tcpClient.Logger.Info("客户端成功连接"); - return result.IsSuccess(); + public bool ClientClose() + { + try + { + tcpClient.CloseAsync(); + return true; + } + catch(Exception e) + { + logHelper.Error("关闭客户端发生错误" + e); + return false; + } } + + } } diff --git a/HighWayIot.TouchSocket/TcpServer.cs b/HighWayIot.TouchSocket/TcpServer.cs index 500b202..db24eea 100644 --- a/HighWayIot.TouchSocket/TcpServer.cs +++ b/HighWayIot.TouchSocket/TcpServer.cs @@ -66,7 +66,7 @@ namespace HighWayIot.TouchSocket //var mes = Encoding.ASCII.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意:数据长度是byteBlock.Len //logHelper.Info($"已从{client.IP}:{client.Port}接收到信息:{mes}"); - BufferMemory(BufferAnalysis.SplitByteArray(e.ByteBlock.Span.ToArray(), 0, e.ByteBlock.Length)); + BufferMemory(ServerBufferAnalysis.SplitByteArray(e.ByteBlock.Span.ToArray(), 0, e.ByteBlock.Length)); return EasyTask.CompletedTask; }; @@ -137,15 +137,15 @@ namespace HighWayIot.TouchSocket byte[] identify = new byte[2] { bytes[1], bytes[2] }; if (identify[0] == 0x30 && identify[1] == 0x31) { - BufferAnalysis.HeartbeatSocket(bytes); + ServerBufferAnalysis.HeartbeatSocket(bytes); } else if (identify[0] == 0x30 && identify[1] == 0x34) { - BufferAnalysis.RFIDStatusSocket(bytes); + ServerBufferAnalysis.RFIDStatusSocket(bytes); } else if (identify[0] == 0x35 && identify[1] == 0x33) { - BufferAnalysis.RFIDCodeSocket(bytes); + ServerBufferAnalysis.RFIDCodeSocket(bytes); } } diff --git a/RFIDSocket/Configuration.xml b/RFIDSocket/Configuration.xml index 43c05d9..cce621d 100644 --- a/RFIDSocket/Configuration.xml +++ b/RFIDSocket/Configuration.xml @@ -1,9 +1,7 @@  - 192.168.0.101 - 192.168.0.102 - 192.168.0.103 - 192.168.0.104 - 192.168.0.105 + 192.168.1.100 + 1500 + 192.168.0.7:1500 354 diff --git a/RFIDSocket/RFIDBinAudlt.Designer.cs b/RFIDSocket/RFIDBinAudlt.Designer.cs index 70fb875..07e9ff6 100644 --- a/RFIDSocket/RFIDBinAudlt.Designer.cs +++ b/RFIDSocket/RFIDBinAudlt.Designer.cs @@ -55,6 +55,13 @@ this.label2 = new System.Windows.Forms.Label(); this.NormalCountLabel = new System.Windows.Forms.Label(); this.ErrorCountLabel = new System.Windows.Forms.Label(); + this.IPText = new System.Windows.Forms.TextBox(); + this.PortText = new System.Windows.Forms.TextBox(); + this.SetAddress = new System.Windows.Forms.Button(); + this.label3 = new System.Windows.Forms.Label(); + this.MonitorOnOff = new System.Windows.Forms.Button(); + this.label4 = new System.Windows.Forms.Label(); + this.MonitorState = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.DataGroupBox.SuspendLayout(); this.SuspendLayout(); @@ -100,9 +107,10 @@ this.dataGridView.Dock = System.Windows.Forms.DockStyle.Fill; this.dataGridView.Location = new System.Drawing.Point(3, 17); this.dataGridView.Name = "dataGridView"; + this.dataGridView.ReadOnly = true; this.dataGridView.RowHeadersVisible = false; this.dataGridView.RowTemplate.Height = 23; - this.dataGridView.Size = new System.Drawing.Size(681, 884); + this.dataGridView.Size = new System.Drawing.Size(700, 884); this.dataGridView.TabIndex = 3; // // Column1 @@ -111,6 +119,7 @@ this.Column1.DataPropertyName = "Column1"; this.Column1.HeaderText = "Column1"; this.Column1.Name = "Column1"; + this.Column1.ReadOnly = true; // // Column2 // @@ -118,6 +127,7 @@ this.Column2.DataPropertyName = "Column2"; this.Column2.HeaderText = "Column2"; this.Column2.Name = "Column2"; + this.Column2.ReadOnly = true; // // Column3 // @@ -125,6 +135,7 @@ this.Column3.DataPropertyName = "Column3"; this.Column3.HeaderText = "Column3"; this.Column3.Name = "Column3"; + this.Column3.ReadOnly = true; // // Column4 // @@ -132,6 +143,7 @@ this.Column4.DataPropertyName = "Column4"; this.Column4.HeaderText = "Column4"; this.Column4.Name = "Column4"; + this.Column4.ReadOnly = true; // // Column5 // @@ -139,6 +151,7 @@ this.Column5.DataPropertyName = "Column5"; this.Column5.HeaderText = "Column5"; this.Column5.Name = "Column5"; + this.Column5.ReadOnly = true; // // Column6 // @@ -146,6 +159,7 @@ this.Column6.DataPropertyName = "Column6"; this.Column6.HeaderText = "Column6"; this.Column6.Name = "Column6"; + this.Column6.ReadOnly = true; // // Column7 // @@ -153,6 +167,7 @@ this.Column7.DataPropertyName = "Column7"; this.Column7.HeaderText = "Column7"; this.Column7.Name = "Column7"; + this.Column7.ReadOnly = true; // // Column8 // @@ -160,6 +175,7 @@ this.Column8.DataPropertyName = "Column8"; this.Column8.HeaderText = "Column8"; this.Column8.Name = "Column8"; + this.Column8.ReadOnly = true; // // Column9 // @@ -167,6 +183,7 @@ this.Column9.DataPropertyName = "Column9"; this.Column9.HeaderText = "Column9"; this.Column9.Name = "Column9"; + this.Column9.ReadOnly = true; // // Column10 // @@ -174,6 +191,7 @@ this.Column10.DataPropertyName = "Column10"; this.Column10.HeaderText = "Column10"; this.Column10.Name = "Column10"; + this.Column10.ReadOnly = true; // // Column11 // @@ -181,6 +199,7 @@ this.Column11.DataPropertyName = "Column11"; this.Column11.HeaderText = "Column11"; this.Column11.Name = "Column11"; + this.Column11.ReadOnly = true; // // Column12 // @@ -188,6 +207,7 @@ this.Column12.DataPropertyName = "Column12"; this.Column12.HeaderText = "Column12"; this.Column12.Name = "Column12"; + this.Column12.ReadOnly = true; // // Column13 // @@ -195,6 +215,7 @@ this.Column13.DataPropertyName = "Column13"; this.Column13.HeaderText = "Column13"; this.Column13.Name = "Column13"; + this.Column13.ReadOnly = true; // // Column14 // @@ -202,6 +223,7 @@ this.Column14.DataPropertyName = "Column14"; this.Column14.HeaderText = "Column14"; this.Column14.Name = "Column14"; + this.Column14.ReadOnly = true; // // Column15 // @@ -209,6 +231,7 @@ this.Column15.DataPropertyName = "Column15"; this.Column15.HeaderText = "Column15"; this.Column15.Name = "Column15"; + this.Column15.ReadOnly = true; // // Column16 // @@ -216,6 +239,7 @@ this.Column16.DataPropertyName = "Column16"; this.Column16.HeaderText = "Column16"; this.Column16.Name = "Column16"; + this.Column16.ReadOnly = true; // // Column17 // @@ -223,6 +247,7 @@ this.Column17.DataPropertyName = "Column17"; this.Column17.HeaderText = "Column17"; this.Column17.Name = "Column17"; + this.Column17.ReadOnly = true; // // Column18 // @@ -230,6 +255,7 @@ this.Column18.DataPropertyName = "Column18"; this.Column18.HeaderText = "Column18"; this.Column18.Name = "Column18"; + this.Column18.ReadOnly = true; // // Column19 // @@ -237,6 +263,7 @@ this.Column19.DataPropertyName = "Column19"; this.Column19.HeaderText = "Column19"; this.Column19.Name = "Column19"; + this.Column19.ReadOnly = true; // // Column20 // @@ -244,6 +271,7 @@ this.Column20.DataPropertyName = "Column20"; this.Column20.HeaderText = "Column20"; this.Column20.Name = "Column20"; + this.Column20.ReadOnly = true; // // DataGroupBox // @@ -254,7 +282,7 @@ this.DataGroupBox.Controls.Add(this.dataGridView); this.DataGroupBox.Location = new System.Drawing.Point(12, 58); this.DataGroupBox.Name = "DataGroupBox"; - this.DataGroupBox.Size = new System.Drawing.Size(687, 904); + this.DataGroupBox.Size = new System.Drawing.Size(706, 904); this.DataGroupBox.TabIndex = 4; this.DataGroupBox.TabStop = false; this.DataGroupBox.Text = "详情显示"; @@ -295,19 +323,103 @@ this.ErrorCountLabel.TabIndex = 8; this.ErrorCountLabel.Text = "N/A"; // + // IPText + // + this.IPText.Location = new System.Drawing.Point(459, 34); + this.IPText.Margin = new System.Windows.Forms.Padding(2); + this.IPText.MaxLength = 20; + this.IPText.Name = "IPText"; + this.IPText.Size = new System.Drawing.Size(98, 21); + this.IPText.TabIndex = 12; + this.IPText.Text = "127.0.0.1"; + // + // PortText + // + this.PortText.Location = new System.Drawing.Point(561, 34); + this.PortText.Margin = new System.Windows.Forms.Padding(2); + this.PortText.MaxLength = 6; + this.PortText.Name = "PortText"; + this.PortText.Size = new System.Drawing.Size(45, 21); + this.PortText.TabIndex = 9; + this.PortText.Text = "1234"; + // + // SetAddress + // + this.SetAddress.Location = new System.Drawing.Point(610, 10); + this.SetAddress.Margin = new System.Windows.Forms.Padding(2); + this.SetAddress.Name = "SetAddress"; + this.SetAddress.Size = new System.Drawing.Size(105, 45); + this.SetAddress.TabIndex = 11; + this.SetAddress.Text = "确认服务端地址"; + this.SetAddress.UseVisualStyleBackColor = true; + this.SetAddress.Click += new System.EventHandler(this.SetAddress_Click); + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(478, 12); + this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(101, 12); + this.label3.TabIndex = 10; + this.label3.Text = "服务端IP:端口号"; + // + // MonitorOnOff + // + this.MonitorOnOff.Location = new System.Drawing.Point(279, 10); + this.MonitorOnOff.Margin = new System.Windows.Forms.Padding(2); + this.MonitorOnOff.Name = "MonitorOnOff"; + this.MonitorOnOff.Size = new System.Drawing.Size(83, 45); + this.MonitorOnOff.TabIndex = 13; + this.MonitorOnOff.Text = "连接服务端"; + this.MonitorOnOff.UseVisualStyleBackColor = true; + this.MonitorOnOff.Click += new System.EventHandler(this.MonitorOnOff_Click); + // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(366, 26); + this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(65, 12); + this.label4.TabIndex = 14; + this.label4.Text = "连接状态:"; + // + // MonitorState + // + this.MonitorState.AutoSize = true; + this.MonitorState.BackColor = System.Drawing.Color.Transparent; + this.MonitorState.Location = new System.Drawing.Point(435, 26); + this.MonitorState.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0); + this.MonitorState.Name = "MonitorState"; + this.MonitorState.Size = new System.Drawing.Size(17, 12); + this.MonitorState.TabIndex = 15; + this.MonitorState.Text = "关"; + // // RFIDBinAudlt // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(711, 974); + this.ClientSize = new System.Drawing.Size(730, 974); + this.Controls.Add(this.label4); + this.Controls.Add(this.MonitorState); + this.Controls.Add(this.MonitorOnOff); + this.Controls.Add(this.IPText); + this.Controls.Add(this.PortText); + this.Controls.Add(this.SetAddress); + this.Controls.Add(this.label3); this.Controls.Add(this.ErrorCountLabel); this.Controls.Add(this.NormalCountLabel); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.DataGroupBox); this.Controls.Add(this.AudltButton); + this.MaximizeBox = false; this.Name = "RFIDBinAudlt"; this.Text = "格口盘点"; + this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.RFIDBinAudlt_MouseDown); + this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.RFIDBinAudlt_MouseMove); + this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.RFIDBinAudlt_MouseUp); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.DataGroupBox.ResumeLayout(false); this.ResumeLayout(false); @@ -343,5 +455,12 @@ private System.Windows.Forms.DataGridViewTextBoxColumn Column18; private System.Windows.Forms.DataGridViewTextBoxColumn Column19; private System.Windows.Forms.DataGridViewTextBoxColumn Column20; + private System.Windows.Forms.TextBox IPText; + private System.Windows.Forms.TextBox PortText; + private System.Windows.Forms.Button SetAddress; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Button MonitorOnOff; + private System.Windows.Forms.Label label4; + private System.Windows.Forms.Label MonitorState; } } \ No newline at end of file diff --git a/RFIDSocket/RFIDBinAudlt.cs b/RFIDSocket/RFIDBinAudlt.cs index ffcea00..0023231 100644 --- a/RFIDSocket/RFIDBinAudlt.cs +++ b/RFIDSocket/RFIDBinAudlt.cs @@ -9,24 +9,29 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml; +using TouchSocket.Sockets; namespace RFIDSocket { public partial class RFIDBinAudlt : Form { - TcpClientServer TCPClient = TcpClientServer.Instance; + private TcpClientServer TCPClient = TcpClientServer.Instance; - DataTable dt = new DataTable(); + private DataTable dt = new DataTable(); - string Path = System.Environment.CurrentDirectory; + private string Path = System.Environment.CurrentDirectory; - string[] ServiceIPs; - - int ServiceCount = -1; + private string[] ServiceIPs; - int NormalCount = 0; + private int ServiceCount = -1; - int ErrorCount = 0; + private int NormalCount = 0; + + private int ErrorCount = 0; + + string Port = string.Empty; + + string IP = string.Empty; public RFIDBinAudlt() { @@ -63,6 +68,10 @@ namespace RFIDSocket { MessageBox.Show("XML配置文件中ServiceIpConfig的值有问题,请重新配置!"); } + XmlNode ServerIp = rootNode.SelectSingleNode("ConnectIp"); // 服务器IP + XmlNode ServerPort = rootNode.SelectSingleNode("ConnectPort"); // 服务器端口 + IP = ServerIp.InnerText; + Port = ServerPort.InnerText; } /// @@ -72,11 +81,6 @@ namespace RFIDSocket /// private void AudltButton_Click(object sender, EventArgs e) { - //bool res = TCPClient.ClientConnect("127.0.0.1", "7789").GetAwaiter().GetResult(); - //if (res) - //{ - // MessageBox.Show("我操牛逼"); - //} NormalCount = 0; ErrorCount = 0; GridViewRefresh(GenerateRandomBoolArray(ServiceCount)); @@ -176,5 +180,83 @@ namespace RFIDSocket return boolArray; } + + /// + /// 连接开关 + /// + /// + /// + private void MonitorOnOff_Click(object sender, EventArgs e) + { + if (!TCPClient.ClientState) + { + if (TCPClient.ClientConnect(IP, Port).GetAwaiter().GetResult()) + { + MessageBox.Show("服务端连接成功!"); + } + else + { + MessageBox.Show("服务端连接失败!"); + } + } + else if (TCPClient.ClientState) + { + if (TCPClient.ClientClose()) + { + MessageBox.Show("服务端断开成功!"); + } + else + { + MessageBox.Show("服务端断开失败!"); + } + } + } + + /// + /// 设置服务端地址 + /// + /// + /// + private void SetAddress_Click(object sender, EventArgs e) + { + + } + + #region 鼠标拖拽 + + private bool isDragging = false; // 用于检测是否正在拖动 + + private Point lastLocation; // 保存上次鼠标位置 + + private void RFIDBinAudlt_MouseDown(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Left) + { + isDragging = true; + lastLocation = e.Location; // 记录鼠标按下时的位置 + } + } + + /// + /// 全局拖拽 + /// + /// + /// + private void RFIDBinAudlt_MouseMove(object sender, MouseEventArgs e) + { + if (isDragging) + { + this.Top = this.Top + (e.Y - lastLocation.Y); + this.Left = this.Left + (e.X - lastLocation.X); + } + } + + private void RFIDBinAudlt_MouseUp(object sender, MouseEventArgs e) + { + isDragging = false; + } + + #endregion + } } diff --git a/RFIDSocket/RFIDBinAudlt.resx b/RFIDSocket/RFIDBinAudlt.resx index 6d60700..67a9371 100644 --- a/RFIDSocket/RFIDBinAudlt.resx +++ b/RFIDSocket/RFIDBinAudlt.resx @@ -177,4 +177,64 @@ True + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + + + True + \ No newline at end of file diff --git a/RFIDSocket/RFIDSocket.csproj b/RFIDSocket/RFIDSocket.csproj index 0245957..6e53afa 100644 --- a/RFIDSocket/RFIDSocket.csproj +++ b/RFIDSocket/RFIDSocket.csproj @@ -144,7 +144,7 @@ - + Form