|
|
@@ -50,13 +50,31 @@ public static class PlcService
|
|
|
var plcPort = config?.PlcPort ?? 102;
|
|
|
var protocol = config?.PlcProtocol ?? "SiemensS7";
|
|
|
|
|
|
- // 当前默认使用 SiemensS7(S7-200 Smart)
|
|
|
- // 切换到其他协议时,使用 PlcDriverFactory.Create() 创建对应驱动
|
|
|
- _plc = new SiemensS7Net(SiemensPLCS.S200Smart, plcIp)
|
|
|
+ // 解析协议类型
|
|
|
+ if (!Enum.TryParse<PlcProtocol>(protocol, out var plcProtocol))
|
|
|
{
|
|
|
- ConnectTimeOut = 3000,
|
|
|
- ReceiveTimeOut = 3000
|
|
|
- };
|
|
|
+ plcProtocol = PlcProtocol.SiemensS7;
|
|
|
+ Log.Warning("未知的 PLC 协议 '{Protocol}',使用默认 SiemensS7", protocol);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 PlcDriverFactory 创建对应驱动
|
|
|
+ _driver = PlcDriverFactory.Create(plcProtocol, plcIp, plcPort);
|
|
|
+
|
|
|
+ // 对于 SiemensS7,同时保留 _plc 引用以保持向后兼容
|
|
|
+ if (plcProtocol == PlcProtocol.SiemensS7 && _driver is SiemensS7Driver siemensDriver)
|
|
|
+ {
|
|
|
+ // 使用反射获取内部的 SiemensS7Net 实例(如果有)
|
|
|
+ // 或者直接使用驱动接口
|
|
|
+ _plc = new SiemensS7Net(SiemensPLCS.S200Smart, plcIp)
|
|
|
+ {
|
|
|
+ ConnectTimeOut = 3000,
|
|
|
+ ReceiveTimeOut = 3000
|
|
|
+ };
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ _plc = null; // 非西门子协议时不使用 _plc
|
|
|
+ }
|
|
|
|
|
|
Log.Information("PLC 服务初始化完成,协议: {Protocol}, IP: {Ip}:{Port}", protocol, plcIp, plcPort);
|
|
|
}
|
|
|
@@ -71,7 +89,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<bool> ConnectAsync()
|
|
|
{
|
|
|
- if (_plc == null)
|
|
|
+ if (_plc == null && _driver == null)
|
|
|
{
|
|
|
Log.Warning("PLC 未初始化");
|
|
|
return false;
|
|
|
@@ -79,7 +97,15 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.ConnectServerAsync();
|
|
|
+ HslCommunication.OperateResult result;
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ result = await _driver.ConnectAsync();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ result = await _plc!.ConnectServerAsync();
|
|
|
+ }
|
|
|
_isConnected = result.IsSuccess;
|
|
|
if (_isConnected)
|
|
|
{
|
|
|
@@ -104,6 +130,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static void Disconnect()
|
|
|
{
|
|
|
+ _driver?.Disconnect();
|
|
|
_plc?.ConnectClose();
|
|
|
_isConnected = false;
|
|
|
Log.Information("PLC 已断开连接");
|
|
|
@@ -114,7 +141,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<float> ReadFloatAsync(string address)
|
|
|
{
|
|
|
- if (_plc == null || !_isConnected)
|
|
|
+ if (!_isConnected)
|
|
|
{
|
|
|
Log.Warning("PLC 未连接,无法读取数据");
|
|
|
return 0f;
|
|
|
@@ -122,8 +149,17 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.ReadFloatAsync(address);
|
|
|
- return result.IsSuccess ? result.Content : 0f;
|
|
|
+ // 优先使用 driver
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ var result = await _driver.ReadFloatAsync(address);
|
|
|
+ return result.IsSuccess ? result.Content : 0f;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 回退到 _plc(向后兼容)
|
|
|
+ if (_plc == null) return 0f;
|
|
|
+ var plcResult = await _plc.ReadFloatAsync(address);
|
|
|
+ return plcResult.IsSuccess ? plcResult.Content : 0f;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -137,7 +173,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<bool> WriteFloatAsync(string address, float value)
|
|
|
{
|
|
|
- if (_plc == null || !_isConnected)
|
|
|
+ if (!_isConnected)
|
|
|
{
|
|
|
Log.Warning("PLC 未连接,无法写入数据");
|
|
|
return false;
|
|
|
@@ -145,8 +181,14 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.WriteAsync(address, value);
|
|
|
- return result.IsSuccess;
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ var result = await _driver.WriteFloatAsync(address, value);
|
|
|
+ return result.IsSuccess;
|
|
|
+ }
|
|
|
+ if (_plc == null) return false;
|
|
|
+ var plcResult = await _plc.WriteAsync(address, value);
|
|
|
+ return plcResult.IsSuccess;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -160,7 +202,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<bool> ReadBoolAsync(string address)
|
|
|
{
|
|
|
- if (_plc == null || !_isConnected)
|
|
|
+ if (!_isConnected)
|
|
|
{
|
|
|
Log.Warning("PLC 未连接,无法读取数据");
|
|
|
return false;
|
|
|
@@ -168,8 +210,14 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.ReadBoolAsync(address);
|
|
|
- return result.IsSuccess && result.Content;
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ var result = await _driver.ReadBoolAsync(address);
|
|
|
+ return result.IsSuccess && result.Content;
|
|
|
+ }
|
|
|
+ if (_plc == null) return false;
|
|
|
+ var plcResult = await _plc.ReadBoolAsync(address);
|
|
|
+ return plcResult.IsSuccess && plcResult.Content;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -183,7 +231,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<bool> WriteBoolAsync(string address, bool value)
|
|
|
{
|
|
|
- if (_plc == null || !_isConnected)
|
|
|
+ if (!_isConnected)
|
|
|
{
|
|
|
Log.Warning("PLC 未连接,无法写入数据");
|
|
|
return false;
|
|
|
@@ -191,8 +239,14 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.WriteAsync(address, value);
|
|
|
- return result.IsSuccess;
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ var result = await _driver.WriteBoolAsync(address, value);
|
|
|
+ return result.IsSuccess;
|
|
|
+ }
|
|
|
+ if (_plc == null) return false;
|
|
|
+ var plcResult = await _plc.WriteAsync(address, value);
|
|
|
+ return plcResult.IsSuccess;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -206,7 +260,7 @@ public static class PlcService
|
|
|
/// </summary>
|
|
|
public static async Task<byte[]?> ReadBytesAsync(string address, ushort length)
|
|
|
{
|
|
|
- if (_plc == null || !_isConnected)
|
|
|
+ if (!_isConnected)
|
|
|
{
|
|
|
Log.Warning("PLC 未连接,无法读取数据");
|
|
|
return null;
|
|
|
@@ -214,8 +268,14 @@ public static class PlcService
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- var result = await _plc.ReadAsync(address, length);
|
|
|
- return result.IsSuccess ? result.Content : null;
|
|
|
+ if (_driver != null)
|
|
|
+ {
|
|
|
+ var result = await _driver.ReadBytesAsync(address, length);
|
|
|
+ return result.IsSuccess ? result.Content : null;
|
|
|
+ }
|
|
|
+ if (_plc == null) return null;
|
|
|
+ var plcResult = await _plc.ReadAsync(address, length);
|
|
|
+ return plcResult.IsSuccess ? plcResult.Content : null;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
@@ -254,6 +314,9 @@ public static class PlcService
|
|
|
public static void Dispose()
|
|
|
{
|
|
|
Disconnect();
|
|
|
+ _driver?.Dispose();
|
|
|
+ _driver = null;
|
|
|
+ _plc?.Dispose();
|
|
|
_plc = null;
|
|
|
}
|
|
|
}
|