add - 界面完备,添加延时开启

master
SoulStar 1 month ago
parent b54233c70d
commit 17fd918a07

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
@ -8,6 +9,8 @@ namespace NDSD_Screwdriver
{ {
internal static class Program internal static class Program
{ {
/// <summary> /// <summary>
/// 应用程序的主入口点。 /// 应用程序的主入口点。
/// </summary> /// </summary>

@ -33,11 +33,18 @@
this.DNoTextBox = new System.Windows.Forms.TextBox(); this.DNoTextBox = new System.Windows.Forms.TextBox();
this.DOpenAll = new System.Windows.Forms.Button(); this.DOpenAll = new System.Windows.Forms.Button();
this.DCloseAll = 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(); this.SuspendLayout();
// //
// DOpenButton // 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.Name = "DOpenButton";
this.DOpenButton.Size = new System.Drawing.Size(100, 30); this.DOpenButton.Size = new System.Drawing.Size(100, 30);
this.DOpenButton.TabIndex = 0; this.DOpenButton.TabIndex = 0;
@ -47,7 +54,7 @@
// //
// DCloseButton // 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.Name = "DCloseButton";
this.DCloseButton.Size = new System.Drawing.Size(100, 30); this.DCloseButton.Size = new System.Drawing.Size(100, 30);
this.DCloseButton.TabIndex = 1; this.DCloseButton.TabIndex = 1;
@ -57,14 +64,15 @@
// //
// DNoTextBox // 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.Name = "DNoTextBox";
this.DNoTextBox.Size = new System.Drawing.Size(100, 25); this.DNoTextBox.Size = new System.Drawing.Size(100, 25);
this.DNoTextBox.TabIndex = 2; this.DNoTextBox.TabIndex = 2;
this.DNoTextBox.Text = "1";
// //
// DOpenAll // 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.Name = "DOpenAll";
this.DOpenAll.Size = new System.Drawing.Size(100, 30); this.DOpenAll.Size = new System.Drawing.Size(100, 30);
this.DOpenAll.TabIndex = 3; this.DOpenAll.TabIndex = 3;
@ -74,7 +82,7 @@
// //
// DCloseAll // 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.Name = "DCloseAll";
this.DCloseAll.Size = new System.Drawing.Size(100, 30); this.DCloseAll.Size = new System.Drawing.Size(100, 30);
this.DCloseAll.TabIndex = 4; this.DCloseAll.TabIndex = 4;
@ -82,11 +90,81 @@
this.DCloseAll.UseVisualStyleBackColor = true; this.DCloseAll.UseVisualStyleBackColor = true;
this.DCloseAll.Click += new System.EventHandler(this.DCloseAll_Click); 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 // ScrewdriverTest
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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.DCloseAll);
this.Controls.Add(this.DOpenAll); this.Controls.Add(this.DOpenAll);
this.Controls.Add(this.DNoTextBox); this.Controls.Add(this.DNoTextBox);
@ -94,6 +172,7 @@
this.Controls.Add(this.DOpenButton); this.Controls.Add(this.DOpenButton);
this.Name = "ScrewdriverTest"; this.Name = "ScrewdriverTest";
this.Text = "螺丝刀测试"; this.Text = "螺丝刀测试";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.ScrewdriverTest_FormClosed);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -106,5 +185,12 @@
private System.Windows.Forms.TextBox DNoTextBox; private System.Windows.Forms.TextBox DNoTextBox;
private System.Windows.Forms.Button DOpenAll; private System.Windows.Forms.Button DOpenAll;
private System.Windows.Forms.Button DCloseAll; 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;
} }
} }

@ -6,8 +6,10 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
namespace NDSD_Screwdriver namespace NDSD_Screwdriver
{ {
@ -15,30 +17,112 @@ namespace NDSD_Screwdriver
{ {
DOperate DOperate; DOperate DOperate;
TcpServer server = TcpServer.Instance;
public ScrewdriverTest() public ScrewdriverTest()
{ {
InitializeComponent(); InitializeComponent();
DOperate = new DOperate(); DOperate = new DOperate();
if(!server.ServerOpen("192.168.0.101", "6001"))
{
MessageBox.Show("服务端打开失败!");
}
} }
private void DOpenButton_Click(object sender, EventArgs e) 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) 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) 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) 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);
}
}
} }
} }

@ -17,15 +17,61 @@ namespace NDSD_TouchSocket
sendAnalysis = new SendAnalysis(); sendAnalysis = new SendAnalysis();
} }
/// <summary>
/// 枚举类控制接口开一段时间
/// </summary>
/// <param name="port">Port枚举类D1-D16</param>
/// <param name="delay">延迟1代表0.1s/100ms</param>
public void DTimeOpen(Port port, int delay)
{
Server.SendMessage(sendAnalysis.DTimeOpenDataAnalysis((byte)port, delay));
}
/// <summary>
/// int控制接口开一段时间
/// </summary>
/// <param name="port">int1-16</param>
/// <param name="delay">延迟1代表0.1s/100ms</param>
public void DTimeOpen(int port, int delay)
{
Server.SendMessage(sendAnalysis.DTimeOpenDataAnalysis((byte)(port + 4), delay));
}
/// <summary>
/// 枚举类控制接口开
/// </summary>
/// <param name="port">Port枚举类D1-D16</param>
public void DOpen(Port port) public void DOpen(Port port)
{ {
Server.SendMessage(sendAnalysis.DOpenDataAnalysis((byte)port)); Server.SendMessage(sendAnalysis.DOpenDataAnalysis((byte)port));
} }
/// <summary>
/// 枚举类控制接口关
/// </summary>
/// <param name="port">Port枚举类D1-D16</param>
public void DClose(Port port) public void DClose(Port port)
{ {
Server.SendMessage(sendAnalysis.DCloseDataAnlysis((byte)port)); Server.SendMessage(sendAnalysis.DCloseDataAnlysis((byte)port));
} }
/// <summary>
/// int控制接口开
/// </summary>
/// <param name="port">int1-16</param>
public void DOpen(int port)
{
Server.SendMessage(sendAnalysis.DOpenDataAnalysis((byte)(port + 4)));
}
/// <summary>
/// int控制接口关
/// </summary>
/// <param name="port">int1-16</param>
public void DClose(int port)
{
Server.SendMessage(sendAnalysis.DCloseDataAnlysis((byte)(port + 4)));
}
} }
public enum Port public enum Port

