|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
using System.Threading;
|
|
|
using System.Threading.Tasks;
|
|
|
using HslCommunication;
|
|
|
using HslCommunication.Profinet.Delta;
|
|
|
using Module.Common;
|
|
|
using Module.DB.Model;
|
|
|
using Module.DB.SQLServerDAL;
|
|
|
using Newtonsoft.Json;
|
|
|
using RailSee.RealTimeDBO;
|
|
|
using RailSee.LogService;
|
|
|
|
|
|
namespace Module.Plc.Profinet.Tool
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// PLC基础工具类
|
|
|
/// </summary>
|
|
|
public class PlcDeltaTool
|
|
|
{
|
|
|
#region 字段
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端连接IP
|
|
|
/// </summary>
|
|
|
private string _ipaddr = "192.168.1.5";
|
|
|
/// <summary>
|
|
|
/// 服务端连接端口
|
|
|
/// </summary>
|
|
|
private int _port = 502;
|
|
|
/// <summary>
|
|
|
/// 站号
|
|
|
/// </summary>
|
|
|
private byte _slaveNo = 0x01;
|
|
|
|
|
|
private DeltaTcpNet _client = null;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 连接超时时间。单位秒
|
|
|
/// </summary>
|
|
|
private int _connecttimeout = 10;
|
|
|
/// <summary>
|
|
|
/// 保持活跃时间。单位秒
|
|
|
/// </summary>
|
|
|
private int _keepalive = 30;
|
|
|
/// <summary>
|
|
|
/// 连接状态
|
|
|
/// </summary>
|
|
|
private ConnectState _connect_state = ConnectState.NOCONNECT;
|
|
|
|
|
|
/// <summary>
|
|
|
/// 分四次读D寄存器,每次读取的长度(字节),如果是零,则不读取
|
|
|
/// </summary>
|
|
|
private int[] _read_d_reg_counts = { 0, 0, 0, 0 };
|
|
|
/// <summary>
|
|
|
/// 分四次读M寄存器,每次读取的长度(字节),如果是零,则不读取
|
|
|
/// </summary>
|
|
|
private int[] _read_m_reg_counts = { 0, 0, 0, 0 };
|
|
|
/// <summary>
|
|
|
/// 分四次读C寄存器,每次读取的长度(字节),如果是零,则不读取
|
|
|
/// </summary>
|
|
|
private int[] _read_c_reg_counts = { 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
|
#endregion 字段
|
|
|
|
|
|
/// <summary>
|
|
|
/// 结构体
|
|
|
/// </summary>
|
|
|
/// <param name="ipaddr">服务端连接IP</param>
|
|
|
/// <param name="port">服务端连接端口</param>
|
|
|
/// <param name="slaveNo">服务端连接站号</param>
|
|
|
/// <param name="connecttimeout">连接超时时间。单位秒</param>
|
|
|
/// <param name="keepalive">保持活跃时间。单位秒</param>
|
|
|
public PlcDeltaTool(string ipaddr, int port, byte slaveNo, int connecttimeout, int keepalive)
|
|
|
{
|
|
|
_ipaddr = ipaddr;
|
|
|
_port = port;
|
|
|
_slaveNo = slaveNo;
|
|
|
_connecttimeout = connecttimeout;
|
|
|
_keepalive = keepalive;
|
|
|
}
|
|
|
|
|
|
#region 属性
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端连接IP
|
|
|
/// </summary>
|
|
|
public string IpAddr
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _ipaddr;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_ipaddr = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 服务端连接端口
|
|
|
/// </summary>
|
|
|
public int Port
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _port;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_port = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 站号
|
|
|
/// </summary>
|
|
|
public byte SlaveNo
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _slaveNo;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_slaveNo = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 连接超时时间。单位秒
|
|
|
/// </summary>
|
|
|
public int ConnectTimeOut
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _connecttimeout;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_connecttimeout = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 保持活跃时间。单位秒
|
|
|
/// </summary>
|
|
|
public int KeepAlive
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _keepalive;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_keepalive = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 与PLC连接状态
|
|
|
/// </summary>
|
|
|
public ConnectState ConnectState
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _connect_state;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_connect_state = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public DeltaTcpNet Client
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return _client;
|
|
|
}
|
|
|
set
|
|
|
{
|
|
|
_client = value;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion 属性
|
|
|
|
|
|
|
|
|
public PlcDeltaTool() { }
|
|
|
|
|
|
public PlcDeltaTool(string ipAddr, int port)
|
|
|
{
|
|
|
_ipaddr = ipAddr;
|
|
|
_port = port;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 客户端连接台达PLC
|
|
|
/// </summary>
|
|
|
public void PlcDeltaConnect()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if(_client==null)
|
|
|
{
|
|
|
_client = new DeltaTcpNet(_ipaddr, _port, _slaveNo);
|
|
|
}
|
|
|
if (_client != null)
|
|
|
{
|
|
|
_client = new DeltaTcpNet(_ipaddr, _port, _slaveNo);
|
|
|
_client.Series = DeltaSeries.AS;
|
|
|
PlcDeltaDisConnect();
|
|
|
|
|
|
OperateResult OptRet = _client.ConnectServer();
|
|
|
_connect_state = ConnectState.CONNECTING;
|
|
|
if (OptRet.IsSuccess)
|
|
|
{
|
|
|
_connect_state = ConnectState.CONNECTED;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_connect_state = ConnectState.NOCONNECT;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Log.LogInstance.WriteLog("连接失败" + ex.ToString(), LogType.Run, "PlcLog");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 客户端断开台达PLC连接
|
|
|
/// </summary>
|
|
|
public void PlcDeltaDisConnect()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
OperateResult OptRet = _client.ConnectClose();
|
|
|
if (OptRet.IsSuccess)
|
|
|
{
|
|
|
_connect_state = ConnectState.NOCONNECT;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
Log.LogInstance.WriteLog("断开连接失败" + ex.ToString(), LogType.Run, "PlcLog");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 读取D寄存器信息
|
|
|
/// </summary>
|
|
|
/// <param name="startaddr">读取开始地址</param>
|
|
|
/// <param name="length">读取长度,最小1,最大100</param>
|
|
|
/// <returns></returns>
|
|
|
public byte[] GetDWordsResult(string startaddr,ushort length)
|
|
|
{
|
|
|
byte[] results = null;
|
|
|
if (length > 0)
|
|
|
{
|
|
|
results = new byte[length];
|
|
|
OperateResult<byte[]> optRst = _client.Read(startaddr, length);
|
|
|
|
|
|
if(optRst.IsSuccess)
|
|
|
{
|
|
|
results = optRst.Content;
|
|
|
}
|
|
|
|
|
|
Int16 rst= _client.ReadInt16(startaddr).Content;
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|