1
0
Fork 0
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.

336 lines
12 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using HighWayIot.Log4net;
using HslCommunication;
using HslCommunication.Profinet.Melsec;
namespace HighWayIot.Plc
{
public class PlcConnect
{
private static LogHelper logHelper = LogHelper.Instance;
/// <summary>
/// 静态懒加载MelsecMcNet
/// </summary>
private static readonly MelsecMcNet MelsecInstance = new PlcConnect().CreateAb();
private PlcConnect()
{
}
/// <summary>
/// 初始化三菱的服务器
/// </summary>
/// <returns></returns>
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;
}
///// <summary>
///// plc 是不是保持链接
///// </summary>
//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;
}
/// <summary>
/// 写入数据
/// </summary>
/// <param name="address">地址</param>
/// <param name="value">值</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
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;
}
/// <summary>
/// 读取数据 使用泛型
/// </summary>
/// <typeparam name="T">读取类型</typeparam>
/// <param name="address">地址</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public static T PlcRead<T>(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;
}
/// <summary>
/// 读取数据 不使用泛型
/// </summary>
/// <typeparam name="T">读取类型</typeparam>
/// <param name="address">地址</param>
/// <param name="type">数据类型</param>
/// <returns></returns>
public static object PlcRead(string address, DataTypeEnum type)
{
object result = default;
var oResult = new OperateResult<object>() { IsSuccess = false, Content = null };
try
{
switch (type)
{
case DataTypeEnum.Bool:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadBool(address), typeof(OperateResult<bool>));
break;
case DataTypeEnum.Int16:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadInt16(address), typeof(OperateResult<short>));
break;
case DataTypeEnum.UInt16:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadUInt16(address), typeof(OperateResult<ushort>));
break;
case DataTypeEnum.Int32:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadInt32(address), typeof(OperateResult<int>));
break;
case DataTypeEnum.UInt32:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadUInt32(address), typeof(OperateResult<uint>));
break;
case DataTypeEnum.Int64:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadInt64(address), typeof(OperateResult<long>));
break;
case DataTypeEnum.UInt64:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadUInt64(address), typeof(OperateResult<ulong>));
break;
case DataTypeEnum.Float:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadFloat(address), typeof(OperateResult<float>));
break;
case DataTypeEnum.Double:
oResult = (OperateResult<object>)Convert.ChangeType(MelsecInstance.ReadDouble(address), typeof(OperateResult<double>));
break;
default:
throw new ArgumentException($"Unsupported data type: {type}");
}
result = oResult.Content;
logHelper.PlcLog($"Read address:[{address}] value:[{result}] type:[{type.ToString()}] result:[{oResult.IsSuccess}]");
if (!oResult.IsSuccess)
{
logHelper.Error("读取失败!");
}
}
catch (Exception ex)
{
logHelper.Error("PLC读取数据发生错误", ex);
}
return result;
}
///// <summary>
///// 读取bool
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static bool ReadBool(string address)
//{
// var result = MelsecInstance.ReadBool(address);
// return result.IsSuccess && result.Content;
//}
///// <summary>
///// 读取Int16
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static short ReadInt16(string address)
//{
// OperateResult<short> result = new OperateResult<short>();
// try
// {
// result = MelsecInstance.ReadInt16(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
///// <summary>
///// 读取UInt16
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static ushort ReadUInt16(string address)
//{
// OperateResult<ushort> result = new OperateResult<ushort>();
// try
// {
// result = MelsecInstance.ReadUInt16(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
///// <summary>
///// 读取Int32
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static int ReadInt32(string address)
//{
// OperateResult<int> result = new OperateResult<int>();
// try
// {
// result = MelsecInstance.ReadInt32(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
///// <summary>
///// 读取UInt32
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static uint ReadUInt32(string address)
//{
// OperateResult<uint> result = new OperateResult<uint>();
// try
// {
// result = MelsecInstance.ReadUInt32(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
///// <summary>
///// 读取Int64
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static long ReadInt64(string address)
//{
// OperateResult<long> result = new OperateResult<long>();
// try
// {
// result = MelsecInstance.ReadInt64(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
///// <summary>
///// 读取UInt64
///// </summary>
///// <param name="address"></param>
///// <returns></returns>
//public static ulong ReadUInt64(string address)
//{
// OperateResult<ulong> result = new OperateResult<ulong>();
// try
// {
// result = MelsecInstance.ReadUInt64(address);
// }
// catch (Exception ex)
// {
// logHelper.Error($"PLC读取Int16发生错误address:[{address}]", ex);
// }
// return result.Content;
//}
}
}