123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- using CommonUtils.Log4;
- using CommonUtils.Msgbox;
- using CommonUtils.Tcp.TcpSocket;
- using Prism.Commands;
- using Prism.Mvvm;
- using S7.Net;
- using S7NetModule.Model;
- using S7NetModule.S7;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using WpfControlLibrary.IpAddress;
- namespace S7NetModule.ViewModels
- {
- public class S7NetViewModel : CommonUtils.ViewModelBase.BaseViewModel
- {
- private string _message;
- public string Message
- {
- get { return _message; }
- set { SetProperty(ref _message, value); }
- }
- private IpAddressViewModel _IpAddressViewModel=new IpAddressViewModel();
- public IpAddressViewModel IpAddressViewModel
- {
- get { return _IpAddressViewModel; }
- set
- {
- S7NetModule.IpAddress=value.AddressText;
- SetProperty(ref _IpAddressViewModel, value);
- }
- }
- private S7PlcModel _s7NetModel=null;
- public S7PlcModel SelectS7Netmodel
- {
- get
- {
- return _s7NetModel;
- }
- set
- {
- SetProperty(ref _s7NetModel, value);
- }
- }
- private List<CpuType> _cpuType=new List<CpuType>()
- {
- CpuType.S7200,
- CpuType.Logo0BA8,
- CpuType.S7200Smart,
- CpuType.S7300,
- CpuType.S7400,
- CpuType.S71200,
- CpuType.S71500,
- };
- public List<CpuType> CpuTypes
- {
- get
- {
- return _cpuType;
- }
- set { SetProperty(ref _cpuType, value); }
- }
- private S7PlcModel s7NetModule=new S7PlcModel();
- public S7PlcModel S7NetModule
- {
- get
- {
- s7NetModule.IpAddress = _IpAddressViewModel.AddressText;
- return s7NetModule;
- }
- set
- {
- SetProperty(ref s7NetModule, value);
- }
- }
- private ObservableCollection<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
- public ObservableCollection<S7PlcModel> S7NetModels
- {
- get => s7NetModels;
- set
- {
- SetProperty(ref s7NetModels, value);
- }
- }
- private string uiOpenText="连接";
- public string UiOpenText
- {
- get => uiOpenText;
- set
- {
- SetProperty(ref uiOpenText, value);
- }
- }
- private DelegateCommand _S7Opencommand;
- public DelegateCommand S7Opencommand =>
- _S7Opencommand ?? ( _S7Opencommand = new DelegateCommand(OpenS7ClientCommand) );
- public S7NetViewModel()
- {
- Message = "View A from your Prism Module";
- IpAddressViewModel=new IpAddressViewModel();
- IpAddressViewModel.SetAddress(S7NetModule.IpAddress);
- }
- public void OpenS7ClientCommand()
- {
- if ( UiOpenText == "断开" )
- {
- for ( int i = 0; i < S7NetModels.Count; i++ )
- {
- if ( SelectS7Netmodel != null )
- {
- if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == SelectS7Netmodel.IpAddress + ":" + SelectS7Netmodel.Port )
- {
- S7NetModels[ i ].plcClient.Close();
- }
- }
- else
- {
- if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == S7NetModule.IpAddress + ":" + S7NetModule.Port )
- {
- S7NetModels[ i ].plcClient.Close();
- }
- }
- }
- UiOpenText = "连接";
- return;
- }
- if ( S7NetModule.IpAddress.Trim() == "" )
- {
- WinMessageBox.Show("IP地址不能为空!");
- return;
- }
- if ( S7NetModule.Slot <= 0 )
- {
- WinMessageBox.Show("端口号不能为空!");
- return;
- }
- S7Plc plc = new S7Plc();
- plc.RemoteClose += S7NetClient_RemoteClose;
- plc.model=new S7PlcModel() {IpAddress= S7NetModule.IpAddress,
- CpuType=S7NetModule.CpuType,
- Timeout=S7NetModule.Timeout,
- Rack= S7NetModule.Rack,
- Slot= S7NetModule.Slot };
- if ( !plc.Open() )
- {
- WinMessageBox.Show("连接服务器失败!");
- return;
- }
- UiOpenText = "断开";
- S7PlcModel model= new S7PlcModel()
- {
- IpAddress = S7NetModule.IpAddress,
- CpuType = S7NetModule.CpuType,
- Rack=S7NetModule.Rack,
- Slot=S7NetModule.Slot,
- Timeout=S7NetModule.Timeout,
- plcClient = plc
- };
- S7Add(model);
-
-
- }
- public void OpenAllClientCommand()
- {
- try
- {
- for ( int i = 0; i < S7NetModels.Count; i++ )
- {
- if ( !S7NetModels[ i ].plcClient.plc.IsConnected )
- {
- if ( !S7NetModels[ i ].plcClient.Open() )
- {
- return;
- }
- }
- }
- }
- catch ( Exception ex )
- {
- isErr = true;
- LogHelper.Error("S7客户端运行错误 " + ex.Message);
- }
- }
- public void CloseAllClientCommand()
- {
- for ( int i = 0; i < S7NetModels.Count; i++ )
- {
- if (S7NetModels[ i ].plcClient.plc.IsConnected )
- {
- S7NetModels[ i ].plcClient.Close();
-
- }
- }
- }
- public static event Action<ObservableCollection<S7PlcModel> > ItemAdded;
- public static event Action<ObservableCollection<S7PlcModel> > ItemRemoved;
- public void S7Add(S7PlcModel item)
- {
- S7NetModels.Add(item);
- ItemAdded?.Invoke(S7NetModels);
- }
- public void S7AddRemove(int item)
- {
- S7NetModels.RemoveAt(item);
- ItemRemoved?.Invoke(S7NetModels);
-
- }
- private void S7NetClient_RemoteClose(string key)
- {
- for ( int i = 0; i < S7NetModels.Count; i++ )
- {
- if ( S7NetModels[ i ].IpAddress + ":" + S7NetModels[ i ].Port == key )
- {
- S7AddRemove(i);
- }
- }
- LogHelper.Info(key + "连接断开");
- }
- }
- }
|