|
@@ -34,33 +34,60 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
|
|
|
#region 私有方法
|
|
|
|
|
|
private void WriteToPlc(object obj)
|
|
|
- {
|
|
|
- foreach (var item in PLCItemList)
|
|
|
+ {//没有连接 plc,直接退出
|
|
|
+ if (!PLCCom.GetInstance().IsConnectPLC())
|
|
|
+ {
|
|
|
+ MessageBox.Show("PLC未连接!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ long id = Convert.ToInt64(obj);
|
|
|
+ var findPlcItem= PLCItemList.FirstOrDefault(l => l.Id == id);
|
|
|
+ if(findPlcItem!=null)
|
|
|
{
|
|
|
- //没有连接 plc,直接退出
|
|
|
- if (!PLCCom.GetInstance().IsConnectPLC())
|
|
|
+ string plcAddr = findPlcItem.PlcAddress;
|
|
|
+ string plcAddType = findPlcItem.PlcAddType;
|
|
|
+ string plcValue = findPlcItem.PlcValue;
|
|
|
+ if (string.IsNullOrEmpty(plcValue))
|
|
|
{
|
|
|
- break;
|
|
|
+ MessageBox.Show("请填写数据!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
+ return;
|
|
|
}
|
|
|
- string plcAddr = item.PlcAddress;
|
|
|
- string plcAddType = item.PlcAddType;
|
|
|
switch (plcAddType)
|
|
|
{
|
|
|
case "bool":
|
|
|
- bool isResult=PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Bit, WritePLCValue);
|
|
|
+ bool isResult = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Bit, plcValue);
|
|
|
+ if(isResult)
|
|
|
+ {
|
|
|
+ MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
break;
|
|
|
case "word":
|
|
|
- bool isResult2 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Word, WritePLCValue);
|
|
|
+ bool isResult2 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Word, plcValue);
|
|
|
+ if (isResult2)
|
|
|
+ {
|
|
|
+ MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
break;
|
|
|
case "dword":
|
|
|
- bool isResult3 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.DWord, WritePLCValue);
|
|
|
+ bool isResult3 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.DWord, plcValue);
|
|
|
+ if (isResult3)
|
|
|
+ {
|
|
|
+ MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
break;
|
|
|
case "real":
|
|
|
- bool isResult4 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Real, WritePLCValue);
|
|
|
+
|
|
|
+ bool isResult4 = PLCCom.GetInstance().WritePlcObject(plcAddr, VarType.Real, plcValue);
|
|
|
+ if (isResult4)
|
|
|
+ {
|
|
|
+ MessageBox.Show("写入成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取PLC配置
|
|
@@ -85,15 +112,7 @@ namespace PLCTool.ViewModels.MonitorManageViewModel
|
|
|
set { plcItemList = value; RaisePropertyChanged(); }
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 写入值
|
|
|
- /// </summary>
|
|
|
- private string writePLCValue;
|
|
|
- public string WritePLCValue
|
|
|
- {
|
|
|
- get { return writePLCValue; }
|
|
|
- set { writePLCValue = value; RaisePropertyChanged(); }
|
|
|
- }
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
#region 命令绑定
|