diff --git a/SocketExample/TCPWindowV2.xaml.cs b/SocketExample/TCPWindowV2.xaml.cs index 90f1fe3..fef4552 100644 --- a/SocketExample/TCPWindowV2.xaml.cs +++ b/SocketExample/TCPWindowV2.xaml.cs @@ -155,6 +155,8 @@ namespace SocketExample public LinkCommand powerselectchanged; + Thread GPIOthread; + #endregion public PanelItem(int index) @@ -170,19 +172,6 @@ namespace SocketExample SelectionChangedCommand = new RelayCommand(ComboBox_SelectChanged); GPOSelectionChangedCommand = new RelayCommand(GPOComboBox_SelectChanged); TextChangedCommand = new RelayCommand(TextBlock_SelectChanged); - - try - { - - path = Text + ".txt"; - File.CreateText(path); - - } - catch (Exception ex) - { - Infotext += ex; - } - //页面初始化 int[] Powerdatasource = {5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33}; string[] GPOdatasource = {"高","低"}; @@ -245,7 +234,7 @@ namespace SocketExample string dataLength = readStateString.Substring(4, 2);//获取字符串中内容长度 string commandState = readStateString.Substring(6, 2);//获取命令 - if(commandState != "81") + //if(commandState != "81") Infotext += $"({currentTime})客户端接收到信息:\n{hexString}\n\n"; int i = Convert.ToInt32(dataLength, 16); @@ -293,7 +282,7 @@ namespace SocketExample case "81": //获取GPIO状态,16进制转二进制 - string GPIBinInfo = Convert.ToString(Convert.ToInt32(readStateString.Substring(8, 2), 16), 2).PadLeft(8, '0'); + string GPIBinInfo = Convert.ToString(Convert.ToInt32(readStateString.Substring(12, 2), 16), 2).PadLeft(8, '0'); string GPOBinInfo = Convert.ToString(Convert.ToInt32(readStateString.Substring(10, 2), 16), 2).PadLeft(8, '0'); for (int j = 4; j < GPOBinInfo.Length; j++) @@ -437,47 +426,54 @@ namespace SocketExample private Task onConnected(ITcpClient client, ConnectedEventArgs e)//连接后初始化 { - Infotext += "已连接!\n"; - StateColour = "Green"; - LinkState = "已连接"; - //streamWriter = new StreamWriter(path); + try + { + Infotext += "已连接!\n"; + StateColour = "Green"; + LinkState = "已连接"; + //streamWriter = new StreamWriter(path); - byte[] data = strToToHexByte("AA 55 01 70 00 71 0D");//获取模块信息 - option = "00"; - client.Send(data); + byte[] data = strToToHexByte("AA 55 01 70 00 71 0D");//获取模块信息 + option = "00"; + client.Send(data); - Thread.Sleep(500); + Thread.Sleep(500); - data = strToToHexByte("AA 55 01 70 03 72 0D");//获取主板硬件信息 - option = "03"; - client.Send(data); + data = strToToHexByte("AA 55 01 70 03 72 0D");//获取主板硬件信息 + option = "03"; + client.Send(data); - Thread.Sleep(500); + Thread.Sleep(500); - data = strToToHexByte("AA 55 01 70 04 75 0D");//获取主板固件信息 - option = "04"; - client.Send(data); + data = strToToHexByte("AA 55 01 70 04 75 0D");//获取主板固件信息 + option = "04"; + client.Send(data); - Thread.Sleep(500); + Thread.Sleep(500); - data = strToToHexByte("AA 55 00 72 72 0D");//获取功率状态 - client.Send(data); + data = strToToHexByte("AA 55 00 72 72 0D");//获取功率状态 + client.Send(data); - Thread.Sleep(500); + Thread.Sleep(500); - data = strToToHexByte("AA 55 00 81 81 0D");//获取GPIO状态 - client.Send(data); + data = strToToHexByte("AA 55 00 81 81 0D");//获取GPIO状态 + client.Send(data); - Thread.Sleep(500); + Thread.Sleep(500); - data = strToToHexByte("AA 55 02 02 03 E8 EB 0D");//获取1000ms内标签信息 - client.Send(data); + data = strToToHexByte("AA 55 02 02 03 E8 EB 0D");//获取1000ms内标签信息 + client.Send(data); - Thread.Sleep(1000); + Thread.Sleep(1000); - - Thread GPIOthread = new Thread(new ThreadStart(delegate () { while (true) { byte[] data = strToToHexByte("AA 55 00 81 81 0D"); client.Send(data); Thread.Sleep(3000); } })); - GPIOthread.Start();//心跳获取GPIO状态 + GPIOthread = new Thread(new ThreadStart(delegate () { while (true) { byte[] data = strToToHexByte("AA 55 00 81 81 0D"); client.Send(data); Thread.Sleep(3000); } })); + GPIOthread.Start();//心跳获取GPIO状态 + } + catch (Exception ex) + { + Infotext += $"连接时报错,错误内容:{ex}!\n"; + } + return EasyTask.CompletedTask; } #endregion @@ -550,7 +546,7 @@ namespace SocketExample //连接事件 client.Connected += onConnected; //断连事件 - client.Closed = (client, e) => { Infotext += "已断开!\n"; StateColour = "Red"; LinkState = "未连接"; return EasyTask.CompletedTask; }; + client.Closed = (client, e) => { GPIOthread.Abort(); Infotext += "已断开!\n"; StateColour = "Red"; LinkState = "未连接"; return EasyTask.CompletedTask; }; //接收事件 client.Received += onRecieved; @@ -705,6 +701,8 @@ namespace SocketExample string comboxname = sender.Name; try { + if (sender.SelectedItem == null) + return; switch (comboxname) { case "GPO1ComboBox": @@ -801,7 +799,12 @@ namespace SocketExample { InitializeComponent(); EnsureFolderExists("日志文件夹"); + this.Closing += TCPWindowV2_Closing; + } + private void TCPWindowV2_Closing(object sender, CancelEventArgs e) + { + System.Environment.Exit(0); } int currentcount = 0;//目前客户端的数量