添加项目文件。

This commit is contained in:
MoYue 2023-07-24 16:07:03 +08:00
parent 1bdda3b01e
commit c1d8e2c6f9
17 changed files with 3355 additions and 0 deletions

25
YTDX202303TCP.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33403.182
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YTDX202303TCP", "YTDX202303TCP\YTDX202303TCP.csproj", "{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0A92B70F-1BDF-484D-9775-72426781D90C}
EndGlobalSection
EndGlobal

6
YTDX202303TCP/App.config Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

197
YTDX202303TCP/Client.cs Normal file
View File

@ -0,0 +1,197 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static YTDX202303TCP.Sockets;
namespace YTDX202303TCP
{
public class Client : SocketObject
{
public PushSockets pushSockets;
bool IsClose = false;
/// <summary>
/// 当前管理对象
/// </summary>
Sockets sk;
/// <summary>
/// 客户端
/// </summary>
TcpClient client;
/// <summary>
/// 当前连接服务端地址
/// </summary>
IPAddress Ipaddress;
/// <summary>
/// 当前连接服务端端口号
/// </summary>
int Port;
/// <summary>
/// 服务端IP+端口
/// </summary>
IPEndPoint ip;
/// <summary>
/// 发送与接收使用的流
/// </summary>
NetworkStream nStream;
/// <summary>
/// 初始化Socket
/// </summary>
/// <param name="ipaddress"></param>
/// <param name="port"></param>
public override void InitSocket(IPAddress ipaddress, int port)
{
Ipaddress = ipaddress;
Port = port;
ip = new IPEndPoint(Ipaddress, Port);
client = new TcpClient();
}
/// <summary>
/// 初始化Socket
/// </summary>
/// <param name="ipaddress">ipd地址</param>
/// <param name="port">端口</param>
public override void InitSocket(string ipaddress, int port)
{
Ipaddress = IPAddress.Parse(ipaddress);
Port = port;
ip = new IPEndPoint(Ipaddress, Port);
client = new TcpClient();
}
/// <summary>
/// 重写Start方法,其实就是连接服务端
/// </summary>
public override void Start()
{
Connect();
}
/// <summary>
/// 连接
/// </summary>
public bool Connect()
{
try
{
client.Connect(ip);
}
catch (Exception)
{
return false;
}
nStream = new NetworkStream(client.Client, true);
sk = new Sockets(ip, client, nStream);
sk.nStream.BeginRead(sk.RecBuffer, 0, sk.RecBuffer.Length, new AsyncCallback(EndReader), sk);
return true;
}
/// <summary>
/// 读取
/// </summary>
private void EndReader(IAsyncResult ir)
{
Sockets s = ir.AsyncState as Sockets;
try
{
if (s != null)
{
if (IsClose && client == null)
{
sk.nStream.Close();
sk.nStream.Dispose();
return;
}
s.Offset = s.nStream.EndRead(ir);
if (pushSockets != null)
pushSockets.Invoke(s);//推送至UI
sk.nStream.BeginRead(sk.RecBuffer, 0, sk.RecBuffer.Length, new AsyncCallback(EndReader), sk);
}
}
catch (Exception skex)
{
Sockets sks = s;
sks.ex = skex;
sks.ClientDispose = true;
if (pushSockets != null)
pushSockets.Invoke(sks);//推送至UI
}
}
/// <summary>
/// 停止
/// </summary>
public override void Stop()
{
Sockets sks = new Sockets();
try
{
if (client != null)
{
client.Client.Shutdown(SocketShutdown.Both);
Thread.Sleep(10);
client.Close();
IsClose = true;
client = null;
}
else
{
sks.ex = new Exception("客户端没有初始化.!");
}
if (pushSockets != null)
pushSockets.Invoke(sks);//推送至UI
}
catch (Exception ex)
{
sks.ex = ex;
}
}
/// <summary>
/// 发送消息
/// </summary>
public void SendData(byte[] SendData)
{
try
{
if (client == null || !client.Connected)
{
Sockets sks = new Sockets();
sks.ex = new Exception("客户端无连接..");
sks.ClientDispose = true;
if (pushSockets != null)
pushSockets.Invoke(sks);//推送至UI
}
if (client.Connected) //如果连接则发送
{
if (nStream == null)
{
nStream = client.GetStream();
}
nStream.Write(SendData, 0, SendData.Length);
}
}
catch (Exception skex)
{
Sockets sks = new Sockets();
sks.ex = skex;
sks.ClientDispose = true;
if (pushSockets != null)
pushSockets.Invoke(sks);//推送至UI
}
}
public bool Is_Connected()
{
return client.Connected;
}
public bool IsOnline()
{
if (client == null)
return false;
return !((client.Client.Poll(1000, SelectMode.SelectRead) && (client.Client.Available == 0)) || !client.Client.Connected);
}
}
}

