1
0
Fork 0

feat - plc测试页面

master
SoulStar 4 weeks ago
parent 5c5256c283
commit aa1770307a

@ -42,6 +42,9 @@ namespace HighWayIot.Plc
logHelper.Error("初始化PLC服务器发生错误", ex); logHelper.Error("初始化PLC服务器发生错误", ex);
} }
string s = IsConnect ? "正常" : "异常";
logHelper.Info($"PLC连接{s}");
return plc; return plc;
} }
@ -53,6 +56,7 @@ namespace HighWayIot.Plc
get get
{ {
var result = MelsecInstance.ReadPlcType(); var result = MelsecInstance.ReadPlcType();
logHelper.Info($"PLC型号{result.Content}");
return result.IsSuccess; return result.IsSuccess;
} }
} }
@ -123,9 +127,14 @@ namespace HighWayIot.Plc
public static T PlcRead<T>(string address, DataTypeEnum type) public static T PlcRead<T>(string address, DataTypeEnum type)
{ {
T result = default; T result = default;
try
{
result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T)); result = (T)Convert.ChangeType(PlcRead(address, type), typeof(T));
}
catch(Exception ex)
{
logHelper.Error($"传入类型有误!", ex);
}
return result; return result;
} }

@ -97,7 +97,7 @@ namespace HighWayIot.Winform.Business
case DataTypeEnum.Double: case DataTypeEnum.Double:
return typeof(double); return typeof(double);
default: default:
throw new ArgumentException($"Unsupported data type: {type}"); return default;
} }
} }
} }

@ -36,6 +36,7 @@
<StartupObject /> <StartupObject />
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="HslCommunication, Version=12.1.3.0, Culture=neutral, PublicKeyToken=3d72ad3b6b5ec0e3" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />

@ -59,7 +59,10 @@ namespace HighWayIot.Winform.UserControlPages
/// <param name="e"></param> /// <param name="e"></param>
private void ReadButton_Click(object sender, EventArgs e) private void ReadButton_Click(object sender, EventArgs e)
{ {
var result = PlcConnect.PlcRead(PlcAddress.Text, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); 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> /// <summary>
@ -69,7 +72,10 @@ namespace HighWayIot.Winform.UserControlPages
/// <param name="e"></param> /// <param name="e"></param>
private void WriteButton_Click(object sender, EventArgs e) private void WriteButton_Click(object sender, EventArgs e)
{ {
decimal.TryParse(PlcValue.Text, out decimal value); if(!decimal.TryParse(PlcValue.Text, out decimal value))
{
MessageBox.Show("类型转换错误!");
}
var result = PlcConnect.PlcWrite(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue)); var result = PlcConnect.PlcWrite(PlcAddress.Text, value, (DataTypeEnum)Convert.ToInt32(PlcType.SelectedValue));
bool r = result.IsSuccess; bool r = result.IsSuccess;
PlcShowValue.Text = r.ToString(); PlcShowValue.Text = r.ToString();

Loading…
Cancel
Save