From 17fd918a079b1f3eeb67d2c52cc0d050e32df235 Mon Sep 17 00:00:00 2001 From: SoulStar Date: Tue, 13 Aug 2024 15:12:16 +0800 Subject: [PATCH] =?UTF-8?q?add=20-=20=E7=95=8C=E9=9D=A2=E5=AE=8C=E5=A4=87?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=BB=B6=E6=97=B6=E5=BC=80=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NDSD-Screwdriver/Program.cs | 3 + NDSD-Screwdriver/ScrewdriverTest.Designer.cs | 98 ++++++++++++++++++-- NDSD-Screwdriver/ScrewdriverTest.cs | 90 +++++++++++++++++- NDSD-TouchSocket/DOperate.cs | 48 +++++++++- NDSD-TouchSocket/SendAnalysis.cs | 70 ++++++++++++-- NDSD-TouchSocket/TcpServer.cs | 25 ++--- 6 files changed, 303 insertions(+), 31 deletions(-) diff --git a/NDSD-Screwdriver/Program.cs b/NDSD-Screwdriver/Program.cs index a89d5fe..451a90f 100644 --- a/NDSD-Screwdriver/Program.cs +++ b/NDSD-Screwdriver/Program.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows.Forms; @@ -8,6 +9,8 @@ namespace NDSD_Screwdriver { internal static class Program { + + /// /// 应用程序的主入口点。 /// diff --git a/NDSD-Screwdriver/ScrewdriverTest.Designer.cs b/NDSD-Screwdriver/ScrewdriverTest.Designer.cs index fc5b61c..1717a6e 100644 --- a/NDSD-Screwdriver/ScrewdriverTest.Designer.cs +++ b/NDSD-Screwdriver/ScrewdriverTest.Designer.cs @@ -33,11 +33,18 @@ this.DNoTextBox = new System.Windows.Forms.TextBox(); this.DOpenAll = new System.Windows.Forms.Button(); this.DCloseAll = new System.Windows.Forms.Button(); + this.DelayTimeTextBox = new System.Windows.Forms.TextBox(); + this.DelayButton = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.AllTimeButton = new System.Windows.Forms.Button(); + this.DelaySpanTextBox = new System.Windows.Forms.TextBox(); + this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // DOpenButton // - this.DOpenButton.Location = new System.Drawing.Point(12, 43); + this.DOpenButton.Location = new System.Drawing.Point(65, 48); this.DOpenButton.Name = "DOpenButton"; this.DOpenButton.Size = new System.Drawing.Size(100, 30); this.DOpenButton.TabIndex = 0; @@ -47,7 +54,7 @@ // // DCloseButton // - this.DCloseButton.Location = new System.Drawing.Point(12, 79); + this.DCloseButton.Location = new System.Drawing.Point(65, 84); this.DCloseButton.Name = "DCloseButton"; this.DCloseButton.Size = new System.Drawing.Size(100, 30); this.DCloseButton.TabIndex = 1; @@ -57,14 +64,15 @@ // // DNoTextBox // - this.DNoTextBox.Location = new System.Drawing.Point(12, 12); + this.DNoTextBox.Location = new System.Drawing.Point(65, 17); this.DNoTextBox.Name = "DNoTextBox"; this.DNoTextBox.Size = new System.Drawing.Size(100, 25); this.DNoTextBox.TabIndex = 2; + this.DNoTextBox.Text = "1"; // // DOpenAll // - this.DOpenAll.Location = new System.Drawing.Point(12, 115); + this.DOpenAll.Location = new System.Drawing.Point(65, 120); this.DOpenAll.Name = "DOpenAll"; this.DOpenAll.Size = new System.Drawing.Size(100, 30); this.DOpenAll.TabIndex = 3; @@ -74,7 +82,7 @@ // // DCloseAll // - this.DCloseAll.Location = new System.Drawing.Point(12, 151); + this.DCloseAll.Location = new System.Drawing.Point(65, 156); this.DCloseAll.Name = "DCloseAll"; this.DCloseAll.Size = new System.Drawing.Size(100, 30); this.DCloseAll.TabIndex = 4; @@ -82,11 +90,81 @@ this.DCloseAll.UseVisualStyleBackColor = true; this.DCloseAll.Click += new System.EventHandler(this.DCloseAll_Click); // + // DelayTimeTextBox + // + this.DelayTimeTextBox.Location = new System.Drawing.Point(240, 17); + this.DelayTimeTextBox.Name = "DelayTimeTextBox"; + this.DelayTimeTextBox.Size = new System.Drawing.Size(100, 25); + this.DelayTimeTextBox.TabIndex = 7; + this.DelayTimeTextBox.Text = "1"; + // + // DelayButton + // + this.DelayButton.Location = new System.Drawing.Point(240, 48); + this.DelayButton.Name = "DelayButton"; + this.DelayButton.Size = new System.Drawing.Size(100, 30); + this.DelayButton.TabIndex = 5; + this.DelayButton.Text = "计时开启"; + this.DelayButton.UseVisualStyleBackColor = true; + this.DelayButton.Click += new System.EventHandler(this.DelayButton_Click); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 20); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(47, 15); + this.label1.TabIndex = 9; + this.label1.Text = "DO No"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(171, 20); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(63, 15); + this.label3.TabIndex = 11; + this.label3.Text = "DO Time"; + // + // AllTimeButton + // + this.AllTimeButton.Location = new System.Drawing.Point(240, 84); + this.AllTimeButton.Name = "AllTimeButton"; + this.AllTimeButton.Size = new System.Drawing.Size(100, 30); + this.AllTimeButton.TabIndex = 12; + this.AllTimeButton.Text = "全部计时开"; + this.AllTimeButton.UseVisualStyleBackColor = true; + this.AllTimeButton.Click += new System.EventHandler(this.AllTimeButton_Click); + // + // DelaySpanTextBox + // + this.DelaySpanTextBox.Location = new System.Drawing.Point(240, 161); + this.DelaySpanTextBox.Name = "DelaySpanTextBox"; + this.DelaySpanTextBox.Size = new System.Drawing.Size(100, 25); + this.DelaySpanTextBox.TabIndex = 13; + this.DelaySpanTextBox.Text = "100"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(228, 143); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(129, 15); + this.label2.TabIndex = 14; + this.label2.Text = "全启间隔时间(ms)"; + // // ScrewdriverTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1296, 264); + this.ClientSize = new System.Drawing.Size(385, 202); + this.Controls.Add(this.label2); + this.Controls.Add(this.DelaySpanTextBox); + this.Controls.Add(this.AllTimeButton); + this.Controls.Add(this.label3); + this.Controls.Add(this.label1); + this.Controls.Add(this.DelayTimeTextBox); + this.Controls.Add(this.DelayButton); this.Controls.Add(this.DCloseAll); this.Controls.Add(this.DOpenAll); this.Controls.Add(this.DNoTextBox); @@ -94,6 +172,7 @@ this.Controls.Add(this.DOpenButton); this.Name = "ScrewdriverTest"; this.Text = "螺丝刀测试"; + this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ScrewdriverTest_FormClosed); this.ResumeLayout(false); this.PerformLayout(); @@ -106,5 +185,12 @@ private System.Windows.Forms.TextBox DNoTextBox; private System.Windows.Forms.Button DOpenAll; private System.Windows.Forms.Button DCloseAll; + private System.Windows.Forms.TextBox DelayTimeTextBox; + private System.Windows.Forms.Button DelayButton; + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Button AllTimeButton; + private System.Windows.Forms.TextBox DelaySpanTextBox; + private System.Windows.Forms.Label label2; } } \ No newline at end of file diff --git a/NDSD-Screwdriver/ScrewdriverTest.cs b/NDSD-Screwdriver/ScrewdriverTest.cs index ab33ab9..ced1825 100644 --- a/NDSD-Screwdriver/ScrewdriverTest.cs +++ b/NDSD-Screwdriver/ScrewdriverTest.cs @@ -6,8 +6,10 @@ using System.Data; using System.Drawing; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; namespace NDSD_Screwdriver { @@ -15,30 +17,112 @@ namespace NDSD_Screwdriver { DOperate DOperate; + TcpServer server = TcpServer.Instance; + public ScrewdriverTest() { InitializeComponent(); DOperate = new DOperate(); + if(!server.ServerOpen("192.168.0.101", "6001")) + { + MessageBox.Show("服务端打开失败!"); + } } private void DOpenButton_Click(object sender, EventArgs e) { - + int no; + int.TryParse(DNoTextBox.Text, out no); + if(no >= 1 && no <= 16) + { + DOperate.DOpen(no); + } + else + { + MessageBox.Show("请输入1-16的数字!"); + return; + } } private void DCloseButton_Click(object sender, EventArgs e) { - + int no; + int.TryParse(DNoTextBox.Text, out no); + if (no >= 1 && no <= 16) + { + DOperate.DClose(no); + } + else + { + MessageBox.Show("请输入1-16的数字!"); + return; + } } private void DCloseAll_Click(object sender, EventArgs e) { - + int span; + int.TryParse(DelaySpanTextBox.Text, out span); + if(span <= 0) + { + MessageBox.Show("属性值无效!"); + return; + } + for (int i = 0; i <= 16; i++) + { + DOperate.DClose(i); + Thread.Sleep(span); + } } private void DOpenAll_Click(object sender, EventArgs e) { + int span; + int.TryParse(DelaySpanTextBox.Text, out span); + if (span <= 0) + { + MessageBox.Show("属性值无效!"); + return; + } + for (int i = 0; i <= 16; i++) + { + DOperate.DOpen(i); + Thread.Sleep(span); + } + } + + private void ScrewdriverTest_FormClosed(object sender, FormClosedEventArgs e) + { + server.ServerStop(); + server.ServerDispose(); + } + + private void DelayButton_Click(object sender, EventArgs e) + { + int no; + int time; + int.TryParse(DNoTextBox.Text, out no); + int.TryParse(DelayTimeTextBox.Text, out time); + DOperate.DTimeOpen(no, time); + } + private void AllTimeButton_Click(object sender, EventArgs e) + { + int span; + int.TryParse(DelaySpanTextBox.Text, out span); + int time; + int.TryParse(DelayTimeTextBox.Text, out time); + if (span <= 0 || time <= 0) + { + MessageBox.Show("属性值无效!"); + return; + } + for (int i = 0; i <= 16; i++) + { + DOperate.DTimeOpen(i, time); + Thread.Sleep(span); + } } + } } diff --git a/NDSD-TouchSocket/DOperate.cs b/NDSD-TouchSocket/DOperate.cs index 901b123..e0f714c 100644 --- a/NDSD-TouchSocket/DOperate.cs +++ b/NDSD-TouchSocket/DOperate.cs @@ -17,15 +17,61 @@ namespace NDSD_TouchSocket sendAnalysis = new SendAnalysis(); } + /// + /// 枚举类控制接口开一段时间 + /// + /// Port枚举类D1-D16 + /// 延迟,1代表0.1s/100ms + public void DTimeOpen(Port port, int delay) + { + Server.SendMessage(sendAnalysis.DTimeOpenDataAnalysis((byte)port, delay)); + } + + /// + /// int控制接口开一段时间 + /// + /// int1-16 + /// 延迟,1代表0.1s/100ms + public void DTimeOpen(int port, int delay) + { + Server.SendMessage(sendAnalysis.DTimeOpenDataAnalysis((byte)(port + 4), delay)); + } + + /// + /// 枚举类控制接口开 + /// + /// Port枚举类D1-D16 public void DOpen(Port port) { Server.SendMessage(sendAnalysis.DOpenDataAnalysis((byte)port)); } - + + /// + /// 枚举类控制接口关 + /// + /// Port枚举类D1-D16 public void DClose(Port port) { Server.SendMessage(sendAnalysis.DCloseDataAnlysis((byte)port)); } + + /// + /// int控制接口开 + /// + /// int1-16 + public void DOpen(int port) + { + Server.SendMessage(sendAnalysis.DOpenDataAnalysis((byte)(port + 4))); + } + + /// + /// int控制接口关 + /// + /// int1-16 + public void DClose(int port) + { + Server.SendMessage(sendAnalysis.DCloseDataAnlysis((byte)(port + 4))); + } } public enum Port diff --git a/NDSD-TouchSocket/SendAnalysis.cs b/NDSD-TouchSocket/SendAnalysis.cs index e67b6bc..c61bda1 100644 --- a/NDSD-TouchSocket/SendAnalysis.cs +++ b/NDSD-TouchSocket/SendAnalysis.cs @@ -3,38 +3,90 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using TouchSocket.Core; namespace NDSD_TouchSocket { public class SendAnalysis { + /// + /// DO启动操作数据头 + /// private string DOperateHead = "01 05 00 "; - private string DOpenData = " FF 00"; - private string DCloseData = " 00 00"; - //开启 + /// + /// DO启动操作数据尾 + /// + private string DOpenData = " FF 00 "; + + /// + /// DO关闭操作数据尾 + /// + private string DCloseData = " 00 00 "; + + /// + /// DO计时启动操作数据头 + /// + private string DTimeOperateHead = " 01 07 00 "; + + /// + /// DO计时启动操作码 + /// + private string DTimeOpenData = " FF 00 "; + + public byte[] DTimeOpenDataAnalysis(byte port, int delay) + { + byte[] bytes = IntToDoubleByte(delay); + Array.Reverse(bytes); + string CrcData = DTimeOperateHead + port.ToString("X2") + DTimeOpenData + bytes.ToHexStrFromByte(); + return CrcCombine(CrcData); + } + + /// + /// DO开启 + /// + /// + /// public byte[] DOpenDataAnalysis(byte port) { string CrcData = DOperateHead + port.ToString("X2") + DOpenData; //结合 return CrcCombine(CrcData); } - //关闭 + /// + /// DO关闭 + /// + /// + /// public byte[] DCloseDataAnlysis(byte port) { string CrcData = DOperateHead + port.ToString("X2") + DCloseData; //结合 return CrcCombine(CrcData); } - //结合CRC + /// + /// 结合CRC16 + /// + /// + /// public byte[] CrcCombine(string CrcData) { byte[] body = CrcData.ToBytesFromHexString(); //byte的校验体 int crc = CRC16.CalculateCRC(body, body.Length); //int32CRC值 - byte highByte = (byte)(crc >> 8); // 获取高字节 - byte lowByte = (byte)(crc & 0xFF); // 获取低字节 - byte[] crcBytes = { lowByte, highByte }; - return body.Concat(crcBytes).ToArray(); + return body.Concat(IntToDoubleByte(crc)).ToArray(); + } + + /// + /// int转换为两位Byte数字 + /// + /// + /// + public byte[] IntToDoubleByte(int i) + { + byte highByte = (byte)(i >> 8); // 获取高字节 + byte lowByte = (byte)(i & 0xFF); // 获取低字节 + byte[] bytes = { lowByte, highByte }; + return bytes; } } diff --git a/NDSD-TouchSocket/TcpServer.cs b/NDSD-TouchSocket/TcpServer.cs index 5eb0d39..e805594 100644 --- a/NDSD-TouchSocket/TcpServer.cs +++ b/NDSD-TouchSocket/TcpServer.cs @@ -14,6 +14,7 @@ namespace NDSD_TouchSocket public static TcpServer Instance => lazy.Value; + TcpService service = new TcpService(); public bool ServerOpen(string ip, string port) @@ -47,7 +48,7 @@ namespace NDSD_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(BufferAnalysis.SplitByteArray(e.ByteBlock.Span.ToArray(), 0, e.ByteBlock.Length)); return EasyTask.CompletedTask; }; @@ -155,17 +156,17 @@ namespace NDSD_TouchSocket } } - private void BufferMemory(byte[] bytes) - { - if (bytes[4] == 0x00 && bytes[5] == 0xB9) - { - BufferAnalysis.JudgeSingle(false); - } - else if (bytes[4] == 0xFF && bytes[5] == 0xF9) - { - BufferAnalysis.JudgeSingle(true); - } - } + //private void BufferMemory(byte[] bytes) + //{ + // if (bytes[4] == 0x00 && bytes[5] == 0xB9) + // { + // BufferAnalysis.JudgeSingle(false); + // } + // else if (bytes[4] == 0xFF && bytes[5] == 0xF9) + // { + // BufferAnalysis.JudgeSingle(true); + // } + //} } }