34
YTDX202303TCP/Data.cs Normal file
View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YTDX202303TCP
{
public class Data
{
public float AIN0 { get; set; }
public float AIN1 { get; set; }
public float AIN2 { get; set; }
public float AIN3 { get; set; }
public float AIN4 { get; set; }
public float AIN5 { get; set; }
public float AIN6 { get; set; }
public float AIN7 { get; set; }
public float DI1 { get; set; }
public float DI2 { get; set; }
public float DI3 { get; set; }
public float DI4 { get; set; }
public string DO1 { get; set; } = "关";
public string DO2 { get; set; } = "关";
public string DO3 { get; set; } = "关";
public string DO4 { get; set; } = "关";
}
}

637
YTDX202303TCP/Form1.Designer.cs generated Normal file
View File

@ -0,0 +1,637 @@
namespace YTDX202303TCP
{
partial class Form1
{
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label37 = new System.Windows.Forms.Label();
this.label36 = new System.Windows.Forms.Label();
this.label35 = new System.Windows.Forms.Label();
this.label34 = new System.Windows.Forms.Label();
this.label33 = new System.Windows.Forms.Label();
this.label32 = new System.Windows.Forms.Label();
this.label31 = new System.Windows.Forms.Label();
this.label30 = new System.Windows.Forms.Label();
this.label21 = new System.Windows.Forms.Label();
this.label20 = new System.Windows.Forms.Label();
this.label19 = new System.Windows.Forms.Label();
this.label18 = new System.Windows.Forms.Label();
this.label17 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.button11 = new System.Windows.Forms.Button();
this.button10 = new System.Windows.Forms.Button();
this.button9 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.label22 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.label22);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(510, 118);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "TCPClient";
//
// button1
//
this.button1.Location = new System.Drawing.Point(410, 31);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "连接";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(281, 31);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 23);
this.textBox2.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(212, 34);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(63, 14);
this.label2.TabIndex = 2;
this.label2.Text = "端口号:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(78, 31);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(116, 23);
this.textBox1.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(9, 34);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(63, 14);
this.label1.TabIndex = 0;
this.label1.Text = "目标IP";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label37);
this.groupBox2.Controls.Add(this.label36);
this.groupBox2.Controls.Add(this.label35);
this.groupBox2.Controls.Add(this.label34);
this.groupBox2.Controls.Add(this.label33);
this.groupBox2.Controls.Add(this.label32);
this.groupBox2.Controls.Add(this.label31);
this.groupBox2.Controls.Add(this.label30);
this.groupBox2.Controls.Add(this.label21);
this.groupBox2.Controls.Add(this.label20);
this.groupBox2.Controls.Add(this.label19);
this.groupBox2.Controls.Add(this.label18);
this.groupBox2.Controls.Add(this.label17);
this.groupBox2.Controls.Add(this.label16);
this.groupBox2.Controls.Add(this.label15);
this.groupBox2.Controls.Add(this.label14);
this.groupBox2.Controls.Add(this.label13);
this.groupBox2.Controls.Add(this.label12);
this.groupBox2.Controls.Add(this.label11);
this.groupBox2.Controls.Add(this.label10);
this.groupBox2.Controls.Add(this.label9);
this.groupBox2.Controls.Add(this.label8);
this.groupBox2.Controls.Add(this.label7);
this.groupBox2.Controls.Add(this.label6);
this.groupBox2.Controls.Add(this.label5);
this.groupBox2.Controls.Add(this.label4);
this.groupBox2.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox2.Location = new System.Drawing.Point(12, 136);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(275, 439);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "RTU IO 状态";
//
// label37
//
this.label37.AutoSize = true;
this.label37.Location = new System.Drawing.Point(154, 413);
this.label37.Name = "label37";
this.label37.Size = new System.Drawing.Size(21, 14);
this.label37.TabIndex = 32;
this.label37.Text = "关";
//
// label36
//
this.label36.AutoSize = true;
this.label36.Location = new System.Drawing.Point(154, 372);
this.label36.Name = "label36";
this.label36.Size = new System.Drawing.Size(21, 14);
this.label36.TabIndex = 31;
this.label36.Text = "关";
//
// label35
//
this.label35.AutoSize = true;
this.label35.Location = new System.Drawing.Point(154, 332);
this.label35.Name = "label35";
this.label35.Size = new System.Drawing.Size(21, 14);
this.label35.TabIndex = 30;
this.label35.Text = "关";
//
// label34
//
this.label34.AutoSize = true;
this.label34.Location = new System.Drawing.Point(154, 298);
this.label34.Name = "label34";
this.label34.Size = new System.Drawing.Size(21, 14);
this.label34.TabIndex = 29;
this.label34.Text = "关";
//
// label33
//
this.label33.AutoSize = true;
this.label33.Location = new System.Drawing.Point(9, 372);
this.label33.Name = "label33";
this.label33.Size = new System.Drawing.Size(35, 14);
this.label33.TabIndex = 28;
this.label33.Text = "复位";
//
// label32
//
this.label32.AutoSize = true;
this.label32.Location = new System.Drawing.Point(9, 332);
this.label32.Name = "label32";
this.label32.Size = new System.Drawing.Size(77, 14);
this.label32.TabIndex = 27;
this.label32.Text = "继电器控制";
//
// label31
//
this.label31.AutoSize = true;
this.label31.Location = new System.Drawing.Point(8, 413);
this.label31.Name = "label31";
this.label31.Size = new System.Drawing.Size(28, 14);
this.label31.TabIndex = 27;
this.label31.Text = "DO4";
//
// label30
//
this.label30.AutoSize = true;
this.label30.Location = new System.Drawing.Point(9, 298);
this.label30.Name = "label30";
this.label30.Size = new System.Drawing.Size(91, 14);
this.label30.TabIndex = 26;
this.label30.Text = "电子开关控制";
//
// label21
//
this.label21.AutoSize = true;
this.label21.Location = new System.Drawing.Point(154, 260);
this.label21.Name = "label21";
this.label21.Size = new System.Drawing.Size(14, 14);
this.label21.TabIndex = 17;
this.label21.Text = "0";
//
// label20
//
this.label20.AutoSize = true;
this.label20.Location = new System.Drawing.Point(154, 230);
this.label20.Name = "label20";
this.label20.Size = new System.Drawing.Size(14, 14);
this.label20.TabIndex = 16;
this.label20.Text = "0";
//
// label19
//
this.label19.AutoSize = true;
this.label19.Location = new System.Drawing.Point(154, 201);
this.label19.Name = "label19";
this.label19.Size = new System.Drawing.Size(14, 14);
this.label19.TabIndex = 15;
this.label19.Text = "0";
//
// label18
//
this.label18.AutoSize = true;
this.label18.Location = new System.Drawing.Point(154, 172);
this.label18.Name = "label18";
this.label18.Size = new System.Drawing.Size(14, 14);
this.label18.TabIndex = 14;
this.label18.Text = "0";
//
// label17
//
this.label17.AutoSize = true;
this.label17.Location = new System.Drawing.Point(154, 144);
this.label17.Name = "label17";
this.label17.Size = new System.Drawing.Size(14, 14);
this.label17.TabIndex = 13;
this.label17.Text = "0";
//
// label16
//
this.label16.AutoSize = true;
this.label16.Location = new System.Drawing.Point(154, 115);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(14, 14);
this.label16.TabIndex = 12;
this.label16.Text = "0";
//
// label15
//
this.label15.AutoSize = true;
this.label15.Location = new System.Drawing.Point(154, 87);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(14, 14);
this.label15.TabIndex = 11;
this.label15.Text = "0";
//
// label14
//
this.label14.AutoSize = true;
this.label14.Location = new System.Drawing.Point(9, 260);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(35, 14);
this.label14.TabIndex = 10;
this.label14.Text = "AIN7";
//
// label13
//
this.label13.AutoSize = true;
this.label13.Location = new System.Drawing.Point(9, 230);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(35, 14);
this.label13.TabIndex = 9;
this.label13.Text = "AIN6";
//
// label12
//
this.label12.AutoSize = true;
this.label12.Location = new System.Drawing.Point(9, 201);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(35, 14);
this.label12.TabIndex = 8;
this.label12.Text = "湿度";
//
// label11
//
this.label11.AutoSize = true;
this.label11.Location = new System.Drawing.Point(9, 172);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(35, 14);
this.label11.TabIndex = 7;
this.label11.Text = "温度";
//
// label10
//
this.label10.AutoSize = true;
this.label10.Location = new System.Drawing.Point(9, 144);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(77, 14);
this.label10.TabIndex = 6;
this.label10.Text = "短路故障位";
//
// label9
//
this.label9.AutoSize = true;
this.label9.Location = new System.Drawing.Point(9, 115);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(77, 14);
this.label9.TabIndex = 5;
this.label9.Text = "过流故障位";
//
// label8
//
this.label8.AutoSize = true;
this.label8.Location = new System.Drawing.Point(9, 87);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(35, 14);
this.label8.TabIndex = 4;
this.label8.Text = "电流";
//
// label7
//
this.label7.AutoSize = true;
this.label7.Location = new System.Drawing.Point(154, 58);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(14, 14);
this.label7.TabIndex = 3;
this.label7.Text = "0";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(9, 58);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(35, 14);
this.label6.TabIndex = 2;
this.label6.Text = "电压";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(157, 28);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(35, 14);
this.label5.TabIndex = 1;
this.label5.Text = "数据";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 28);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(35, 14);
this.label4.TabIndex = 0;
this.label4.Text = "名称";
//
// groupBox3
//
this.groupBox3.Controls.Add(this.button11);
this.groupBox3.Controls.Add(this.button10);
this.groupBox3.Controls.Add(this.button9);
this.groupBox3.Controls.Add(this.button8);
this.groupBox3.Controls.Add(this.button7);
this.groupBox3.Controls.Add(this.button6);
this.groupBox3.Controls.Add(this.button5);
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Controls.Add(this.button3);
this.groupBox3.Controls.Add(this.button2);
this.groupBox3.Controls.Add(this.label3);
this.groupBox3.Font = new System.Drawing.Font("宋体", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox3.Location = new System.Drawing.Point(310, 136);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(212, 439);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "RTU DO 控制";
//
// button11
//
this.button11.Location = new System.Drawing.Point(18, 389);
this.button11.Name = "button11";
this.button11.Size = new System.Drawing.Size(75, 23);
this.button11.TabIndex = 10;
this.button11.Text = "自动读取";
this.button11.UseVisualStyleBackColor = true;
this.button11.Click += new System.EventHandler(this.button11_Click);
//
// button10
//
this.button10.Location = new System.Drawing.Point(115, 304);
this.button10.Name = "button10";
this.button10.Size = new System.Drawing.Size(88, 23);
this.button10.TabIndex = 9;
this.button10.Text = "继电器4关";
this.button10.UseVisualStyleBackColor = true;
this.button10.Click += new System.EventHandler(this.button10_Click);
//
// button9
//
this.button9.Location = new System.Drawing.Point(18, 304);
this.button9.Name = "button9";
this.button9.Size = new System.Drawing.Size(88, 23);
this.button9.TabIndex = 8;
this.button9.Text = "继电器4开";
this.button9.UseVisualStyleBackColor = true;
this.button9.Click += new System.EventHandler(this.button9_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(115, 230);
this.button8.Name = "button8";
this.button8.Size = new System.Drawing.Size(88, 23);
this.button8.TabIndex = 7;
this.button8.Text = "继电器3关";
this.button8.UseVisualStyleBackColor = true;
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(18, 230);
this.button7.Name = "button7";
this.button7.Size = new System.Drawing.Size(88, 23);
this.button7.TabIndex = 6;
this.button7.Text = "继电器3开";
this.button7.UseVisualStyleBackColor = true;
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(115, 163);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(88, 23);
this.button6.TabIndex = 5;
this.button6.Text = "继电器2关";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(18, 163);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(88, 23);
this.button5.TabIndex = 4;
this.button5.Text = "继电器2开";
this.button5.UseVisualStyleBackColor = true;
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(115, 87);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 23);
this.button4.TabIndex = 3;
this.button4.Text = "继电器1关";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(18, 87);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 23);
this.button3.TabIndex = 2;
this.button3.Text = "继电器1开";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(117, 28);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "读取";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(15, 33);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(91, 14);
this.label3.TabIndex = 0;
this.label3.Text = "读取系统状态";
//
// label22
//
this.label22.AutoSize = true;
this.label22.Location = new System.Drawing.Point(9, 79);
this.label22.Name = "label22";
this.label22.Size = new System.Drawing.Size(63, 14);
this.label22.TabIndex = 0;
this.label22.Text = "子站地址";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(78, 76);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(116, 23);
this.textBox3.TabIndex = 1;
this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(534, 608);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "YTDA202303TCP";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label21;
private System.Windows.Forms.Label label20;
private System.Windows.Forms.Label label19;
private System.Windows.Forms.Label label18;
private System.Windows.Forms.Label label17;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label37;
private System.Windows.Forms.Label label36;
private System.Windows.Forms.Label label35;
private System.Windows.Forms.Label label34;
private System.Windows.Forms.Label label33;
private System.Windows.Forms.Label label32;
private System.Windows.Forms.Label label31;
private System.Windows.Forms.Label label30;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button10;
private System.Windows.Forms.Button button9;
private System.Windows.Forms.Button button8;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button11;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label22;
}
}

