|
|
|
@ -48,9 +48,6 @@ namespace HighWayIot.Plc
|
|
|
|
|
logHelper.Error("初始化PLC服务器发生错误!", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//string s = IsConnect ? "成功" : "失败";
|
|
|
|
|
//logHelper.Info($"PLC连接:{s}");
|
|
|
|
|
|
|
|
|
|
return plc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -62,18 +59,12 @@ namespace HighWayIot.Plc
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (MelsecInstance == null) return false;
|
|
|
|
|
var result = MelsecInstance.ReadPlcType();
|
|
|
|
|
logHelper.Info($"PLC型号 信息:[{result.Message}] 内容:[{result.Message}]");
|
|
|
|
|
return result.IsSuccess;
|
|
|
|
|
var result = MelsecInstance.IpAddress;
|
|
|
|
|
logHelper.Info($"PLCIP:[{result}]");
|
|
|
|
|
return !string.IsNullOrEmpty(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int Test()
|
|
|
|
|
{
|
|
|
|
|
var s = IsConnect;
|
|
|
|
|
return MelsecInstance.ReadInt16("D1").Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 写入数据
|
|
|
|
|
/// </summary>
|
|
|
|
@ -131,210 +122,220 @@ namespace HighWayIot.Plc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取数据 使用泛型
|
|
|
|
|
/// 读取bool
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">读取类型</typeparam>
|
|
|
|
|
/// <param name="address">地址</param>
|
|
|
|
|
/// <param name="type">数据类型</param>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T PlcRead<T>(string address, DataTypeEnum type)
|
|
|
|
|
public static bool ReadBool(string address)
|
|
|
|
|
{
|
|
|
|
|
T result = default;
|
|
|
|
|
OperateResult<bool> result = new OperateResult<bool>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T));
|
|
|
|
|
result = MelsecInstance.ReadBool(address);
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error($"传入类型有误!", ex);
|
|
|
|
|
logHelper.Error($"PLC读取Bool发生错误!address:[{address}]", ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return result.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取数据 不使用泛型
|
|
|
|
|
/// 读取Int16
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">读取类型</typeparam>
|
|
|
|
|
/// <param name="address">地址</param>
|
|
|
|
|
/// <param name="type">数据类型</param>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static object PlcRead(string address, DataTypeEnum type)
|
|
|
|
|
public static short ReadInt16(string address)
|
|
|
|
|
{
|
|
|
|
|
object result = default;
|
|
|
|
|
var oResult = new OperateResult<object>() { IsSuccess = false, Content = null };
|
|
|
|
|
OperateResult<short> result = new OperateResult<short>();
|
|
|
|
|
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("读取失败!");
|
|
|
|
|
}
|
|
|
|
|
result = MelsecInstance.ReadInt16(address);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error("PLC读取数据发生错误!", ex);
|
|
|
|
|
logHelper.Error($"PLC读取Int16发生错误!address:[{address}]", ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return result.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///// <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>
|
|
|
|
|
/// 读取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 default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return 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>
|
|
|
|
|
/// 读取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 default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
/// 读取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 default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
/// 读取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 default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
/// 读取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 default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
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>
|
|
|
|
|
/// 读取Float
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static float ReadFloat(string address)
|
|
|
|
|
{
|
|
|
|
|
OperateResult<float> result = new OperateResult<float>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = MelsecInstance.ReadFloat(address);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error($"PLC读取Float发生错误!address:[{address}]", ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
//}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取Double
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="address"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static double ReadDouble(string address)
|
|
|
|
|
{
|
|
|
|
|
OperateResult<double> result = new OperateResult<double>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = MelsecInstance.ReadDouble(address);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
logHelper.Error($"PLC读取Double发生错误!address:[{address}]", ex);
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
return result.Content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|