diff --git a/UserClass/AsyncTcpClient.cs b/UserClass/AsyncTcpClient.cs index 886973e..df4c69c 100644 --- a/UserClass/AsyncTcpClient.cs +++ b/UserClass/AsyncTcpClient.cs @@ -30,9 +30,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass /// 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.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到服务器 tcpClient.Closing = (client, e) => { return EasyTask.CompletedTask; };//即将从服务器断开连接。此处仅主动断开才有效。 @@ -51,28 +51,14 @@ namespace SunlightCentralizedControlManagement_SCCM_.UserClass .SetRemoteIPHost(ip+":"+port) .ConfigureContainer(a => { - a.AddConsoleLogger();//添加一个日志注入 + // a.AddConsoleLogger();//添加一个日志注入 })); - await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 - - Result result = await tcpClient.TryConnectAsync();//或者可以调用TryConnectAsync - if (result.IsSuccess()) - { - - } - - tcpClient.Logger.Info("客户端成功连接"); + await tcpClient.ConnectAsync();//调用连接,当连接不成功时,会抛出异常。 } - - // public Task SendAsync(string id, ReadOnlyMemory memory); - // public Task SendAsync(string id, IRequestInfo requestInfo); - } - - } diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index 7fea735..45ea72b 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/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.Collections.Generic; using System.ComponentModel; @@ -8,6 +10,8 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Threading; +using TouchSocket.Core; +using TouchSocket.Sockets; using static SunlightCentralizedControlManagement_SCCM_.UserClass.SqliteHelper; namespace SunlightCentralizedControlManagement_SCCM_.ViewModel @@ -69,9 +73,16 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S { Sys_Time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); + + + } 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 { @@ -89,7 +100,30 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel SQLiteHelpers.Close(); 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 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(name); + return index; + } + } + catch (Exception) + { + // LogGing.LogGingDATA("SDTD:" + ex.ToString()); + return "ERR"; + } + } } }