1
0
Fork 0

feat - 添加PLC读取类,准备测试

master
SoulStar 4 weeks ago
parent 39d1e0ed6e
commit f30e997935

@ -63,8 +63,5 @@
<ItemGroup>
<Folder Include="Impl\" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -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;
}
}

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HslCommunication" version="12.1.3" targetFramework="net48" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net48" />
</packages>

@ -48,7 +48,7 @@ namespace HighWayIot.Winform.UserControlPages
private void button2_Click(object sender, EventArgs e)
{
var res = PlcConnect.Test();
var res = PlcConnect.IsConnect;
PlcShowValue.Text = res.ToString();
}
@ -59,10 +59,7 @@ namespace HighWayIot.Winform.UserControlPages
/// <param name="e"></param>
private void ReadButton_Click(object sender, EventArgs e)
{
DataTypeEnum type = (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue);
Type t = GeneralUtils.GetTypeByEnum(type);
var result = PlcConnect.PlcRead(PlcAddress.Text, type);
PlcShowValue.Text = Convert.ToDecimal(result).ToString();
}
/// <summary>

Loading…
Cancel
Save