sc 11 months ago
parent
commit
6e2b9c2264
  1. 22
      UserClass/AsyncTcpClient.cs
  2. 54
      ViewModel/MainWindowViewModel.cs

22
UserClass/AsyncTcpClient.cs

@ -30,9 +30,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
/// </summary> /// </summary>
public class AsyncTcpClient public class AsyncTcpClient
{ {
public async Task TcpClient(string ip,string port) public static async Task TcpClient(TcpClient tcpClient, string ip,string port)
{ {
TcpClient tcpClient = new TcpClient(); //TcpClient tcpClient = new TcpClient();
tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp tcpClient.Connecting = (client, e) => { return EasyTask.CompletedTask; };//即将连接到服务器,此时已经创建socket,但是还未建立tcp
tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器 tcpClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器
tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。
@ -51,28 +51,14 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass
.SetRemoteIPHost(ip+":"+port) .SetRemoteIPHost(ip+":"+port)
.ConfigureContainer(a => .ConfigureContainer(a =>
{ {
a.AddConsoleLogger();//添加一个日志注入 // a.AddConsoleLogger();//添加一个日志注入
})); }));
await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。
Result result = await tcpClient.TryConnectAsync();//或者可以调用TryConnectAsync
if (result.IsSuccess())
{
}
tcpClient.Logger.Info("客户端成功连接");
} }
// public Task SendAsync(string id, ReadOnlyMemory<byte> memory);
// public Task SendAsync(string id, IRequestInfo requestInfo);
} }
} }

54
ViewModel/MainWindowViewModel.cs

@ -1,4 +1,6 @@
using SunlightCentralizedControlManagement_SCCM_.Properties; using OpenTK.Graphics.ES11;
using SunlightCentralizedControlManagement_SCCM_.Properties;
using SunlightCentralizedControlManagement_SCCM_.UserClass;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -8,6 +10,8 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Threading; using System.Windows.Threading;
using TouchSocket.Core;
using TouchSocket.Sockets;
using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper;
namespace SunlightCentralizedControlManagement_SCCM_.ViewModel namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
@ -69,9 +73,16 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S
{ {
Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
} }
void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S void Tick_Event_5S(object sender, EventArgs e)//Tick_Event周期执行事件5S
{ {
for (int i = 0; i < Machinesdata_Count; i++)
{
MachiensTcpClient[i].SendAsync("SC800");
}
} }
void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS void DisTimer_500MS(object sender, EventArgs e)//Tick_Event周期执行事件500MS
{ {
@ -89,7 +100,30 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
SQLiteHelpers.Close(); SQLiteHelpers.Close();
CountDown(); CountDown();
TcpClientNEW();
} }
private readonly TcpClient[] MachiensTcpClient = new TcpClient[999];
public int Machinesdata_Count;
public void TcpClientNEW()
{
DataRow[] machinesdata = Machines.Select("PORT>0 AND IP<>''");//获取连接有效的组
Machinesdata_Count = machinesdata.Count();
for (int i = 0; i < Machinesdata_Count; i++)
{
MachiensTcpClient[i] = new TcpClient();
DataRow dt= machinesdata[i];
_= AsyncTcpClient.TcpClient(MachiensTcpClient[i] //建立tcp连接
, Selet_Machines(MainWindowViewModel.Machines, "IP", Convert.ToInt16( dt["ID"])).ToString()
, Selet_Machines(MainWindowViewModel.Machines, "PORT", Convert.ToInt16(dt["ID"])).ToString());
}
}
public static string SYS_WorkNumder; //工单号 public static string SYS_WorkNumder; //工单号
public static int SYS_AT1; //附缸1-3 public static int SYS_AT1; //附缸1-3
@ -100,7 +134,23 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel
public static object Selet_Machines(DataTable DB, string name, int key)//查询
{
try
{
lock (DB)
{
DataRow drEmployee = DB.Select("ID='" + key + "'").First();
object index = drEmployee.Field<object>(name);
return index;
}
}
catch (Exception)
{
// LogGing.LogGingDATA("SDTD:" + ex.ToString());
return "ERR";
}
}
} }
} }

Loading…
Cancel
Save