_socket.BeginConnect,开药箱指令14->0
This commit is contained in:
parent
f2614723e2
commit
c7d7a3b266
|
@ -67,6 +67,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentModbus" Version="5.3.0" />
|
||||||
<PackageReference Include="log4net" Version="2.0.15" />
|
<PackageReference Include="log4net" Version="2.0.15" />
|
||||||
<PackageReference Include="MaterialDesignThemes" Version="4.8.0" />
|
<PackageReference Include="MaterialDesignThemes" Version="4.8.0" />
|
||||||
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
|
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
|
||||||
|
|
|
@ -39,7 +39,7 @@ namespace DM_Weight.Port
|
||||||
.Execute<Socket>(() => {
|
.Execute<Socket>(() => {
|
||||||
_socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
|
_socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
|
||||||
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
|
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
|
||||||
_socket.Connect(iep);
|
_socket.BeginConnect(iep,null,null);
|
||||||
return _socket;
|
return _socket;
|
||||||
});
|
});
|
||||||
return _socket;
|
return _socket;
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
using DM_Weight.ViewModels;
|
||||||
|
using FluentModbus;
|
||||||
|
using log4net;
|
||||||
|
using Polly;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Speech.Synthesis;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace DM_Weight.Port
|
||||||
|
{
|
||||||
|
public class ModBusTcpClientHelper
|
||||||
|
{
|
||||||
|
public static ModbusTcpClient client;
|
||||||
|
public static bool BoxOperate { get; set; }
|
||||||
|
private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderNewWindowViewModel));
|
||||||
|
public static ModbusTcpClient GetInstance()
|
||||||
|
{
|
||||||
|
if (client == null)
|
||||||
|
{
|
||||||
|
client = new ModbusTcpClient();
|
||||||
|
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4002), ModbusEndianness.BigEndian);
|
||||||
|
}
|
||||||
|
else if (!client.IsConnected)
|
||||||
|
{
|
||||||
|
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 4002), ModbusEndianness.BigEndian);
|
||||||
|
}
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
public bool OpenBoxDoor(int boxNum)
|
||||||
|
{
|
||||||
|
bool bFlag = false;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
BoxOperate = true;
|
||||||
|
Thread.Sleep(50);
|
||||||
|
client.WriteSingleRegister(1, (ushort)boxNum, 0);
|
||||||
|
logger.Info($"开门指令已发送{(ushort)boxNum}");
|
||||||
|
SpeakAsync("药箱已打开,请及时关闭");
|
||||||
|
bFlag = true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
SpeakAsync("药箱已打开,请及时关闭");
|
||||||
|
logger.Info($"药箱已打开异常:{ex.Message}");
|
||||||
|
}
|
||||||
|
return bFlag;
|
||||||
|
}
|
||||||
|
public bool[] GetAllBoxState()
|
||||||
|
{
|
||||||
|
bool[] state = new bool[1];
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
private static SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer();
|
||||||
|
public static void SpeakAsync(string textinfo)
|
||||||
|
{
|
||||||
|
speechSynthesizer.SpeakAsync(textinfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,7 @@ namespace DM_Weight.Port
|
||||||
{
|
{
|
||||||
logger.Info("ModbusHelper");
|
logger.Info("ModbusHelper");
|
||||||
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port);
|
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port);
|
||||||
|
//socket = CreateSocket();
|
||||||
client = new TcpClient();
|
client = new TcpClient();
|
||||||
client.Client = socket;
|
client.Client = socket;
|
||||||
master = ModbusIpMaster.CreateIp(client);
|
master = ModbusIpMaster.CreateIp(client);
|
||||||
|
@ -51,7 +52,7 @@ namespace DM_Weight.Port
|
||||||
private void SetModusIpMaster()
|
private void SetModusIpMaster()
|
||||||
{
|
{
|
||||||
logger.Info("SetModusIpMaster");
|
logger.Info("SetModusIpMaster");
|
||||||
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port); ;
|
socket = KeepALiveSocket.MakeKeepALiveSocket(ip, port);
|
||||||
client = new TcpClient();
|
client = new TcpClient();
|
||||||
client.Client = socket;
|
client.Client = socket;
|
||||||
master = ModbusIpMaster.CreateIp(client);
|
master = ModbusIpMaster.CreateIp(client);
|
||||||
|
@ -128,13 +129,14 @@ namespace DM_Weight.Port
|
||||||
this.Dispose();
|
this.Dispose();
|
||||||
//Debug.WriteLine($"打开箱子出错,第{retryCount}次重试", exception);
|
//Debug.WriteLine($"打开箱子出错,第{retryCount}次重试", exception);
|
||||||
logger.Info($"打开箱子出错,第{retryCount}次重试,异常信息{exception}");
|
logger.Info($"打开箱子出错,第{retryCount}次重试,异常信息{exception}");
|
||||||
|
SpeakAsync("打开药箱网中连接失败,正在尝试重新打开");
|
||||||
Thread.Sleep(50);
|
Thread.Sleep(50);
|
||||||
this.SetModusIpMaster();
|
this.SetModusIpMaster();
|
||||||
//return TimeSpan.FromSeconds (1);
|
//return TimeSpan.FromSeconds (1);
|
||||||
})).Execute(() =>
|
})).Execute(() =>
|
||||||
{
|
{
|
||||||
logger.Info($"正在打开{boxNum}号药箱");
|
logger.Info($"正在打开{boxNum}号药箱");
|
||||||
master.WriteSingleRegister(1, (ushort)boxNum, 14);
|
master.WriteSingleRegister(1, (ushort)boxNum, 0);
|
||||||
logger.Info($"开门指令已发送{(ushort)boxNum}");
|
logger.Info($"开门指令已发送{(ushort)boxNum}");
|
||||||
SpeakAsync("药箱已打开,请及时关闭");
|
SpeakAsync("药箱已打开,请及时关闭");
|
||||||
//Console.WriteLine($"开门指令已发送{(ushort)boxNum}");
|
//Console.WriteLine($"开门指令已发送{(ushort)boxNum}");
|
||||||
|
@ -154,5 +156,30 @@ namespace DM_Weight.Port
|
||||||
{
|
{
|
||||||
speechSynthesizer.SpeakAsync(textinfo);
|
speechSynthesizer.SpeakAsync(textinfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Socket CreateSocket()
|
||||||
|
{
|
||||||
|
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
string serverIp = "127.0.0.1"; // 服务器IP地址
|
||||||
|
int port = 4002; // 服务器端口
|
||||||
|
IPAddress serverAddress = IPAddress.Parse(serverIp);
|
||||||
|
IPEndPoint remoteEP = new IPEndPoint(serverAddress, port);
|
||||||
|
|
||||||
|
clientSocket.Connect(remoteEP);
|
||||||
|
|
||||||
|
if (clientSocket.Connected)
|
||||||
|
{
|
||||||
|
|
||||||
|
// 发送数据
|
||||||
|
//byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
|
||||||
|
//clientSocket.Send(msg);
|
||||||
|
|
||||||
|
// 关闭socket
|
||||||
|
//clientSocket.Shutdown(SocketShutdown.Both);
|
||||||
|
//clientSocket.Close();
|
||||||
|
}
|
||||||
|
return clientSocket;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -285,37 +285,33 @@ namespace DM_Weight.ViewModels
|
||||||
// _regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath);
|
// _regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath);
|
||||||
// }
|
// }
|
||||||
//}
|
//}
|
||||||
|
if (SelectedMenu != null && SelectedMenu.PremissionName == "退出")
|
||||||
if (!ModbusHelper.BoxOperate)
|
|
||||||
{
|
{
|
||||||
if (SelectedMenu != null && SelectedMenu.PremissionName == "退出")
|
//bool[] boolArrs = ModbusHelper.GetInstance().GetAllBoxState();
|
||||||
|
//bool allTrue = Array.TrueForAll(boolArrs, b => b);
|
||||||
|
//false是关着,true是开着
|
||||||
|
if (!ModbusHelper.BoxOperate)
|
||||||
{
|
{
|
||||||
//bool[] boolArrs = ModbusHelper.GetInstance().GetAllBoxState();
|
logger.Info($"用户【{Operator?.Nickname}】退出登录");
|
||||||
//bool allTrue = Array.TrueForAll(boolArrs, b => b);
|
Operator = null;
|
||||||
//false是关着,true是开着
|
Reviewer = null;
|
||||||
//if (!ModbusHelper.BoxOperate)
|
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
||||||
//{
|
|
||||||
logger.Info($"用户【{Operator?.Nickname}】退出登录");
|
|
||||||
Operator = null;
|
|
||||||
Reviewer = null;
|
|
||||||
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
|
||||||
//}
|
|
||||||
//else
|
|
||||||
//{
|
|
||||||
// //还有药箱开着不能退出
|
|
||||||
// _portUtil.SpeakAsync("请关闭药箱后再退出");
|
|
||||||
// SelectedMenu = _premissionDmList[0];
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//SelectedMenu.Children = SelectedMenu.Children;
|
//还有药箱开着不能退出
|
||||||
//SelectedChildMenu = SelectedMenu.Children[0];
|
_portUtil.SpeakAsync("请关闭药箱后再退出");
|
||||||
if (!_portUtil.Operate)
|
SelectedMenu = _premissionDmList[0];
|
||||||
{
|
}
|
||||||
_regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath);
|
}
|
||||||
}
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//SelectedMenu.Children = SelectedMenu.Children;
|
||||||
|
//SelectedChildMenu = SelectedMenu.Children[0];
|
||||||
|
if (!_portUtil.Operate)
|
||||||
|
{
|
||||||
|
_regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue