S7NetViewModel.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using CommonUtils.Log4;
  2. using CommonUtils.Msgbox;
  3. using CommonUtils.Tcp.TcpSocket;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using S7.Net;
  7. using S7NetModule.Model;
  8. using S7NetModule.S7;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using WpfControlLibrary.IpAddress;
  16. namespace S7NetModule.ViewModels
  17. {
  18. public class S7NetViewModel : CommonUtils.ViewModelBase.BaseViewModel
  19. {
  20. private string _message;
  21. public string Message
  22. {
  23. get { return _message; }
  24. set { SetProperty(ref _message, value); }
  25. }
  26. private IpAddressViewModel _IpAddressViewModel=new IpAddressViewModel();
  27. public IpAddressViewModel IpAddressViewModel
  28. {
  29. get { return _IpAddressViewModel; }
  30. set
  31. {
  32. S7NetModule.IpAddress=value.AddressText;
  33. SetProperty(ref _IpAddressViewModel, value);
  34. }
  35. }
  36. private S7PlcModel _s7NetModel=null;
  37. public S7PlcModel SelectS7Netmodel
  38. {
  39. get
  40. {
  41. return _s7NetModel;
  42. }
  43. set
  44. {
  45. SetProperty(ref _s7NetModel, value);
  46. }
  47. }
  48. private List<CpuType> _cpuType=new List<CpuType>()
  49. {
  50. CpuType.S7200,
  51. CpuType.Logo0BA8,
  52. CpuType.S7200Smart,
  53. CpuType.S7300,
  54. CpuType.S7400,
  55. CpuType.S71200,
  56. CpuType.S71500,
  57. };
  58. public List<CpuType> CpuTypes
  59. {
  60. get
  61. {
  62. return _cpuType;
  63. }
  64. set { SetProperty(ref _cpuType, value); }
  65. }
  66. private S7PlcModel s7NetModule=new S7PlcModel();
  67. public S7PlcModel S7NetModule
  68. {
  69. get
  70. {
  71. s7NetModule.IpAddress = _IpAddressViewModel.AddressText;
  72. return s7NetModule;
  73. }
  74. set
  75. {
  76. SetProperty(ref s7NetModule, value);
  77. }
  78. }
  79. private ObservableCollection<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
  80. public ObservableCollection<S7PlcModel> S7NetModels
  81. {
  82. get => s7NetModels;
  83. set
  84. {
  85. SetProperty(ref s7NetModels, value);
  86. }
  87. }
  88. private string uiOpenText="连接";
  89. public string UiOpenText
  90. {
  91. get => uiOpenText;
  92. set
  93. {
  94. SetProperty(ref uiOpenText, value);
  95. }
  96. }
  97. private DelegateCommand _S7Opencommand;
  98. public DelegateCommand S7Opencommand =>
  99. _S7Opencommand ?? ( _S7Opencommand = new DelegateCommand(OpenS7ClientCommand) );
  100. public S7NetViewModel()
  101. {
  102. Message = "View A from your Prism Module";
  103. IpAddressViewModel=new IpAddressViewModel();
  104. IpAddressViewModel.SetAddress(S7NetModule.IpAddress);
  105. }
  106. public void OpenS7ClientCommand()
  107. {
  108. if ( UiOpenText == "断开" )
  109. {
  110. for ( int i = 0; i < S7NetModels.Count; i++ )
  111. {
  112. if ( SelectS7Netmodel != null )
  113. {
  114. if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == SelectS7Netmodel.IpAddress + ":" + SelectS7Netmodel.Port )
  115. {
  116. S7NetModels[ i ].plcClient.Close();
  117. }
  118. }
  119. else
  120. {
  121. if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == S7NetModule.IpAddress + ":" + S7NetModule.Port )
  122. {
  123. S7NetModels[ i ].plcClient.Close();
  124. }
  125. }
  126. }
  127. UiOpenText = "连接";
  128. return;
  129. }
  130. if ( S7NetModule.IpAddress.Trim() == "" )
  131. {
  132. WinMessageBox.Show("IP地址不能为空!");
  133. return;
  134. }
  135. if ( S7NetModule.Slot <= 0 )
  136. {
  137. WinMessageBox.Show("端口号不能为空!");
  138. return;
  139. }
  140. S7Plc plc = new S7Plc();
  141. plc.RemoteClose += S7NetClient_RemoteClose;
  142. plc.model=new S7PlcModel() {IpAddress= S7NetModule.IpAddress,
  143. CpuType=S7NetModule.CpuType,
  144. Timeout=S7NetModule.Timeout,
  145. Rack= S7NetModule.Rack,
  146. Slot= S7NetModule.Slot };
  147. if ( !plc.Open() )
  148. {
  149. WinMessageBox.Show("连接服务器失败!");
  150. return;
  151. }
  152. UiOpenText = "断开";
  153. S7PlcModel model= new S7PlcModel()
  154. {
  155. IpAddress = S7NetModule.IpAddress,
  156. CpuType = S7NetModule.CpuType,
  157. Rack=S7NetModule.Rack,
  158. Slot=S7NetModule.Slot,
  159. Timeout=S7NetModule.Timeout,
  160. plcClient = plc
  161. };
  162. S7Add(model);
  163. }
  164. public void OpenAllClientCommand()
  165. {
  166. try
  167. {
  168. for ( int i = 0; i < S7NetModels.Count; i++ )
  169. {
  170. if ( !S7NetModels[ i ].plcClient.plc.IsConnected )
  171. {
  172. if ( !S7NetModels[ i ].plcClient.Open() )
  173. {
  174. return;
  175. }
  176. }
  177. }
  178. }
  179. catch ( Exception ex )
  180. {
  181. isErr = true;
  182. LogHelper.Error("S7客户端运行错误 " + ex.Message);
  183. }
  184. }
  185. public void CloseAllClientCommand()
  186. {
  187. for ( int i = 0; i < S7NetModels.Count; i++ )
  188. {
  189. if (S7NetModels[ i ].plcClient.plc.IsConnected )
  190. {
  191. S7NetModels[ i ].plcClient.Close();
  192. }
  193. }
  194. }
  195. public static event Action<ObservableCollection<S7PlcModel> > ItemAdded;
  196. public static event Action<ObservableCollection<S7PlcModel> > ItemRemoved;
  197. public void S7Add(S7PlcModel item)
  198. {
  199. S7NetModels.Add(item);
  200. ItemAdded?.Invoke(S7NetModels);
  201. }
  202. public void S7AddRemove(int item)
  203. {
  204. S7NetModels.RemoveAt(item);
  205. ItemRemoved?.Invoke(S7NetModels);
  206. }
  207. private void S7NetClient_RemoteClose(string key)
  208. {
  209. for ( int i = 0; i < S7NetModels.Count; i++ )
  210. {
  211. if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == key )
  212. {
  213. S7AddRemove(i);
  214. }
  215. }
  216. LogHelper.Info(key + "连接断开");
  217. }
  218. }
  219. }