S7WriteViewModel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Prism.Commands;
  2. using S7NetModule.Model;
  3. using S7NetModule.S7;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using Prism.Mvvm;
  9. using System.Threading.Tasks;
  10. using S7.Net;
  11. using System.Collections.ObjectModel;
  12. using CommonUtils.Log4;
  13. namespace S7NetModule.ViewModels
  14. {
  15. public class S7WriteViewModel: CommonUtils.ViewModelBase.BaseViewModel
  16. {
  17. private S7WriteModel _WriteModel=new S7WriteModel();
  18. public S7WriteModel Write_Model
  19. {
  20. get => _WriteModel;
  21. set
  22. {
  23. SetProperty(ref _WriteModel, value);
  24. }
  25. }
  26. private ObservableCollection<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
  27. public ObservableCollection<S7PlcModel> S7NetModels
  28. {
  29. get => s7NetModels;
  30. set
  31. {
  32. SetProperty(ref s7NetModels, value);
  33. }
  34. }
  35. private DelegateCommand _S7Writecommand;
  36. public DelegateCommand S7Writecommand =>
  37. _S7Writecommand ?? ( _S7Writecommand = new DelegateCommand(WriteS7ClientCommand) );
  38. public S7WriteViewModel()
  39. {
  40. S7NetViewModel.ItemAdded += TcpClientConfig_ItemAdded;
  41. S7NetViewModel.ItemRemoved += TcpClientConfig_ItemRemoved;
  42. DataTypes=Enum.GetNames(typeof(VarType)).ToList();
  43. }
  44. private void TcpClientConfig_ItemRemoved(ObservableCollection<S7PlcModel> models)
  45. {
  46. S7NetModels = models;
  47. }
  48. private void TcpClientConfig_ItemAdded(ObservableCollection<S7PlcModel> models)
  49. {
  50. S7NetModels = models;
  51. }
  52. private List<string> dataTypes=new List<string>();
  53. public List<string> DataTypes { get => dataTypes; set => dataTypes = value; }
  54. public void WriteS7ClientCommand()
  55. {
  56. try {
  57. switch ( Write_Model.VarType )
  58. {
  59. case VarType.Bit:
  60. if ( Write_Model.Value.ToString().ToUpper() == "TRUE" || Write_Model.Value.ToString() == "1" )
  61. {
  62. Write_Model.Value = true;
  63. }
  64. else
  65. {
  66. Write_Model.Value = false;
  67. }
  68. S7NetModels[ 0 ].plcClient.WriteBool(Write_Model);
  69. break;
  70. case VarType.Byte:
  71. break;
  72. case VarType.Word:
  73. break;
  74. case VarType.DWord:
  75. break;
  76. case VarType.Int:
  77. S7NetModels[ 0 ].plcClient.WriteShort(Write_Model);
  78. break;
  79. case VarType.DInt:
  80. S7NetModels[ 0 ].plcClient.WriteDint(Write_Model);
  81. break;
  82. case VarType.Real:
  83. S7NetModels[ 0 ].plcClient.WriteReal(Write_Model);
  84. break;
  85. case VarType.LReal:
  86. break;
  87. case VarType.String:
  88. S7NetModels[ 0 ].plcClient.WriteString(Write_Model);
  89. break;
  90. case VarType.S7String:
  91. break;
  92. case VarType.S7WString:
  93. break;
  94. case VarType.Timer:
  95. break;
  96. case VarType.Counter:
  97. break;
  98. case VarType.DateTime:
  99. break;
  100. case VarType.DateTimeLong:
  101. break;
  102. default:
  103. break;
  104. }
  105. }
  106. catch ( Exception ex )
  107. {
  108. isErr = true;
  109. LogHelper.Error("S7写入运行错误 " + ex.Message);
  110. }
  111. }
  112. }
  113. }