94 lines
4.0 KiB
C#
94 lines
4.0 KiB
C#
using System;
|
|
using HHWMS.Web.Support.PLC;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace HHWMS.Web.Support
|
|
{
|
|
public class PLCHelper : IDisposable
|
|
{
|
|
private readonly PlcFactory _plcFactory;
|
|
private IPlcDevice? _currentPlc;
|
|
|
|
public PLCHelper(ILoggerFactory loggerFactory)
|
|
{
|
|
_plcFactory = new PlcFactory(loggerFactory);
|
|
}
|
|
|
|
public bool Connect(PlcType plcType, string ipAddress, byte rack = 0, byte slot = 1)
|
|
{
|
|
_currentPlc?.Dispose();
|
|
_currentPlc = _plcFactory.CreatePlc(plcType);
|
|
return _currentPlc.Connect(ipAddress, rack, slot);
|
|
}
|
|
|
|
#region 基本类型读写
|
|
public short ReadInt16(string address) =>
|
|
_currentPlc?.ReadInt16(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteInt16(string address, short value) =>
|
|
_currentPlc?.WriteInt16(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public int ReadInt32(string address) =>
|
|
_currentPlc?.ReadInt32(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteInt32(string address, int value) =>
|
|
_currentPlc?.WriteInt32(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public long ReadInt64(string address) =>
|
|
_currentPlc?.ReadInt64(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteInt64(string address, long value) =>
|
|
_currentPlc?.WriteInt64(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public float ReadFloat(string address) =>
|
|
_currentPlc?.ReadFloat(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteFloat(string address, float value) =>
|
|
_currentPlc?.WriteFloat(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public double ReadDouble(string address) =>
|
|
_currentPlc?.ReadDouble(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteDouble(string address, double value) =>
|
|
_currentPlc?.WriteDouble(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool ReadBool(string address) =>
|
|
_currentPlc?.ReadBool(address) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteBool(string address, bool value) =>
|
|
_currentPlc?.WriteBool(address, value) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public string ReadString(string address, ushort length) =>
|
|
_currentPlc?.ReadString(address, length) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteString(string address, string value, ushort length) =>
|
|
_currentPlc?.WriteString(address, value, length) ?? throw new InvalidOperationException("PLC未连接");
|
|
#endregion
|
|
|
|
#region 批量读写
|
|
public short[] ReadInt16Array(string address, ushort length) =>
|
|
_currentPlc?.ReadInt16Array(address, length) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteInt16Array(string address, short[] values) =>
|
|
_currentPlc?.WriteInt16Array(address, values) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool[] ReadBoolArray(string address, ushort length) =>
|
|
_currentPlc?.ReadBoolArray(address, length) ?? throw new InvalidOperationException("PLC未连接");
|
|
|
|
public bool WriteBoolArray(string address, bool[] values) =>
|
|
_currentPlc?.WriteBoolArray(address, values) ?? throw new InvalidOperationException("PLC未连接");
|
|
#endregion
|
|
|
|
public void Disconnect()
|
|
{
|
|
_currentPlc?.Disconnect();
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_currentPlc?.Dispose();
|
|
}
|
|
|
|
public bool IsConnected => _currentPlc?.IsConnected ?? false;
|
|
}
|
|
} |