1.修正GPIO获取

2.修复心跳线程不回收的问题
master
zhangxy 3 weeks ago
parent 399779926f
commit c7fe310bbf

@ -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>(ComboBox_SelectChanged);
GPOSelectionChangedCommand = new RelayCommand<ComboBox>(GPOComboBox_SelectChanged);
TextChangedCommand = new RelayCommand<ScrollViewer>(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++)
@ -436,6 +425,8 @@ namespace SocketExample
}
private Task onConnected(ITcpClient client, ConnectedEventArgs e)//连接后初始化
{
try
{
Infotext += "已连接!\n";
StateColour = "Green";
@ -475,9 +466,14 @@ namespace SocketExample
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 = 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;//目前客户端的数量

Loading…
Cancel
Save