change-改变PLC交互

master
liuwf 5 months ago
parent e01f7e73a3
commit 7d57ce7841

@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Security.Cryptography.Xml; using System.Security.Cryptography.Xml;
using System.ServiceModel.Channels;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using TouchSocket.Core; using TouchSocket.Core;
@ -50,6 +51,8 @@ namespace SlnMesnac.Business
public delegate void RefreshMessage(string message, bool isWarning = false); public delegate void RefreshMessage(string message, bool isWarning = false);
public static event RefreshMessage? RefreshMessageEvent; public static event RefreshMessage? RefreshMessageEvent;
#endregion #endregion
@ -124,14 +127,14 @@ namespace SlnMesnac.Business
hikFlag = await JudgeIsSuccessAsync(); hikFlag = await JudgeIsSuccessAsync();
if (hikFlag) if (hikFlag)
{ {
//校验成功下发放行 //校验成功放行
Pass();
RefreshMessageEvent?.Invoke("Logo识别成功下发放行"); RefreshMessageEvent?.Invoke("Logo识别成功下发放行");
logger.LogInformation($"箱体码:{materialCodeStr}Logo识别成功下发放行"); logger.LogInformation($"箱体码:{materialCodeStr}Logo识别成功下发放行");
} }
else else
{ {
//校验成功下发放行 //校验失败禁止放行
WarningAndStop($"Logo识别失败禁止放行"); WarningAndStop($"Logo识别失败禁止放行");
} }
} }
@ -140,7 +143,6 @@ namespace SlnMesnac.Business
// 不需要校验 // 不需要校验
hikFlag = true; hikFlag = true;
Pass();
RefreshMessageEvent?.Invoke("Logo无需识别下发放行"); RefreshMessageEvent?.Invoke("Logo无需识别下发放行");
logger.LogInformation($"箱体码:{materialCodeStr}Logo无需识别下发放行"); logger.LogInformation($"箱体码:{materialCodeStr}Logo无需识别下发放行");
} }
@ -267,23 +269,88 @@ namespace SlnMesnac.Business
//M100停止点位 //M100停止点位
public void Pass() public void Pass()
{ {
if(plc!=null && plc.IsConnected) try
{
if (plc != null && plc.IsConnected)
{
plc.writeInt16ByAddress("M100", 8);
Task.Run(() =>
{
// 设置计时器
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
if (plc.readInt16ByAddress("M100") == 0)
{ {
plc.writeInt16ByAddress("M100",0); logger.LogInformation("PLC复位成功,启动线体");
RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true);
}
// 检查是否超过两秒
if (stopwatch.ElapsedMilliseconds > 3000)
{
logger.LogError("PLC复位超时");
RefreshMessageEvent?.Invoke("PLC复位超时", true);
}
Thread.Sleep(100);
}
});
}
else
{
logger.LogError("PLC未连接请检查连接");
RefreshMessageEvent?.Invoke("PLC未连接请检查连接", true);
} }
// 声光电报警复位 // 声光电报警复位
gunHelper.SendData("OK"); gunHelper.SendData("OK");
}
catch (Exception ex)
{
logger.LogError($"下发PLC复位方法Pass()异常:{ex.Message}");
}
} }
public void Stop() public void Stop()
{
try
{ {
if (plc != null && plc.IsConnected) if (plc != null && plc.IsConnected)
{ {
plc.writeInt16ByAddress("M100", 1); plc.writeInt16ByAddress("M100", 9);
Task.Run(() =>
{
// 设置计时器
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
while (true)
{
if (plc.readInt16ByAddress("M100") == 1)
{
logger.LogInformation("PLC复位成功,启动线体");
RefreshMessageEvent?.Invoke("PLC复位成功,启动线体", true);
} }
// 声光电报警复位 // 检查是否超过两秒
if (stopwatch.ElapsedMilliseconds > 3000)
{
logger.LogError("PLC复位超时");
RefreshMessageEvent?.Invoke("PLC复位超时", true);
}
Thread.Sleep(100);
}
});
}
// 声光电报警
gunHelper.SendData("NG"); gunHelper.SendData("NG");
}
catch (Exception ex)
{
logger.LogError($"下发PLC报警方法Stop()异常:{ex.Message}");
}
} }
#endregion #endregion

@ -1,12 +1,18 @@
using GalaSoft.MvvmLight; using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Command;
using HslCommunication.Enthernet;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Win32; using Microsoft.Win32;
using SlnMesnac.Business; using SlnMesnac.Business;
using SlnMesnac.Common; using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Model.domain; using SlnMesnac.Model.domain;
using SlnMesnac.Plc;
using SlnMesnac.Repository.service; using SlnMesnac.Repository.service;
using SlnMesnac.TouchSocket;
using SlnMesnac.WPF.Views;
using SqlSugar; using SqlSugar;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -26,6 +32,18 @@ namespace SlnMesnac.WPF.ViewModel
private ILogoIdentifyService logoIdentifyService; private ILogoIdentifyService logoIdentifyService;
private GunHelper gunHelper = GunHelper.Instance; private GunHelper gunHelper = GunHelper.Instance;
public RelayCommand ResetCommand { get; set;} public RelayCommand ResetCommand { get; set;}
private LogoBusiness logoBusiness = null;
private readonly ILogger<LogoBusiness> _logger2;
private IBaseMaterialService? baseMaterialService;
private ILogoIdentifyService? ocrVerfiyService;
private ILogoConfigService? logoConfigService;
private TcpServer? tcpServer;
private PlcPool plcPool = null;
public IndexViewModel() public IndexViewModel()
{ {
logoIdentifyService = App.ServiceProvider.GetService<ILogoIdentifyService>(); logoIdentifyService = App.ServiceProvider.GetService<ILogoIdentifyService>();
@ -34,6 +52,13 @@ namespace SlnMesnac.WPF.ViewModel
ResetCommand = new RelayCommand(Reset); ResetCommand = new RelayCommand(Reset);
RefreshDataGrid(); RefreshDataGrid();
plcPool = App.ServiceProvider.GetService<PlcPool>();
tcpServer = App.ServiceProvider.GetService<TcpServer>();
ocrVerfiyService = App.ServiceProvider.GetService<ILogoIdentifyService>();
baseMaterialService = App.ServiceProvider.GetService<IBaseMaterialService>();
logoConfigService = App.ServiceProvider.GetService<ILogoConfigService>();
_logger2 = App.ServiceProvider.GetService<ILogger<LogoBusiness>>();
logoBusiness = LogoBusiness.GetInstance(_logger2, logoConfigService, baseMaterialService, ocrVerfiyService, plcPool, tcpServer);
} }
/// <summary> /// <summary>
@ -43,10 +68,7 @@ namespace SlnMesnac.WPF.ViewModel
/// <exception cref="NotImplementedException"></exception> /// <exception cref="NotImplementedException"></exception>
private void Reset() private void Reset()
{ {
// TODO 复位PLC放行 logoBusiness.Pass();
// 复位报警灯
gunHelper.SendData("OK");
} }
/// <summary> /// <summary>

Loading…
Cancel
Save