S7ReadlViewModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Prism.Mvvm;
  6. using System.Threading.Tasks;
  7. using S7NetModule.S7;
  8. using S7NetModule.Model;
  9. using Prism.Commands;
  10. using S7.Net;
  11. using System.Collections.ObjectModel;
  12. using CommonUtils.ViewModelBase;
  13. using CommonUtils.Log4;
  14. namespace S7NetModule.ViewModels
  15. {
  16. public class S7ReadlViewModel : BaseViewModel
  17. {
  18. private S7ReadModel _ReadModel=new S7ReadModel();
  19. private string _OutMsg="";
  20. public bool[] Res_Pass = new bool[] {true,false };
  21. public string OutMsg
  22. {
  23. get => _OutMsg;
  24. set
  25. {
  26. SetProperty(ref _OutMsg, value);
  27. }
  28. }
  29. public S7ReadModel Read_Model
  30. {
  31. get => _ReadModel;
  32. set
  33. {
  34. SetProperty(ref _ReadModel, value);
  35. }
  36. }
  37. private ObservableCollection<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
  38. public ObservableCollection<S7PlcModel> S7NetModels
  39. {
  40. get => s7NetModels;
  41. set
  42. {
  43. SetProperty(ref s7NetModels, value);
  44. }
  45. }
  46. private List<string> dataTypes=new List<string>();
  47. public List<string> DataTypes { get => dataTypes; set => dataTypes = value; }
  48. private DelegateCommand _S7Readcommand;
  49. public DelegateCommand S7Readcommand =>
  50. _S7Readcommand ?? ( _S7Readcommand = new DelegateCommand(ReadS7ClientCommand) );
  51. public S7ReadlViewModel()
  52. {
  53. S7NetViewModel.ItemAdded += TcpClientConfig_ItemAdded;
  54. S7NetViewModel.ItemRemoved += TcpClientConfig_ItemRemoved;
  55. DataTypes = Enum.GetNames(typeof(VarType)).ToList();
  56. }
  57. private void TcpClientConfig_ItemRemoved(ObservableCollection<S7PlcModel> models)
  58. {
  59. S7NetModels = models;
  60. }
  61. private void TcpClientConfig_ItemAdded(ObservableCollection<S7PlcModel> models)
  62. {
  63. S7NetModels = models;
  64. }
  65. public void ReadS7ClientCommand()
  66. {
  67. try
  68. {
  69. while (true)
  70. {
  71. switch (Read_Model.VarType)
  72. {
  73. case VarType.Bit:
  74. OutMsg = S7NetModels[0].plcClient.ReadBool(Read_Model).ToString();
  75. break;
  76. case VarType.Byte:
  77. break;
  78. case VarType.Word:
  79. break;
  80. case VarType.DWord:
  81. break;
  82. case VarType.Int:
  83. OutMsg = S7NetModels[0].plcClient.ReadInt(Read_Model).ToString();
  84. break;
  85. case VarType.DInt:
  86. OutMsg = S7NetModels[0].plcClient.ReadDInt(Read_Model).ToString();
  87. break;
  88. case VarType.Real:
  89. OutMsg = S7NetModels[0].plcClient.ReadReal(Read_Model).ToString();
  90. break;
  91. case VarType.LReal:
  92. break;
  93. case VarType.String:
  94. OutMsg = S7NetModels[0].plcClient.ReadString(Read_Model).ToString();
  95. break;
  96. case VarType.S7String:
  97. break;
  98. case VarType.S7WString:
  99. break;
  100. case VarType.Timer:
  101. break;
  102. case VarType.Counter:
  103. break;
  104. case VarType.DateTime:
  105. break;
  106. case VarType.DateTimeLong:
  107. break;
  108. default:
  109. break;
  110. }
  111. if (Read_Model.VarType == VarType.Bit)
  112. {
  113. if (OutMsg == Read_Model.RefPass.ToString())
  114. {
  115. break;
  116. }
  117. }
  118. else
  119. {
  120. break;
  121. }
  122. }
  123. }catch (Exception ex)
  124. {
  125. isErr = true;
  126. LogHelper.Error("S7读取运行错误 "+ex.Message);
  127. }
  128. }
  129. }
  130. }