wcs_server_kate_suzhou/WcsMain/EquipOperation/ConnectPLCs.cs

54 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using PlcTool.Siemens;
using WcsMain.Common;
using WcsMain.DataBase.Dao;
using WcsMain.DataBase.TableEntity;
using WcsMain.WcsAttribute.AutoFacAttribute;
namespace WcsMain.EquipOperation;
/// <summary>
/// 连接PLC
/// </summary>
[Component]
public class ConnectPLCs(AppPLCDao pLCDao, AppDBDao dBDao)
{
private readonly AppPLCDao _plcDao = pLCDao;
private readonly AppDBDao _dbDao = dBDao;
/// <summary>
/// 连接PLC
/// </summary>
/// <returns>返回值表示是否继续检测PLC连接状态</returns>
public bool ConnectPlc()
{
/* 查询所有PLC */
List<AppPLC>? plcs = _plcDao.GetDataWithStatus(1);
if (plcs == default)
{
ConsoleLog.Tip("$[异常]PLC 数据查找失败请检查网络是否正常或者是否录入PLC数据");
return true;
}
if (plcs.Count == 0)
{
ConsoleLog.Warning("【警告】您没有录入或者启用任何 PLC");
CommonData.IsConnectPlc = false;
return false;
}
List<AppDB>? dbs = _dbDao.Select();
CommonTool.Siemens = new SiemensS7();
CommonTool.Siemens.SetPlcs(plcs).SetPlcDB(dbs);
var connectResult = CommonTool.Siemens.ConnectPlcs();
if (connectResult.Success)
{
CommonData.IsConnectPlc = true;
}
else
{
CommonData.IsConnectPlc = false;
ConsoleLog.Error($"[异常]{connectResult.Message}");
ConsoleLog.Error($"请检查设备网络后重新启动 WCS ");
}
return true;
}
}