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

112 lines
3.4 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 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;
1 month ago
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;
3 months ago
}
3 months ago
1 month ago
4 weeks ago
1 month 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 months ago
}
4 weeks ago
3 months ago
}