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

122 lines
3.8 KiB
C#

3 months ago
using System;
4 weeks ago
using System.Management.Instrumentation;
3 months ago
3 months ago
using HslCommunication;
3 months ago
using HslCommunication.LogNet;
3 months ago
using HslCommunication.Profinet.AllenBradley;
using HslCommunication.Profinet.Siemens;
3 weeks ago
using NewLife;
3 months ago
using Newtonsoft.Json;
3 months ago
namespace Tool
{
public class PlcConnect
{
4 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;
2 months ago
AllenBradleyNet plc = new AllenBradleyNet();
plc.Slot = 0;
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(rfidSetting.PlcIp, 44818)
{
ConnectTimeOut = 5000, // 连接超时时间,单位毫秒
3 weeks ago
ReceiveTimeOut = 20000, // 接收设备数据反馈的超时时间
2 months ago
SleepTime = 0,
SocketKeepAliveTime = -1,
IsPersistentConnection = true,
};
return plc;
3 months ago
}
3 months ago
2 months ago
4 weeks ago
2 months ago
public OperateResult Write(string db, string value)
3 months ago
{
return Instance.Write(db, value);
}
4 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;
}
3 weeks ago
public static OperateResult WriteTag(string address, UInt32 num)
{
3 weeks ago
OperateResult result = Instance.WriteTag(address, 0xC4, num.GetBytes());
3 weeks ago
logNet.WriteInfo("打印日志", $"write 地址[{address}] value:[{num.ToString()}] type:[Tag] result:[{result.ToJsonString()}]");
3 weeks ago
return result;
3 weeks ago
}
3 months ago
}
4 weeks ago
3 months ago
}