356 lines
11 KiB
C#
356 lines
11 KiB
C#
using System;
|
|
using HslCommunication;
|
|
using HslCommunication.Profinet.Siemens;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace HHWMS.Web.Support.PLC
|
|
{
|
|
public class SiemensPlc : IPlcDevice
|
|
{
|
|
private readonly ILogger _logger;
|
|
private SiemensS7Net? _plc;
|
|
private bool _isConnected;
|
|
|
|
public SiemensPlc(ILogger logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
public bool Connect(string ipAddress, byte rack = 0, byte slot = 1)
|
|
{
|
|
try
|
|
{
|
|
_plc = new SiemensS7Net(SiemensPLCS.S1200, ipAddress)
|
|
{
|
|
Rack = rack,
|
|
Slot = slot
|
|
};
|
|
|
|
OperateResult connect = _plc.ConnectServer();
|
|
_isConnected = connect.IsSuccess;
|
|
|
|
if (_isConnected)
|
|
{
|
|
_logger.LogInformation($"西门子PLC连接成功: {ipAddress}");
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError($"西门子PLC连接失败: {ipAddress}, 错误信息: {connect.Message}");
|
|
}
|
|
|
|
return _isConnected;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"西门子PLC连接异常: {ex.Message}");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void Disconnect()
|
|
{
|
|
if (_isConnected && _plc != null)
|
|
{
|
|
_plc.ConnectClose();
|
|
_isConnected = false;
|
|
_logger.LogInformation("西门子PLC连接已断开");
|
|
}
|
|
}
|
|
|
|
public short ReadInt16(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadInt16(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Int16成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Int16失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteInt16(string address, short value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Int16成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Int16失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public bool ReadBool(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadBool(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Bool成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Bool失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteBool(string address, bool value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Bool成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Bool失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public string ReadString(string address, ushort length)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadString(address, length);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取String成功: 地址={address}, 长度={length}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取String失败: 地址={address}, 长度={length}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteString(string address, string value, ushort length)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value, length);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入String成功: 地址={address}, 长度={length}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入String失败: 地址={address}, 长度={length}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public int ReadInt32(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadInt32(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Int32成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Int32失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteInt32(string address, int value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Int32成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Int32失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public long ReadInt64(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadInt64(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Int64成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Int64失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteInt64(string address, long value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Int64成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Int64失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public float ReadFloat(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadFloat(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Float成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Float失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteFloat(string address, float value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Float成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Float失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public double ReadDouble(string address)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadDouble(address);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Double成功: 地址={address}, 值={result.Content}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Double失败: 地址={address}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteDouble(string address, double value)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, value);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Double成功: 地址={address}, 值={value}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Double失败: 地址={address}, 值={value}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public short[] ReadInt16Array(string address, ushort length)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadInt16(address, length);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Int16数组成功: 地址={address}, 长度={length}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Int16数组失败: 地址={address}, 长度={length}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteInt16Array(string address, short[] values)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, values);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Int16数组成功: 地址={address}, 长度={values.Length}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Int16数组失败: 地址={address}, 长度={values.Length}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
public bool[] ReadBoolArray(string address, ushort length)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.ReadBool(address, length);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"读取Bool数组成功: 地址={address}, 长度={length}");
|
|
return result.Content;
|
|
}
|
|
|
|
var error = $"读取Bool数组失败: 地址={address}, 长度={length}, 错误={result.Message}";
|
|
_logger.LogError(error);
|
|
throw new InvalidOperationException(error);
|
|
}
|
|
|
|
public bool WriteBoolArray(string address, bool[] values)
|
|
{
|
|
CheckConnection();
|
|
|
|
var result = _plc!.Write(address, values);
|
|
if (result.IsSuccess)
|
|
{
|
|
_logger.LogDebug($"写入Bool数组成功: 地址={address}, 长度={values.Length}");
|
|
return true;
|
|
}
|
|
|
|
_logger.LogError($"写入Bool数组失败: 地址={address}, 长度={values.Length}, 错误={result.Message}");
|
|
return false;
|
|
}
|
|
|
|
private void CheckConnection()
|
|
{
|
|
if (!_isConnected || _plc == null)
|
|
{
|
|
throw new InvalidOperationException("PLC未连接");
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
Disconnect();
|
|
_plc?.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
|
|
public bool IsConnected => _isConnected;
|
|
}
|
|
} |