@ -3,38 +3,90 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TouchSocket.Core;
namespace NDSD_TouchSocket namespace NDSD_TouchSocket
{ {
public class SendAnalysis public class SendAnalysis
{ {
/// <summary>
/// DO启动操作数据头
/// </summary>
private string DOperateHead = "01 05 00 "; private string DOperateHead = "01 05 00 ";
private string DOpenData = " FF 00";
private string DCloseData = " 00 00";
//开启 /// <summary>
/// DO启动操作数据尾
/// </summary>
private string DOpenData = " FF 00 ";
/// <summary>
/// DO关闭操作数据尾
/// </summary>
private string DCloseData = " 00 00 ";
/// <summary>
/// DO计时启动操作数据头
/// </summary>
private string DTimeOperateHead = " 01 07 00 ";
/// <summary>
/// DO计时启动操作码
/// </summary>
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);
}
/// <summary>
/// DO开启
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public byte[] DOpenDataAnalysis(byte port) public byte[] DOpenDataAnalysis(byte port)
{ {
string CrcData = DOperateHead + port.ToString("X2") + DOpenData; //结合 string CrcData = DOperateHead + port.ToString("X2") + DOpenData; //结合
return CrcCombine(CrcData); return CrcCombine(CrcData);
} }
//关闭 /// <summary>
/// DO关闭
/// </summary>
/// <param name="port"></param>
/// <returns></returns>
public byte[] DCloseDataAnlysis(byte port) public byte[] DCloseDataAnlysis(byte port)
{ {
string CrcData = DOperateHead + port.ToString("X2") + DCloseData; //结合 string CrcData = DOperateHead + port.ToString("X2") + DCloseData; //结合
return CrcCombine(CrcData); return CrcCombine(CrcData);
} }
//结合CRC /// <summary>
/// 结合CRC16
/// </summary>
/// <param name="CrcData"></param>
/// <returns></returns>
public byte[] CrcCombine(string CrcData) public byte[] CrcCombine(string CrcData)
{ {
byte[] body = CrcData.ToBytesFromHexString(); //byte的校验体 byte[] body = CrcData.ToBytesFromHexString(); //byte的校验体
int crc = CRC16.CalculateCRC(body, body.Length); //int32CRC值 int crc = CRC16.CalculateCRC(body, body.Length); //int32CRC值
byte highByte = (byte)(crc >> 8); // 获取高字节 return body.Concat(IntToDoubleByte(crc)).ToArray();
byte lowByte = (byte)(crc & 0xFF); // 获取低字节 }
byte[] crcBytes = { lowByte, highByte };
return body.Concat(crcBytes).ToArray(); /// <summary>
/// int转换为两位Byte数字
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public byte[] IntToDoubleByte(int i)
{
byte highByte = (byte)(i >> 8); // 获取高字节
byte lowByte = (byte)(i & 0xFF); // 获取低字节
byte[] bytes = { lowByte, highByte };
return bytes;
} }
} }

@ -14,6 +14,7 @@ namespace NDSD_TouchSocket
public static TcpServer Instance => lazy.Value; public static TcpServer Instance => lazy.Value;
TcpService service = new TcpService(); TcpService service = new TcpService();
public bool ServerOpen(string ip, string port) 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 //var mes = Encoding.ASCII.GetString(e.ByteBlock.Buffer, 0, e.ByteBlock.Len);//注意数据长度是byteBlock.Len
//logHelper.Info($"已从{client.IP}:{client.Port}接收到信息:{mes}"); //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; return EasyTask.CompletedTask;
}; };
@ -155,17 +156,17 @@ namespace NDSD_TouchSocket
} }
} }
private void BufferMemory(byte[] bytes) //private void BufferMemory(byte[] bytes)
{ //{
if (bytes[4] == 0x00 && bytes[5] == 0xB9) // if (bytes[4] == 0x00 && bytes[5] == 0xB9)
{ // {
BufferAnalysis.JudgeSingle(false); // BufferAnalysis.JudgeSingle(false);
} // }
else if (bytes[4] == 0xFF && bytes[5] == 0xF9) // else if (bytes[4] == 0xFF && bytes[5] == 0xF9)
{ // {
BufferAnalysis.JudgeSingle(true); // BufferAnalysis.JudgeSingle(true);
} // }
} //}
} }
} }

Loading…
Cancel
Save