退出软件断开与板子的连接

This commit is contained in:
maqiao 2025-01-18 15:08:04 +08:00
parent 35270de250
commit 12c2cc4e22
2 changed files with 37 additions and 14 deletions

View File

@ -6,6 +6,7 @@ using DM_Weight.ViewModels;
using DM_Weight.Views;
using log4net;
using Mina.Core.Future;
using Mina.Core.Session;
using Mina.Filter.Codec;
using Mina.Filter.Logging;
using Mina.Transport.Socket;
@ -27,12 +28,12 @@ namespace DM_Weight.Port
private readonly ILog logger = LogManager.GetLogger(typeof(SocketHelper));
AsyncSocketConnector acceptor = new AsyncSocketConnector();
IConnectFuture iConnectFuture;
public bool OpenStatus=false;
public bool OpenStatus = false;
public bool ConnectedStatus = false;
//多线程退出标识
public bool IsMultiThread = false;
string ip = ConfigurationManager.AppSettings["modbusIp"].ToString();
int port =Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
int port = Convert.ToInt32(ConfigurationManager.AppSettings["modbusPort"]);
IEventAggregator _eventAggregator;
public DateTime dateTime { get; set; } = DateTime.Now;
FingerprintUtil _fingerprintUtil;
@ -70,16 +71,17 @@ namespace DM_Weight.Port
//}
};
acceptor.SessionClosed += (o, e) =>
{
logger.Info("SessionClosed");
//Task.Delay(50).Wait();
SocketConnect();
Task.Factory.StartNew(() =>
{
_fingerprintUtil.FingerDisconnect();
});
};
acceptor.SessionClosed += new EventHandler<IoSessionEventArgs>(SessionCloseMethod);
//acceptor.SessionClosed += (o, e) =>
//{
// logger.Info("SessionClosed");
// //Task.Delay(50).Wait();
// SocketConnect();
// Task.Factory.StartNew(() =>
// {
// _fingerprintUtil.FingerDisconnect();
// });
//};
this.SocketConnect();
_fingerprintUtil = fingerprintUtil;
}
@ -94,7 +96,7 @@ namespace DM_Weight.Port
{
if (i <= 3)
{
Thread.Sleep(1950);
Thread.Sleep(50);
//没连上会再连两次
SocketConnect();
}
@ -115,7 +117,27 @@ namespace DM_Weight.Port
}
ConnectedStatus = iConnectFuture.Connected;
}
private void SessionCloseMethod(System.Object o, IoSessionEventArgs e)
{
logger.Info($"SessionClosed");
SocketConnect();
Task.Factory.StartNew(() =>
{
_fingerprintUtil.FingerDisconnect();
});
}
public void SocketDisConnect()
{
try
{
acceptor.SessionClosed -= new EventHandler<IoSessionEventArgs>(SessionCloseMethod);
iConnectFuture.Session.CloseNow();
}
catch(Exception ex)
{
logger.Info($"SocketDisConnect异常{ex.Message}");
}
}
public void SendMessage(MyBaseMessage baseMessage)
{
if (!ConnectedStatus)

View File

@ -374,6 +374,7 @@ _exitCommand ??= new DelegateCommand(Exit);
void Exit()
{
//_chkFunction.HIKLoginOut();
_socketHelper.SocketDisConnect();
Process.GetCurrentProcess().Kill();
Environment.Exit(0);
}