492
YTDX202303TCP/Form1.cs Normal file
View File

@ -0,0 +1,492 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;
namespace YTDX202303TCP
{
public partial class Form1 : Form
{
private string main_ip = "192.168.0.65"; //IP
private int main_port = 502; //端口
private Client _client = new Client(); //主板客户端
//单线程
private System.Threading.Timer _flash_page;
private Data data = new Data();
static bool isSending = false;//数据是否定时发送
private int station_id = 100;
public Form1()
{
InitializeComponent();
textBox1.Text = main_ip;
textBox2.Text = main_port.ToString();
textBox3.Text = station_id.ToString();
//刷新界面的线程
_flash_page = new System.Threading.Timer(new System.Threading.TimerCallback(WritetoPage), null, 2000, 1000);
}
/// <summary>
/// 数据写入界面
/// </summary>
/// <param name="sender"></param>
private void WritetoPage(object sender)
{
Invoke((EventHandler)(delegate
{
label7.Text = data.AIN0.ToString("0.00") + " V";
label15.Text = data.AIN1.ToString("0.00") + " A";
label16.Text = data.AIN2.ToString("0.00") + " V";
label17.Text = data.AIN3.ToString("0.00") + " V";
label18.Text = data.AIN4.ToString("0.00") + " ℃";
label19.Text = data.AIN5.ToString("0.00") + " %";
label20.Text = data.AIN6.ToString("0.00") + " V";
label21.Text = data.AIN7.ToString("0.00") + " V";
label34.Text = data.DO1;
label35.Text = data.DO2;
label36.Text = data.DO3;
label37.Text = data.DO4;
}));
}
/// <summary>
/// 连接
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "连接")
{
//连接Socket
_client.InitSocket(textBox1.Text, Convert.ToInt32(textBox2.Text));
bool isConnected = _client.Connect();
if (isConnected)
{
_client.pushSockets = ReceiveMess;//注册推送器
button1.Text = "断开";
textBox1.Enabled = false;
textBox2.Enabled = false;
}
}
else if (button1.Text == "断开")
{
if (button11.Text == "停止")
{
MessageBox.Show("请先关闭自动读取功能!");
}
else
{
_client.Stop();
button1.Text = "连接";
textBox1.Enabled = true;
textBox2.Enabled = true;
}
}
}
/// <summary>
/// 客户端数据接收
/// </summary>
/// <param name="sks"></param>
private void ReceiveMess(Sockets sks)
{
if (sks.ex != null)
{
if (sks.ClientDispose == true)
{
//由于未知原因引发异常.导致客户端下线. 比如网络故障.或服务器断开连接.
//wirte_textbox(string.Format("客户端下线.!异常消息:{0}\r\n", sks.ex));
}
else
{
//SetClientState(string.Format("异常消息:{0}\r\n", sks.ex));
}
//timerConnect.Enabled = true;
}
else if (sks.Offset == 0)
{
//客户端主动下线
//wirte_textbox("客户端下线!");
}
else
{
byte[] buffer = new byte[sks.Offset];
Array.Copy(sks.RecBuffer, buffer, sks.Offset);
string str = Encoding.UTF8.GetString(buffer);
try
{
ParsingData(new List<byte>(buffer));
}
catch (Exception)
{
return;
}
}
}
/// <summary>
/// 数据解析
/// </summary>
/// <param name="byteList"></param>
private void ParsingData(List<byte> byteList)
{
string rec_16 = null;
for (int i = 0; i < byteList.Count; i++)
{
rec_16 += byteList[i].ToString("X2"); //16进制显示
}
//string _message = tools.HexStringToString(rec_16, Encoding.Default).Replace("\r\n", "");
//校验当前List是否为空或者长度
if (byteList == null || byteList.Count == 0)
return;
//获取当前系统时间
DateTime time_DataBase = DateTime.Now;
//帧头
byte[] _header = new byte[] { (byte)station_id };
//功能码
byte _fun_code = 0;
//操作功能
byte _operation = 0;
//传感器类型
byte _kind = 0;
//校验帧头
if (byteList[0] != _header[0])
{
return;
}
//读取状态显示
if (rec_16.StartsWith(station_id.ToString("x2") + "0330"))
{
data.AIN0 = (float)tools.bytetofloat(byteList, 3) * 118F; //电压
data.AIN1 = (float)tools.bytetofloat(byteList, 7) / 0.75F; //电流
data.AIN2 = (float)tools.bytetofloat(byteList, 11);
data.AIN3 = (float)tools.bytetofloat(byteList, 15);
data.AIN4 = (float)tools.bytetofloat(byteList, 19) * 100F; //温度
data.AIN5 = (float)tools.bytetofloat(byteList, 23) * 100F / 3F; //湿度
data.AIN6 = (float)tools.bytetofloat(byteList, 27);
data.AIN7 = (float)tools.bytetofloat(byteList, 31);
if (byteList[44] == 1)
{
data.DO1 = "开";
}
else
{
data.DO1 = "关";
}
if (byteList[46] == 1)
{
data.DO2 = "开";
}
else
{
data.DO2 = "关";
}
if (byteList[48] == 1)
{
data.DO3 = "开";
}
else
{
data.DO3 = "关";
}
if (byteList[50] == 1)
{
data.DO4 = "开";
}
else
{
data.DO4 = "关";
}
}
}
/// <summary>
/// 继电器1开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Control(20, 01);
}
/// <summary>
/// 继电器1关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
Control(20, 00);
}
/// <summary>
/// 继电器2开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Control(21, 01);
//Thread.Sleep(500);
//button3_Click(null, null);
}
/// <summary>
/// 继电器2关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Control(21, 00);
}
/// <summary>
/// 继电器3开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
Control(22, 01);
}
/// <summary>
/// 继电器3关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
Control(22, 00);
}
/// <summary>
/// 继电器4开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_Click(object sender, EventArgs e)
{
Control(23, 01);
}
/// <summary>
/// 继电器4关
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
Control(23, 00);
}
/// <summary>
/// 读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24, 76, 53 };
byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byte_read, byte_read.Length);
List<byte> data = byte_read.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
//创建读取线程
private System.Threading.Timer timer_Read;
/// <summary>
/// 定时读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
if (button11.Text == "自动读取")
{
int time = 1;//间隔
isSending = true;
//byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
//byte[] nr_crc = new byte[2];
//nr_crc = Crc16(byte_read, byte_read.Length);
//List<byte> data = byte_read.ToList();
//data.AddRange(nr_crc);
//string list = tools.byteToHexStr(data.ToArray());
//byte[] bytea = tools.ConvertHexStringToBytes(list);
//Time_Send(time, bytea);
timer_Read = new System.Threading.Timer(new System.Threading.TimerCallback(Time_Send), null, 0, 1000);
button11.Text = "停止";
}
else if (button11.Text == "停止")
{
timer_Read.Dispose();
isSending = false;
button11.Text = "自动读取";
}
}
/// <summary>
/// 定时发送数据
/// </summary>
/// <param name="time"></param>
/// <param name="message"></param>
//private async void Time_Send(int time, byte[] message)
//{
// while (isSending)
// {
// _client.SendData(message);
// //间隔
// await Task.Delay(1000 * time);
// }
//}
private void Time_Send(object sender)
{
byte[] byte_read = new byte[] { (byte)station_id, 03, 00, 00, 00, 24 };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byte_read, byte_read.Length);
List<byte> data = byte_read.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
/// <summary>
/// 远程控制
/// </summary>
/// <param name="kind"></param>
/// <param name="Sensor_Id"></param>
/// <param name="crc_1"></param>
/// <param name="cec_2"></param>
private void Control(int kind, int Sensor_Id)
{
byte[] byteaq = new byte[] { (byte)station_id, 16, 00, (byte)kind, 00, 01, 02, 00, (byte)Sensor_Id };
byte[] nr_crc = new byte[2];
nr_crc = Crc16(byteaq, byteaq.Length);
List<byte> data = byteaq.ToList();
data.AddRange(nr_crc);
string list = tools.byteToHexStr(data.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
_client.SendData(bytea);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox3.Text))
return;
station_id = Convert.ToInt32(this.textBox3.Text);
}
#region CRC校验
private static readonly byte[] aucCRCHi = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40
};
private static readonly byte[] aucCRCLo = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
0x41, 0x81, 0x80, 0x40
};
private static byte[] Crc16(byte[] pucFrame, int usLen)
{
int i = 0;
byte crcHi = 0xFF;
byte crcLo = 0xFF;
UInt16 iIndex = 0x0000;
while (usLen-- > 0)
{
iIndex = (UInt16)(crcLo ^ pucFrame[i++]);
crcLo = (byte)(crcHi ^ aucCRCHi[iIndex]);
crcHi = aucCRCLo[iIndex];
}
return new byte[] { crcLo, crcHi };
}
#endregion
}
}

