using S7.Net;
using S7.Net.Types;
using SamplePre.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SamplePre.Communication
{
public class S7PlcCommunicator : ICommunication, IDisposable
{
public Plc _plc;
private readonly string _ipAddress;
private readonly int _rack;
private readonly int _slot;
private readonly CpuType _cpuType;
///
/// 是否连接
///
private bool _isConnected = false;
///
/// 是否连接
///
public bool IsConnected
{
get => _isConnected;
set
{
if (_isConnected != value)
{
_isConnected = value;
ConnectionStatusChanged?.Invoke(value);
}
}
}
///
/// 连接状态变化事件
///
public event Action ConnectionStatusChanged;
///
/// 构造函数
///
/// PLC型号
/// IP地址
/// 机架号
/// 插槽号
public S7PlcCommunicator(CpuType cpuType, string ipAddress, int rack = 0, int slot = 1)
{
_cpuType = cpuType;
_ipAddress = ipAddress;
_rack = rack;
_slot = slot;
_plc = new Plc(cpuType, ipAddress, (short)rack, (short)slot);
}
///
/// 连接PLC
///
/// 是否连接成功
public bool Connect()
{
try
{
if (IsConnected)
{
LoggerHelper.Logger.Info("已经处于连接状态!");
return true;
}
// 如果之前的连接对象已释放,创建新的
if (_plc == null)
{
_plc = new Plc(_cpuType, _ipAddress, (short)_rack, (short)_slot);
}
_plc.Open();
bool connected = _plc.IsConnected;
LoggerHelper.Logger.Info(connected ? "PLC连接成功" : "PLC连接失败");
IsConnected = connected;
return connected;
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"连接PLC失败: {ex.Message}");
IsConnected = false;
return false;
}
}
///
/// 断开连接
///
public void Disconnect()
{
try
{
if (IsConnected)
{
_plc.Close();
IsConnected = false;
LoggerHelper.Logger.Info("PLC连接已断开");
}
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"断开PLC连接失败: {ex.Message}");
}
}
///
/// 写入数组数据
///
///
///
///
///
public bool WriteArraysBtye(int Dbno, int start, T[] bytes)
{
try
{
if (!IsConnected && !Connect())
{
return false;
}
byte[] data = bytes.Cast().ToArray();
_plc.WriteBytes(DataType.DataBlock, Dbno, start, data);
return true;
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"写入数组失败: {ex.Message}");
return false;
}
}
///
/// 读取数组
///
///
public ushort[] ReadArraysShort(int Dbno,int start,int lenth)
{
try
{
if (!IsConnected && !Connect())
{
return null;
}
//public object? Read(DataType dataType, int db, int startByteAdr, VarType varType, int varCount, byte bitAdr = 0)
ushort[] datas = _plc.Read(DataType.DataBlock, Dbno, start, VarType.Word, lenth) as ushort[];
return datas;
}
catch (Exception ex)
{
LoggerHelper.Logger.Info($"读取数组失败: {ex.Message}");
return null;
}
}
///
/// 写入数据
///
/// 地址(如"M0.0", "DB1.DBW2")
/// 要写入的值
/// 是否写入成功
public bool WriteSingleData(string address, object value)
{
try
{
if (!IsConnected && !Connect())
{
return false;
}
_plc.Write(address, value);
Console.WriteLine($"写入 {address} 成功: {value}");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"写入 {address} 失败: {ex.Message}");
return false;
}
}
///
/// 释放资源
///
public void Dispose()
{
// 释放托管资源
Disconnect();
//_plc?.Dispose();
_plc = null;
}
~S7PlcCommunicator()
{
Dispose();
}
}
}