using DMS.Infrastructure.Interfaces.Services; using Microsoft.Extensions.Logging; using NLog; using S7.Net; using S7.Net.Types; namespace DMS.Infrastructure.Services { /// /// S7服务实现类,用于与S7 PLC进行通信 /// public class S7Service : IS7Service { private Plc _plc; private readonly ILogger _logger; public bool IsConnected => _plc?.IsConnected ?? false; public S7Service(ILogger logger) { _logger = logger; } /// /// 异步连接到S7 PLC /// public async Task ConnectAsync(string ipAddress, int port, short rack, short slot, CpuType cpuType) { try { _plc = new Plc(cpuType, ipAddress, (short)port, rack, slot); await _plc.OpenAsync(); _logger.LogInformation($"成功连接到S7 PLC: {ipAddress}:{port}"); } catch (Exception ex) { _logger.LogError(ex, $"连接S7 PLC时发生错误: {ex.Message}"); throw; } } /// /// 异步断开与S7 PLC的连接 /// public async Task DisconnectAsync() { try { if (_plc != null) { _plc.Close(); _logger.LogInformation("已断开S7 PLC连接"); } } catch (Exception ex) { _logger.LogError(ex, $"断开S7 PLC连接时发生错误: {ex.Message}"); throw; } } /// /// 异步读取单个变量的值 /// public async Task ReadVariableAsync(string address) { if (!IsConnected) { throw new InvalidOperationException("PLC未连接"); } try { var dataItem = DataItem.FromAddress(address); // await _plc.ReadMultipleVarsAsync(dataItem); return dataItem.Value; } catch (Exception ex) { _logger.LogError(ex, $"读取变量 {address} 时发生错误: {ex.Message}"); throw; } } /// /// 异步读取多个变量的值 /// public async Task> ReadVariablesAsync(List addresses) { if (!IsConnected) { throw new InvalidOperationException("PLC未连接"); } try { var dataItems = addresses.Select(DataItem.FromAddress).ToList(); await _plc.ReadMultipleVarsAsync(dataItems); var result = new Dictionary(); for (int i = 0; i < addresses.Count; i++) { result[addresses[i]] = dataItems[i].Value; } return result; } catch (Exception ex) { _logger.LogError(ex, $"批量读取变量时发生错误: {ex.Message}"); throw; } } /// /// 异步写入单个变量的值 /// public async Task WriteVariableAsync(string address, object value) { if (!IsConnected) { throw new InvalidOperationException("PLC未连接"); } try { await _plc.WriteAsync(address, value); _logger.LogInformation($"成功写入变量 {address},值: {value}"); } catch (Exception ex) { _logger.LogError(ex, $"写入变量 {address} 时发生错误: {ex.Message}"); throw; } } /// /// 异步写入多个变量的值 /// public async Task WriteVariablesAsync(Dictionary values) { if (!IsConnected) { throw new InvalidOperationException("PLC未连接"); } try { var addresses = values.Keys.ToList(); var dataItems = new List(); foreach (var kvp in values) { var dataItem = DataItem.FromAddress(kvp.Key); dataItem.Value = kvp.Value; dataItems.Add(dataItem); } // await _plc.write(dataItems); _logger.LogInformation($"成功批量写入 {values.Count} 个变量"); } catch (Exception ex) { _logger.LogError(ex, $"批量写入变量时发生错误: {ex.Message}"); throw; } } } }