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
{
///
/// PLC基础工具类
///
public class PlcDeltaTool
{
#region 字段
///
/// 服务端连接IP
///
private string _ipaddr = "192.168.1.5";
///
/// 服务端连接端口
///
private int _port = 502;
///
/// 站号
///
private byte _slaveNo = 0x01;
private DeltaTcpNet _client = null;
///
/// 连接超时时间。单位秒
///
private int _connecttimeout = 10;
///
/// 保持活跃时间。单位秒
///
private int _keepalive = 30;
///
/// 连接状态
///
private ConnectState _connect_state = ConnectState.NOCONNECT;
///
/// 分四次读D寄存器,每次读取的长度(字节),如果是零,则不读取
///
private int[] _read_d_reg_counts = { 0, 0, 0, 0 };
///
/// 分四次读M寄存器,每次读取的长度(字节),如果是零,则不读取
///
private int[] _read_m_reg_counts = { 0, 0, 0, 0 };
///
/// 分四次读C寄存器,每次读取的长度(字节),如果是零,则不读取
///
private int[] _read_c_reg_counts = { 0, 0, 0, 0 };
#endregion 字段
///
/// 结构体
///
/// 服务端连接IP
/// 服务端连接端口
/// 服务端连接站号
/// 连接超时时间。单位秒
/// 保持活跃时间。单位秒
public PlcDeltaTool(string ipaddr, int port, byte slaveNo, int connecttimeout, int keepalive)
{
_ipaddr = ipaddr;
_port = port;
_slaveNo = slaveNo;
_connecttimeout = connecttimeout;
_keepalive = keepalive;
}
#region 属性
///
/// 服务端连接IP
///
public string IpAddr
{
get
{
return _ipaddr;
}
set
{
_ipaddr = value;
}
}
///
/// 服务端连接端口
///
public int Port
{
get
{
return _port;
}
set
{
_port = value;
}
}
///
/// 站号
///
public byte SlaveNo
{
get
{
return _slaveNo;
}
set
{
_slaveNo = value;
}
}
///
/// 连接超时时间。单位秒
///
public int ConnectTimeOut
{
get
{
return _connecttimeout;
}
set
{
_connecttimeout = value;
}
}
///
/// 保持活跃时间。单位秒
///
public int KeepAlive
{
get
{
return _keepalive;
}
set
{
_keepalive = value;
}
}
///
/// 与PLC连接状态
///
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;
}
///
/// 客户端连接台达PLC
///
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");
}
}
///
/// 客户端断开台达PLC连接
///
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");
}
}
///
/// 读取D寄存器信息
///
/// 读取开始地址
/// 读取长度,最小1,最大100
///
public byte[] GetDWordsResult(string startaddr,ushort length)
{
byte[] results = null;
if (length > 0)
{
results = new byte[length];
OperateResult optRst = _client.Read(startaddr, length);
if(optRst.IsSuccess)
{
results = optRst.Content;
}
Int16 rst= _client.ReadInt16(startaddr).Content;
}
return results;
}
}
}