diff --git a/SocketExample/FodyWeavers.xml b/SocketExample/FodyWeavers.xml
new file mode 100644
index 0000000..a5dcf04
--- /dev/null
+++ b/SocketExample/FodyWeavers.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/SocketExample/MyTouchClass.cs b/SocketExample/MyTouchClass.cs
new file mode 100644
index 0000000..82da05d
--- /dev/null
+++ b/SocketExample/MyTouchClass.cs
@@ -0,0 +1,83 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.NetworkInformation;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows;
+using TouchSocket;
+using TouchSocket.Core;
+using TouchSocket.Sockets;
+using static SocketExample.TCPWindowV2;
+namespace SocketExample
+{
+ public class MyTouchClass
+ {
+ public void ConnectedSend()
+ {
+ try
+ {
+
+ //streamWriter = new StreamWriter(path);
+
+ byte[] data = strToToHexByte("AA 55 01 70 00 71 0D");//获取模块信息
+ option = "00";
+ client.Send(data);
+
+ Thread.Sleep(500);
+
+ data = strToToHexByte("AA 55 01 70 03 72 0D");//获取主板硬件信息
+ option = "03";
+ client.Send(data);
+
+ Thread.Sleep(500);
+
+ data = strToToHexByte("AA 55 01 70 04 75 0D");//获取主板固件信息
+ option = "04";
+ client.Send(data);
+
+ Thread.Sleep(500);
+
+ data = strToToHexByte("AA 55 00 72 72 0D");//获取功率状态
+ client.Send(data);
+
+ Thread.Sleep(500);
+
+ data = strToToHexByte("AA 55 00 81 81 0D");//获取GPIO状态
+ client.Send(data);
+
+ Thread.Sleep(500);
+
+ data = strToToHexByte("AA 55 02 02 07 D0 D7 0D");//获取1000ms内标签信息
+ client.Send(data);
+
+ Thread.Sleep(1000);
+
+ GPIOthread = new Thread(new ThreadStart(delegate ()
+ {
+ while (true) { byte[] data = strToToHexByte("AA 55 00 81 81 0D"); client.Send(data); Thread.Sleep(1000); }
+ }));
+ GPIOthread.Start();//心跳获取GPIO状态
+ }
+ catch (Exception ex)
+ {
+ Infotext += $"连接时报错,错误内容:{ex}!\n";
+ }
+ }
+ public sealed class MySessionClient : TcpSessionClient
+ {
+
+ protected override async Task OnTcpReceived(ReceivedDataEventArgs e)
+ {
+ //此处逻辑单线程处理。
+
+ //此处处理数据,功能相当于Received委托。
+ var mes = e.ByteBlock.Span.ToString(Encoding.UTF8);
+ Console.WriteLine($"已接收到信息:{mes}");
+
+ await base.OnTcpReceived(e);
+ }
+ }
+ }
+}
diff --git a/SocketExample/RFIDmonitor.csproj b/SocketExample/RFIDmonitor.csproj
index 650a992..3487987 100644
--- a/SocketExample/RFIDmonitor.csproj
+++ b/SocketExample/RFIDmonitor.csproj
@@ -1,5 +1,6 @@
+
Debug
@@ -63,6 +64,9 @@
..\packages\CommonServiceLocator.2.0.2\lib\net47\CommonServiceLocator.dll
+
+ ..\packages\Costura.Fody.6.0.0\lib\netstandard2.0\Costura.dll
+
..\packages\MvvmLightLibs.5.4.1.1\lib\net45\GalaSoft.MvvmLight.dll
@@ -203,6 +207,7 @@
+
TCPWindowV2.xaml
@@ -272,6 +277,9 @@
+
+
+
@@ -279,5 +287,10 @@
这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。
+
+
+
+
+
\ No newline at end of file
diff --git a/SocketExample/TCPWindowV2.xaml b/SocketExample/TCPWindowV2.xaml
index 56a95e1..f2ee66f 100644
--- a/SocketExample/TCPWindowV2.xaml
+++ b/SocketExample/TCPWindowV2.xaml
@@ -105,9 +105,10 @@
-
+
+
diff --git a/SocketExample/TCPWindowV2.xaml.cs b/SocketExample/TCPWindowV2.xaml.cs
index 026dfba..c2c63a6 100644
--- a/SocketExample/TCPWindowV2.xaml.cs
+++ b/SocketExample/TCPWindowV2.xaml.cs
@@ -9,6 +9,7 @@ using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Net;
+using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -36,6 +37,8 @@ using TouchSocket.Core;
using TouchSocket.Sockets;
using ZstdSharp.Unsafe;
+using static SocketExample.MyTouchClass;
+
namespace SocketExample
{
///
@@ -49,13 +52,14 @@ namespace SocketExample
#region 变量定义
IPAddress ip;
int port;
- TcpClient client;
+ ITcpClient client;
+ ITcpSessionClient TcpSessionClient;
public string option;//命令选项
- public ICommand TextChangedCommand { get; }
- public ICommand SelectionChangedCommand { get; }
- public ICommand SendTextChangedCommand { get; }
+ public ICommand TextChangedCommand { get; set; }
+ public ICommand SelectionChangedCommand { get; set; }
+ public ICommand SendTextChangedCommand { get; set; }
- public ICommand GPOSelectionChangedCommand { get; }
+ public ICommand GPOSelectionChangedCommand { get; set; }
private ObservableCollection _tagItems;
public ObservableCollection TagItems { get => _tagItems; set { _tagItems = value; OnPropertyChanged(); } }
@@ -64,6 +68,7 @@ namespace SocketExample
public string Porttext { get; set; }//端口文本框
private string _messagetext;
+ public string SessionID { get; set; }
public int Borderwidth { get => _borderwidth; set { _borderwidth = value; OnPropertyChanged(); } }
private int _borderwidth { get; set; }
public string MessageText { get => _messagetext; set { _messagetext = value; OnPropertyChanged(); } }//发送框
@@ -191,6 +196,22 @@ namespace SocketExample
public PanelItem(int index)
{
Text = $"客户端{index + 1}";
+ PanelInitialization();
+ }
+
+ public PanelItem(TcpSessionClient recieveClient)
+ {
+ Text = $"客户端{recieveClient.IP}";
+ TcpSessionClient = recieveClient as ITcpSessionClient;
+ SessionID = recieveClient.Id;
+ //断连事件
+ //TcpSessionClient.Closed = (client, e) => { try { GPIOthread.Abort(); Infotext += "已断开!\n"; StateColour = "Red"; LinkState = "未连接"; } catch (Exception ex) { Infotext += ex; } return EasyTask.CompletedTask; };
+ //接收事件
+ PanelInitialization();
+ }
+
+ public void PanelInitialization()//终端初始化
+ {
linkcomn = new LinkCommand(Button_Link);//绑定按钮的链接事件
readcomn = new LinkCommand(Button_Read);
stopreadingcomn = new LinkCommand(Button_StopReading);
@@ -232,7 +253,8 @@ namespace SocketExample
GPI2 = "低";
GPI3 = "低";
GPI4 = "低";
- }
+ }//终端初始化
+
#region 界面功能绑定定义
public LinkCommand _powerselectchanged { get { return powerselectchanged; } }
@@ -645,12 +667,13 @@ namespace SocketExample
{
int number = 4;
int width = ((int)SystemParameters.PrimaryScreenWidth - 60) / number;
- while(width<360 || width > 700){
+ while (width < 360 || width > 700)
+ {
if (width < 360)
{
number--;
}
- else if(width >700)
+ else if (width > 700)
{
number++;
}
@@ -1145,7 +1168,7 @@ namespace SocketExample
public string Countinfo { get => _countinfo; set { _countinfo = value; OnPropertyChanged(); } }
private string _timeinfo;//当前时间
public string Timeinfo { get => _timeinfo; set { _timeinfo = value; OnPropertyChanged(); } }
-
+
private string _infocolor;
public string InfoColor { get => _infocolor; set { _infocolor = value; OnPropertyChanged(); } }
private string _epcinfo;//EPC信息
@@ -1165,7 +1188,7 @@ namespace SocketExample
{
InfoColor = "Red";
}
- else
+ else
InfoColor = "Green";
}
@@ -1194,6 +1217,7 @@ namespace SocketExample
InitializeComponent();
EnsureFolderExists("日志文件夹");
this.Closing += TCPWindowV2_Closing;
+ PanelContainer.ItemsSource = items;
SaveWindowSettings();
Buttonflag = 0;
}
@@ -1242,7 +1266,7 @@ namespace SocketExample
if (Buttonflag == 0)
{
this.WindowState = WindowState.Maximized;
- MaxMinImage.Source = new BitmapImage(new Uri(@"/取消全屏.png",UriKind.Relative));
+ MaxMinImage.Source = new BitmapImage(new Uri(@"/取消全屏.png", UriKind.Relative));
Buttonflag = 1;
}
else
@@ -1267,7 +1291,112 @@ namespace SocketExample
PanelContainer.ItemsSource = null;
currentcount = 0;
}
+ private void ListenButton_Click(object sender, RoutedEventArgs e)
+ {
+ Thread ListeningPortThread = new Thread(ListeningTheard);
+ ListeningPortThread.IsBackground = true;
+ ListeningPortThread.Start();
+ }
#endregion
+
+ #region 其他功能
+
+ private void ListeningTheard() //监听线程
+ {
+ IEnumerable networkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
+ .Where(nic => nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet // 仅以太网
+ && nic.OperationalStatus == OperationalStatus.Up); // 仅已启用的接口
+ CreateListenConnect(networkInterfaces);
+
+
+ while (true)
+ {
+ IEnumerable NewnetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces()
+ .Where(nic => nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet // 仅以太网
+ && nic.OperationalStatus == OperationalStatus.Up); // 仅已启用的接口
+
+ var UniquenetworkInterfaces = NewnetworkInterfaces
+ .Where(newNic => !networkInterfaces.Any(oldNic => oldNic.Id == newNic.Id))
+ .ToList();
+
+ if (!UniquenetworkInterfaces.Any())
+ {
+ Thread.Sleep(1000);
+ continue;
+ }
+ networkInterfaces = NewnetworkInterfaces;
+ CreateListenConnect(UniquenetworkInterfaces);
+ }
+ }
+
+ private void CreateListenConnect(IEnumerable networkInterfaces) //创建连接
+ {
+ foreach (var nic in networkInterfaces)
+ {
+ var ipProperties = nic.GetIPProperties();
+ var unicastAddresses = ipProperties.UnicastAddresses
+ .Where(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
+
+ foreach (var addr in unicastAddresses)
+ {
+ var tcpService = new TcpService();
+ tcpService.SetupAsync(new TouchSocketConfig()//载入配置
+ .SetListenIPHosts($"tcp://{addr.Address}:20108", 7790)//可以同时监听两个地址
+ .ConfigureContainer(a =>//容器的配置顺序应该在最前面
+ {
+ a.AddConsoleLogger();//添加一个控制台日志注入(注意:在maui中控制台日志不可用)
+ })
+ .ConfigurePlugins(a =>
+ {
+ //a.Add();//此处可以添加插件
+ }));
+ tcpService.Connected += ListeningConnected;
+ tcpService.Closed += ListeningClosed;
+ tcpService.StartAsync();
+ Console.WriteLine($"网卡: {nic.Description}, IP: {addr.Address}");
+ MessageBox.Show($"{addr.Address}监听成功!");
+ }
+ }
+ }
+
+ private Task ListeningConnected(TcpSessionClient client, ConnectedEventArgs e) //连接事件
+ {
+ Application.Current.Dispatcher.Invoke(() =>
+ {
+ items.Add(new PanelItem(client));
+ PanelContainer.ItemsSource = null;
+ PanelContainer.ItemsSource = items;
+
+ });
+ return EasyTask.CompletedTask;
+ }
+
+ private Task ListeningClosed(TcpSessionClient client, ClosedEventArgs e) //断开事件
+ {
+ Application.Current.Dispatcher.Invoke(() =>
+ {
+ foreach (PanelItem item in items)
+ {
+ if (item.SessionID.Equals(client.Id))
+ {
+ try
+ {
+ items.Remove(item);
+ if (items.Count == 0)
+ break;
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message);
+ }
+ }
+ }
+ PanelContainer.ItemsSource = null;
+ PanelContainer.ItemsSource = items;
+ });
+ return EasyTask.CompletedTask;
+ }
+
public class LinkCommand : ICommand //command方法实现
{
private Action _excute;
@@ -1529,5 +1658,6 @@ namespace SocketExample
OriginWindowState = this.WindowState;
}
+ #endregion
}
}
diff --git a/SocketExample/packages.config b/SocketExample/packages.config
index e4def36..9a52a7b 100644
--- a/SocketExample/packages.config
+++ b/SocketExample/packages.config
@@ -2,6 +2,8 @@
+
+