1253
YTDX202303TCP/Form1.resx Normal file

File diff suppressed because it is too large Load Diff

22
YTDX202303TCP/Program.cs Normal file
View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace YTDX202303TCP
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("YTDX202303TCP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ZTT")]
[assembly: AssemblyProduct("YTDX202303TCP")]
[assembly: AssemblyCopyright("Copyright © ZTT 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("99eb89bc-a17c-48a3-8e8f-a62d84eb02d1")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,71 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
// 运行时版本: 4.0.30319.42000
//
// 对此文件的更改可能导致不正确的行为,如果
// 重新生成代码,则所做更改将丢失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace YTDX202303TCP.Properties
{
/// <summary>
/// 强类型资源类,用于查找本地化字符串等。
/// </summary>
// 此类是由 StronglyTypedResourceBuilder
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
// (以 /str 作为命令选项),或重新生成 VS 项目。
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
/// <summary>
/// 返回此类使用的缓存 ResourceManager 实例。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("YTDX202303TCP.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
/// <summary>
/// 重写当前线程的 CurrentUICulture 属性,对
/// 使用此强类型资源类的所有资源查找执行重写。
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}

View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,30 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace YTDX202303TCP.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}

View File

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)">
<Profiles>
<Profile Name="(Default)" />
</Profiles>
<Settings />
</SettingsFile>

