You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CaiQie/Tool/PlcConnect.cs

121 lines
3.7 KiB
C#

3 months ago
using System;
3 weeks ago
using System.Management.Instrumentation;
3 months ago
2 months ago
using HslCommunication;
3 months ago
using HslCommunication.LogNet;
3 months ago
using HslCommunication.Profinet.AllenBradley;
using HslCommunication.Profinet.Siemens;
2 weeks ago
using NewLife;
2 months ago
using Newtonsoft.Json;
3 months ago
namespace Tool
{
public class PlcConnect
{
3 weeks ago
private static ILogNet logNet = ILogNetFactory.GetLogNet;
3 months ago
private static readonly Lazy<AllenBradleyNet> lazy = new Lazy<AllenBradleyNet>(() => new PlcConnect().CreateAb());
public static AllenBradleyNet Instance => lazy.Value;
private PlcConnect()
{
3 months ago
3 months ago
}
3 months ago
3 months ago
private AllenBradleyNet CreateAb()
{
var rfidSetting = RfidSetting.Current;
1 month ago
AllenBradleyNet plc = new AllenBradleyNet();
plc.Slot = 0;
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(rfidSetting.PlcIp, 44818)
{
3 days ago
ConnectTimeOut = 1000, // 连接超时时间,单位毫秒
1 month ago
SleepTime = 0,
SocketKeepAliveTime = -1,
IsPersistentConnection = true,
};
return plc;
3 months ago
}
2 months ago
1 month ago
3 weeks ago
1 month ago
public OperateResult Write(string db, string value)
2 months ago
{
return Instance.Write(db, value);
}
3 weeks ago
/// <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;
}
2 weeks ago
public static OperateResult WriteTag(string address, UInt32 num)
{
2 weeks ago
OperateResult result = Instance.WriteTag(address, 0xC4, num.GetBytes());
2 weeks ago
logNet.WriteInfo("打印日志", $"write 地址[{address}] value:[{num.ToString()}] type:[Tag] result:[{result.ToJsonString()}]");
2 weeks ago
return result;
2 weeks ago
}
3 months ago
}
3 weeks ago
3 months ago
}