添加HSL,并创建工具类,在IWCS写测试
This commit is contained in:
1 parent
2c178b1810
commit
b0a6dcc035
10 files changed
+1381
-4
No files matched your search
@@ -6,12 +6,12 @@ namespace HHWMS.Web
|
||||
{
|
||||
public abstract class ApiController : BaseApiController
|
||||
{
|
||||
protected JsonResult ApiResult(object data = null, string msg = "success")
|
||||
protected JsonResult ApiResult(object data = null, string msg = "操作成功!")
|
||||
{
|
||||
return new JsonResult(new ApiJsonResult<object> { Data = data, Success = true, Status = true, Msg = msg });
|
||||
}
|
||||
|
||||
protected JsonResult ApiResultError(object data = null, string msg = "faield")
|
||||
protected JsonResult ApiResultError(object data = null, string msg = "操作失败!")
|
||||
{
|
||||
return new JsonResult(new ApiJsonResult<object> { Data = data, Success = false, Status = false, Msg = msg });
|
||||
}
|
||||
|
||||
@@ -4,10 +4,15 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using HHWMS.Web.Models;
|
||||
using HHWMS.Web.Support;
|
||||
using HHWMS.Web.Support.PLC;
|
||||
using HslCommunication;
|
||||
using HslCommunication.Profinet.Siemens;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using WalkingTec.Mvvm.Core;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using HslCommunication.Core;
|
||||
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
@@ -25,12 +30,14 @@ namespace HHWMS.Web
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IMemoryCache _cache;
|
||||
private const int DEBOUNCE_INTERVAL_MS = 1000; // 防抖间隔1秒
|
||||
private readonly PLCHelper _plcHelper;
|
||||
|
||||
public IWCSController(IUnitWork unitWork, IMapper mapper, IMemoryCache cache)
|
||||
public IWCSController(IUnitWork unitWork, IMapper mapper, IMemoryCache cache, PLCHelper plcHelper)
|
||||
{
|
||||
_unitWork = unitWork;
|
||||
_mapper = mapper;
|
||||
_cache = cache;
|
||||
_plcHelper = plcHelper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,6 +108,14 @@ namespace HHWMS.Web
|
||||
private async Task ProcessRequestIn(WcsRequest request)
|
||||
{
|
||||
// TODO: 实现具体的入库处理逻辑
|
||||
var t = 0;
|
||||
var plc = new SiemensS7Net( SiemensPLCS.S1200, "10.211.55.4" );
|
||||
var read = await plc.ReadInt32Async( "V2634" );
|
||||
if (read.IsSuccess)
|
||||
{
|
||||
t = read.Content;
|
||||
}
|
||||
var write = await plc.WriteAsync( "V2634", t + 1 );
|
||||
await Task.Delay(1000); // 模拟异步处理
|
||||
}
|
||||
|
||||
@@ -159,5 +174,116 @@ namespace HHWMS.Web
|
||||
return ApiResult(new { LocationCode = location.Code });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// PLC通信测试接口
|
||||
/// </summary>
|
||||
/// <param name="request">通信参数</param>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CommunicateWithPLC(PlcCommunicationRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
return ApiResultError("请求参数不能为空");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 连接PLC
|
||||
if (!_plcHelper.Connect(PlcType.Siemens, request.IpAddress))
|
||||
{
|
||||
return ApiResultError(msg:"PLC连接失败");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 根据操作类型执行不同的读写操作
|
||||
switch (request.OperationType)
|
||||
{
|
||||
case "ReadInt32":
|
||||
var value = _plcHelper.ReadInt32(request.Address);
|
||||
return ApiResult(new { Value = value });
|
||||
|
||||
case "WriteInt16":
|
||||
if (!short.TryParse(request.Value, out short shortValue))
|
||||
{
|
||||
return ApiResultError(msg: "无效的数值");
|
||||
}
|
||||
var writeResult = _plcHelper.WriteInt16(request.Address, shortValue);
|
||||
return ApiResult(new { Success = writeResult });
|
||||
|
||||
case "ReadBool":
|
||||
var boolValue = _plcHelper.ReadBool(request.Address);
|
||||
return ApiResult(new { Value = boolValue });
|
||||
|
||||
case "WriteBool":
|
||||
if (!bool.TryParse(request.Value, out bool boolWriteValue))
|
||||
{
|
||||
return ApiResultError("无效的布尔值");
|
||||
}
|
||||
var boolWriteResult = _plcHelper.WriteBool(request.Address, boolWriteValue);
|
||||
return ApiResult(new { Success = boolWriteResult });
|
||||
|
||||
case "ReadString":
|
||||
if (!ushort.TryParse(request.StringLength, out ushort length))
|
||||
{
|
||||
return ApiResultError("无效的字符串长度");
|
||||
}
|
||||
var stringValue = _plcHelper.ReadString(request.Address, length);
|
||||
return ApiResult(new { Value = stringValue });
|
||||
|
||||
case "WriteString":
|
||||
if (!ushort.TryParse(request.StringLength, out ushort writeLength))
|
||||
{
|
||||
return ApiResultError("无效的字符串长度");
|
||||
}
|
||||
var stringWriteResult = _plcHelper.WriteString(request.Address, request.Value, writeLength);
|
||||
return ApiResult(new { Success = stringWriteResult });
|
||||
|
||||
default:
|
||||
return ApiResultError("不支持的操作类型");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
// 确保操作完成后断开连接
|
||||
_plcHelper.Disconnect();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ApiResultError($"PLC操作失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// PLC通信请求参数类
|
||||
/// </summary>
|
||||
public class PlcCommunicationRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// PLC的IP地址
|
||||
/// </summary>
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 操作类型(ReadInt16, WriteInt16, ReadBool, WriteBool, ReadString, WriteString)
|
||||
/// </summary>
|
||||
public string OperationType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// PLC地址(例如"DB1.0")
|
||||
/// </summary>
|
||||
public string Address { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 要写入的值(用于写操作)
|
||||
/// </summary>
|
||||
public string Value { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 字符串长度(用于字符串操作)
|
||||
/// </summary>
|
||||
public string StringLength { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user