扫码之后给PLC下发产品型号
This commit is contained in:
parent
50acb6a1b4
commit
60c348cba6
@ -18,6 +18,7 @@ namespace RIZO.Infrastructure.Helper
|
||||
{
|
||||
private SiemensS7Net _plc;
|
||||
private bool _isConnected = false;
|
||||
private string IP;
|
||||
|
||||
/// <summary>
|
||||
/// 连接状态
|
||||
@ -33,47 +34,73 @@ namespace RIZO.Infrastructure.Helper
|
||||
_plc = new SiemensS7Net(SiemensPLCS.S1500)
|
||||
{
|
||||
IpAddress = ip,
|
||||
|
||||
Port = 102
|
||||
};
|
||||
IP = ip;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 连接PLC
|
||||
/// 连接PLC(修正版)
|
||||
/// </summary>
|
||||
public async Task<bool> ConnectAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
int maxRetries = 3;
|
||||
int retryDelayMs = 2000;
|
||||
int attempt = 0;
|
||||
|
||||
while (attempt < maxRetries)
|
||||
// 如果已经连接,直接返回成功
|
||||
if (_isConnected && _plc != null)
|
||||
return true;
|
||||
|
||||
for (int attempt = 1; attempt <= maxRetries; attempt++)
|
||||
{
|
||||
attempt++;
|
||||
|
||||
var result = await Task.Run(() => _plc.ConnectServer());
|
||||
_isConnected = result.IsSuccess;
|
||||
if (!_isConnected)
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} - IP: {IP}");
|
||||
|
||||
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败");
|
||||
var result = await Task.Run(() => _plc.ConnectServer(), cancellationToken);
|
||||
_isConnected = result.IsSuccess;
|
||||
|
||||
if (_isConnected)
|
||||
{
|
||||
Console.WriteLine($"PLC连接成功 - IP: {IP}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 失败: {result.Message},IP: {IP}");
|
||||
|
||||
// 如果不是最后一次尝试,则等待后重试
|
||||
if (attempt < maxRetries)
|
||||
{
|
||||
Console.WriteLine($"{retryDelayMs / 1000}秒后重试...");
|
||||
await Task.Delay(retryDelayMs, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("IP: {IP} 连接操作已取消");
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"连接尝试 {attempt}/{maxRetries} 发生异常: {ex.Message},IP: {IP}");
|
||||
|
||||
if (attempt < maxRetries)
|
||||
{
|
||||
Console.WriteLine($"{retryDelayMs / 1000}秒后重试...");
|
||||
await Task.Delay(retryDelayMs, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("无法建立PLC连接,请检查网络和设备状态");
|
||||
_plc?.ConnectClose();
|
||||
_plc = null;
|
||||
return _isConnected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 所有重试都失败了
|
||||
Console.WriteLine($"无法建立 {IP} PLC连接,请检查网络和设备状态");
|
||||
_plc?.ConnectClose();
|
||||
_isConnected = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开连接
|
||||
/// </summary>
|
||||
|
||||
@ -196,5 +196,13 @@ namespace RIZO.Admin.WebApi.Controllers.Mes.WorkOrderInfo
|
||||
var response = _WorkOrderService.QueryWorkOrderToDay(parm);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
//TODO 扫码之后给PLC下发产品型号
|
||||
[HttpGet("scan_product_model")]
|
||||
public IActionResult ScanProductModel( int scanCode)
|
||||
{
|
||||
var response = _WorkOrderService.ScanProductModel(scanCode).;
|
||||
return SUCCESS(response);
|
||||
|
||||
}
|
||||
}
|
||||
@ -31,5 +31,7 @@ namespace RIZO.Service.Mes.IMesService.WorkOrderInfo
|
||||
List<WorkOrder> QueryWorkOrderMonth();
|
||||
|
||||
ApiResult QueryWorkOrderToDay(WorkOrderTodayDto parm);
|
||||
|
||||
Task ScanProductModel(int scanCode);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,7 +2,11 @@ using Aliyun.OSS;
|
||||
using Infrastructure.Attribute;
|
||||
using Infrastructure.Extensions;
|
||||
using Infrastructure.Model;
|
||||
using MDM.Services.Material;
|
||||
using MDM.Services.Process;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.Extensions.Options;
|
||||
using RIZO.Infrastructure.Helper;
|
||||
using RIZO.Model.Mes.Dto.WorkOrderInfo;
|
||||
using RIZO.Model.Mes.MasterData;
|
||||
using RIZO.Model.Mes.WorkOrderInfo;
|
||||
@ -10,8 +14,6 @@ using RIZO.Repository;
|
||||
using RIZO.Service.Mes.IMesService.MasterData;
|
||||
using RIZO.Service.Mes.IMesService.WorkOrderInfo;
|
||||
using RIZO.Service.Mes.MasterData;
|
||||
using MDM.Services.Process;
|
||||
using MDM.Services.Material;
|
||||
using SqlSugar;
|
||||
using SqlSugar.Extensions;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
@ -27,6 +29,12 @@ namespace RIZO.Service.Mes.WorkOrderInfo
|
||||
private WorkOrderItemService workOrderItemService = new WorkOrderItemService();
|
||||
private MaterialListService materialListService = new MaterialListService();
|
||||
private ProcessRoutingService processRoutingService = new ProcessRoutingService();
|
||||
|
||||
private readonly PlcAddress _plcAddress;
|
||||
public WorkOrderService(IOptions<OptionsSetting> options)
|
||||
{
|
||||
_plcAddress = options.Value.plcAddress;
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询工单主表列表
|
||||
/// </summary>
|
||||
@ -440,5 +448,27 @@ namespace RIZO.Service.Mes.WorkOrderInfo
|
||||
return ApiResult.Error(ex.Message);
|
||||
}
|
||||
}
|
||||
private SiemensS7Helper s7Helper;
|
||||
public async Task ScanProductModel(int ScanProductModelId)
|
||||
{
|
||||
using(s7Helper = new SiemensS7Helper(_plcAddress.IP))
|
||||
{
|
||||
if (!s7Helper.IsConnected)
|
||||
{
|
||||
var isConnected = await s7Helper.ConnectAsync();
|
||||
if (!isConnected)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await s7Helper.WriteIntAsync(_plcAddress.Write.ScanProductModelId, (short)ScanProductModelId);
|
||||
await s7Helper.WriteBoolAsync(_plcAddress.Write.ScanOk, true);
|
||||
await s7Helper.WriteBoolAsync(_plcAddress.Write.ScanNg, false);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -64,10 +64,13 @@ namespace RIZO.Service.PLCbackTask
|
||||
// 使用内部取消源停止循环,而不是直接return
|
||||
_internalCts.Cancel();
|
||||
break; // 跳出循环,但会继续执行finally块和Dispose
|
||||
}else if(ReadHeartBeat != null && ReadHeartBeat.Value)
|
||||
{
|
||||
await s7Helper.WriteBoolAsync(_plcAddress.Write.WriteHeartBeat, true);
|
||||
}
|
||||
|
||||
//TODO :1. 轮询出站请求
|
||||
bool? OutStationAsk = await s7Helper.ReadBoolAsync(_plcAddress.Read.OutStationAsk);
|
||||
//TODO :1. 轮询出站请求
|
||||
bool? OutStationAsk = await s7Helper.ReadBoolAsync(_plcAddress.Read.OutStationAsk);
|
||||
if (OutStationAsk != null && OutStationAsk.Value)
|
||||
{
|
||||
//TODO : 2.读取业务数据
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user