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); } } }