109
YTDX202303TCP/Sockets.cs Normal file
View File

@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace YTDX202303TCP
{
public class Sockets
{
public delegate void PushSockets(Sockets sockets);
/// <summary>
///
/// </summary>
public Sockets()
{
}
/// <summary>
/// 创建Sockets对象
/// </summary>
/// <param name="ip">Ip地址</param>
/// <param name="client">TcpClient</param>
/// <param name="ns">承载客户端Socket的网络流</param>
public Sockets(IPEndPoint ip, TcpClient client, NetworkStream ns)
{
Ip = ip;
Client = client;
nStream = ns;
}
/// <summary>
/// 创建Sockets对象
/// </summary>
/// <param name="name">用户名</param>
/// <param name="pass">密码</param>
/// <param name="ip">Ip地址</param>
/// <param name="client">TcpClient</param>
/// <param name="ns">承载客户端Socket的网络流</param>
public Sockets(string name, string pass, IPEndPoint ip, TcpClient client, NetworkStream ns)
{
UserName = name;
Password = pass;
Ip = ip;
Client = client;
nStream = ns;
}
/// <summary>
/// 接收缓冲区
/// </summary>
public byte[] RecBuffer = new byte[8 * 1024];
/// <summary>
/// 发送缓冲区
/// </summary>
public byte[] SendBuffer = new byte[8 * 1024];
/// <summary>
/// 异步接收后包的大小
/// </summary>
public int Offset { get; set; }
/// <summary>
/// 用户名
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; }
/// <summary>
/// 当前IP地址端口号
/// </summary>
public IPEndPoint Ip { get; set; }
/// <summary>
/// 客户端主通信程序
/// </summary>
public TcpClient Client { get; set; }
/// <summary>
/// 承载客户端Socket的网络流
/// </summary>
public NetworkStream nStream { get; set; }
/// <summary>
/// 发生异常时不为null
/// </summary>
public Exception ex { get; set; }
/// <summary>
/// 新客户端标识.如果推送器发现此标识为true,那么认为是客户端上线 仅服务端有效
/// </summary>
public bool NewClientFlag { get; set; }
/// <summary>
/// 客户端退出标识.如果服务端发现此标识为true,那么认为客户端下线
/// 客户端接收此标识时,认为客户端异常.
/// </summary>
public bool ClientDispose { get; set; }
}
/// <summary>
/// Socket基类(抽象类)
/// 抽象3个方法,初始化Socket(含一个构造),停止,启动方法.
/// 此抽象类为TcpServer与TcpClient的基类,前者实现后者抽象方法
/// </summary>
public abstract class SocketObject
{
public abstract void InitSocket(IPAddress ipaddress, int port);
public abstract void InitSocket(string ipaddress, int port);
public abstract void Start();
public abstract void Stop();
}
}

View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{99EB89BC-A17C-48A3-8E8F-A62D84EB02D1}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>YTDX202303TCP</RootNamespace>
<AssemblyName>YTDX202303TCP</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>bitbug_favicon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="Data.cs" />
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sockets.cs" />
<Compile Include="tools.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
<SubType>Designer</SubType>
</EmbeddedResource>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Resource Include="bitbug_favicon.ico">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

223
YTDX202303TCP/tools.cs Normal file
View File

@ -0,0 +1,223 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace YTDX202303TCP
{
class tools
{
//获取当前设备的所有端口号
public static string[] GetSerialPort()
{
string[] ports = null;
ports = System.IO.Ports.SerialPort.GetPortNames();
return ports;
}
/// 16进制原码字符串转字节数组
/// </summary>
/// <param name="hexString">"AABBCC"或"AA BB CC"格式的字符串</param>
/// <returns></returns>
public static byte[] ConvertHexStringToBytes(string hexString)
{
hexString = hexString.Replace(" ", "");
if (hexString.Length % 2 != 0)
{
throw new ArgumentException("参数长度不正确");
}
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return returnBytes;
}
/// <summary>
/// //16转2方法
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public static string HexString2BinString(string hexString)
{
try
{
string result = string.Empty;
foreach (char c in hexString)
{
int v = Convert.ToInt32(c.ToString(), 16);
int v2 = int.Parse(Convert.ToString(v, 2));
// 去掉格式串中的空格即可去掉每个4位二进制数之间的空格
result += string.Format("{0:d4} ", v2);
}
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
}
/// <summary>
/// 字节数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
public static string HexStringToString(string hs, Encoding encode)
{
StringBuilder strTemp = new StringBuilder();
byte[] b = new byte[hs.Length / 2];
for (int i = 0; i < hs.Length / 2; i++)
{
strTemp.Clear();
strTemp.Append(hs.Substring(i * 2, 2));
b[i] = Convert.ToByte(strTemp.ToString(), 16);
}
return encode.GetString(b);
}
/// <summary>
/// txt文档自动保存
/// </summary>
/// <param name="logstring"></param>
public static void AddLgoToTXT(string _file_name, string path, string logstring)
{
path = path + _file_name;
if (!System.IO.File.Exists(path))
{
FileStream stream = System.IO.File.Create(path);
stream.Close();
stream.Dispose();
}
//using (StreamWriter writer = new StreamWriter(path, true))
//{
// writer.WriteLine(logstring);
//}
//[1]创建文件流 文件路径 和枚举类型的文件操作类型
FileStream fs = new FileStream(path, FileMode.Append);
//[2]创建写入器
StreamWriter sw = new StreamWriter(fs);
//[3]以流的方式写入数据
//sw.Write(logstring.Trim());
sw.Write(logstring);
//[4]关闭写入器
sw.Close();
//[5]关闭文件流
fs.Close();
}
//十六进制转字符串
public string ConvertHexToString(string HexValue, string separator = null)
{
HexValue = string.IsNullOrEmpty(separator) ? HexValue : HexValue.Replace(string.Empty, separator);
StringBuilder sbStrValue = new StringBuilder();
while (HexValue.Length > 0)
{
sbStrValue.Append(Convert.ToChar(Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString());
HexValue = HexValue.Substring(2);
}
return sbStrValue.ToString();
}
/// <summary>
/// 将传入的byte数组 从指定位置倒置
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public static float bytetofloat(List<byte> b, int start)
{
return BitConverter.ToSingle(new byte[] { b[start + 3], b[start + 2], b[start + 1], b[start] }, 0);
}
public static int bytetoint(List<byte> b, int start, int len)
{
string a = "";
for (int i = 0; i < len; i++)
{
a += b[start + i].ToString("X2");
}
return Convert.ToInt32(a, 16);
}
public static int ByteToInt16(List<byte> b, int start, int len)
{
string a = "";
for (int i = 0; i < len; i++)
{
a += b[start + i].ToString("X2");
}
return Convert.ToInt16(a, 16);
}
/// <summary>
/// 将一条十六进制字符串转换为ASCII
/// </summary>
/// <param name="hexstring">一条十六进制字符串</param>
/// <returns>返回一条ASCII码</returns>
public static string HexStringToASCII(string hexstring)
{
byte[] bt = HexStringToBinary(hexstring);
string lin = "";
for (int i = 0; i < bt.Length; i++)
{
lin = lin + bt[i] + " ";
}
string[] ss = lin.Trim().Split(new char[] { ' ' });
char[] c = new char[ss.Length];
int a;
for (int i = 0; i < c.Length; i++)
{
a = Convert.ToInt32(ss[i]);
c[i] = Convert.ToChar(a);
}
string b = new string(c);
return b;
}
/**/
/// <summary>
/// 16进制字符串转换为二进制数组
/// </summary>
/// <param name="hexstring">用空格切割字符串</param>
/// <returns>返回一个二进制字符串</returns>
public static byte[] HexStringToBinary(string hexstring)
{
string[] tmpary = hexstring.Trim().Split(' ');
byte[] buff = new byte[tmpary.Length];
for (int i = 0; i < buff.Length; i++)
{
buff[i] = Convert.ToByte(tmpary[i], 16);
}
return buff;
}
}
}