|
|
|
|
using System;
|
|
|
|
|
using System.Management.Instrumentation;
|
|
|
|
|
|
|
|
|
|
using HslCommunication;
|
|
|
|
|
using HslCommunication.LogNet;
|
|
|
|
|
using HslCommunication.Profinet.AllenBradley;
|
|
|
|
|
using HslCommunication.Profinet.Siemens;
|
|
|
|
|
using NewLife;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
namespace Tool
|
|
|
|
|
{
|
|
|
|
|
public class PlcConnect
|
|
|
|
|
{
|
|
|
|
|
private static ILogNet logNet = ILogNetFactory.GetLogNet;
|
|
|
|
|
private static readonly Lazy<AllenBradleyNet> lazy = new Lazy<AllenBradleyNet>(() => new PlcConnect().CreateAb());
|
|
|
|
|
public static AllenBradleyNet Instance => lazy.Value;
|
|
|
|
|
private PlcConnect()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private AllenBradleyNet CreateAb()
|
|
|
|
|
{
|
|
|
|
|
var rfidSetting = RfidSetting.Current;
|
|
|
|
|
AllenBradleyNet plc = new AllenBradleyNet();
|
|
|
|
|
plc.Slot = 0;
|
|
|
|
|
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(rfidSetting.PlcIp, 44818)
|
|
|
|
|
{
|
|
|
|
|
ConnectTimeOut = 5000, // 连接超时时间,单位毫秒
|
|
|
|
|
ReceiveTimeOut = 10000, // 接收设备数据反馈的超时时间
|
|
|
|
|
SleepTime = 0,
|
|
|
|
|
SocketKeepAliveTime = -1,
|
|
|
|
|
IsPersistentConnection = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return plc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public OperateResult Write(string db, string value)
|
|
|
|
|
{
|
|
|
|
|
return Instance.Write(db, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 写入数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address">地址</param>
|
|
|
|
|
/// <param name="value">值</param>
|
|
|
|
|
/// <param name="type">数据类型</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static OperateResult Write(string address, object value, DataTypeEnum type)
|
|
|
|
|
{
|
|
|
|
|
var result = new OperateResult() { IsSuccess = false };
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case DataTypeEnum.Bool:
|
|
|
|
|
result= Instance.Write(address, Convert.ToBoolean(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Byte:
|
|
|
|
|
result = Instance.Write(address, Convert.ToByte(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int16:
|
|
|
|
|
result = Instance.Write(address, Convert.ToInt16(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.UInt16:
|
|
|
|
|
result = Instance.Write(address, Convert.ToUInt16(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int32:
|
|
|
|
|
result = Instance.Write(address, Convert.ToInt32(value));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.UInt32:
|
|
|
|
|
result = Instance.Write(address, Convert.ToUInt32(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Int64:
|
|
|
|
|
result = Instance.Write(address, Convert.ToInt64(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.UInt64:
|
|
|
|
|
result = Instance.Write(address, Convert.ToUInt64(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Float:
|
|
|
|
|
result = Instance.Write(address, Convert.ToSingle(value));
|
|
|
|
|
break;
|
|
|
|
|
case DataTypeEnum.Double:
|
|
|
|
|
result = Instance.Write(address, Convert.ToDouble(value));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logNet.WriteInfo("打印日志",$"write 地址[{address}] value:[{value.ToString()}] type:[{type.ToString()}] result:[{result.ToJsonString()}]");
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static OperateResult WriteTag(string address, UInt32 num)
|
|
|
|
|
{
|
|
|
|
|
OperateResult result = Instance.WriteTag(address, 0xC4, num.GetBytes());
|
|
|
|
|
logNet.WriteInfo("打印日志", $"write 地址[{address}] value:[{num.ToString()}] type:[Tag] result:[{result.ToJsonString()}]");
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|