using System;
using HighWayIot.Log4net;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
namespace HighWayIot.Plc
{
public class PlcConnect
{
private static LogHelper logHelper = LogHelper.Instance;
///
/// 静态懒加载MelsecMcNet
///
private static readonly MelsecMcNet MelsecInstance = new PlcConnect().CreateAb();
private PlcConnect()
{
}
///
/// 初始化三菱的服务器
///
///
private MelsecMcNet CreateAb()
{
string Ip = "192.168.0.7";
MelsecMcNet plc = new MelsecMcNet();
try
{
plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(Ip, 2001)
{
ConnectTimeOut = 1000, // 连接超时时间,单位毫秒
SleepTime = 0,
SocketKeepAliveTime = -1,
IsPersistentConnection = true,
};
var a = plc.ConnectServer();
logHelper.Info($"Plc连接 信息:[{a.Message}] 是否成功:[{a.IsSuccess.ToString()}] 错误代码:[{a.ErrorCode}]");
}
catch (Exception ex)
{
logHelper.Error("初始化PLC服务器发生错误!", ex);
}
//string s = IsConnect ? "成功" : "失败";
//logHelper.Info($"PLC连接:{s}");
return plc;
}
/////
///// plc 是不是保持链接
/////
//public static bool IsConnect
//{
// get
// {
// if (MelsecInstance == null) return false;
// var result = MelsecInstance.ReadPlcType();
// logHelper.Info($"PLC型号:{result.Content}");
// return result.IsSuccess;
// }
//}
public static int Test()
{
return MelsecInstance.ReadInt16("D1").Content;
}
///
/// 写入数据
///
/// 地址
/// 值
/// 数据类型
///
public static OperateResult PlcWrite(string address, object value, DataTypeEnum type)
{
var result = new OperateResult() { IsSuccess = false };
try
{
switch (type)
{
case DataTypeEnum.Bool:
result = MelsecInstance.Write(address, Convert.ToBoolean(value));
break;
//case DataTypeEnum.Byte:
// result = Instance.Write(address, Convert.ToByte(value));
// break;
case DataTypeEnum.Int16:
result = MelsecInstance.Write(address, Convert.ToInt16(value));
break;
case DataTypeEnum.UInt16:
result = MelsecInstance.Write(address, Convert.ToUInt16(value));
break;
case DataTypeEnum.Int32:
result = MelsecInstance.Write(address, Convert.ToInt32(value));
break;
case DataTypeEnum.UInt32:
result = MelsecInstance.Write(address, Convert.ToUInt32(value));
break;
case DataTypeEnum.Int64:
result = MelsecInstance.Write(address, Convert.ToInt64(value));
break;
case DataTypeEnum.UInt64:
result = MelsecInstance.Write(address, Convert.ToUInt64(value));
break;
case DataTypeEnum.Float:
result = MelsecInstance.Write(address, Convert.ToSingle(value));
break;
case DataTypeEnum.Double:
result = MelsecInstance.Write(address, Convert.ToDouble(value));
break;
default:
throw new ArgumentException($"Unsupported data type: {type}");
}
logHelper.PlcLog($"Read address:[{address}] value:[{value}] type:[{type.ToString()}] result:[{result.IsSuccess}]");
}
catch (Exception ex)
{
logHelper.Error("PLC写入数据发生错误!", ex);
}
return result;
}
///
/// 读取数据 使用泛型
///
/// 读取类型
/// 地址
/// 数据类型
///
public static T PlcRead(string address, DataTypeEnum type)
{
T result = default;
try
{
result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T));
}
catch(Exception ex)
{
logHelper.Error($"传入类型有误!", ex);
}
return result;
}
///
/// 读取数据 不使用泛型
///
/// 读取类型
/// 地址
/// 数据类型
///
public static object PlcRead(string address, DataTypeEnum type)
{
object result = default;
var oResult = new OperateResult