using System; using System.Collections.Generic; using System.Linq; using System.Text; using Prism.Mvvm; using System.Threading.Tasks; using S7NetModule.S7; using S7NetModule.Model; using Prism.Commands; using S7.Net; using System.Collections.ObjectModel; using CommonUtils.ViewModelBase; using CommonUtils.Log4; namespace S7NetModule.ViewModels { public class S7ReadlViewModel : BaseViewModel { private S7ReadModel _ReadModel=new S7ReadModel(); private string _OutMsg=""; public bool[] Res_Pass = new bool[] {true,false }; public string OutMsg { get => _OutMsg; set { SetProperty(ref _OutMsg, value); } } public S7ReadModel Read_Model { get => _ReadModel; set { SetProperty(ref _ReadModel, value); } } private ObservableCollection s7NetModels =new ObservableCollection(); public ObservableCollection S7NetModels { get => s7NetModels; set { SetProperty(ref s7NetModels, value); } } private List dataTypes=new List(); public List DataTypes { get => dataTypes; set => dataTypes = value; } private DelegateCommand _S7Readcommand; public DelegateCommand S7Readcommand => _S7Readcommand ?? ( _S7Readcommand = new DelegateCommand(ReadS7ClientCommand) ); public S7ReadlViewModel() { S7NetViewModel.ItemAdded += TcpClientConfig_ItemAdded; S7NetViewModel.ItemRemoved += TcpClientConfig_ItemRemoved; DataTypes = Enum.GetNames(typeof(VarType)).ToList(); } private void TcpClientConfig_ItemRemoved(ObservableCollection models) { S7NetModels = models; } private void TcpClientConfig_ItemAdded(ObservableCollection models) { S7NetModels = models; } public void ReadS7ClientCommand() { try { while (true) { switch (Read_Model.VarType) { case VarType.Bit: OutMsg = S7NetModels[0].plcClient.ReadBool(Read_Model).ToString(); break; case VarType.Byte: break; case VarType.Word: break; case VarType.DWord: break; case VarType.Int: OutMsg = S7NetModels[0].plcClient.ReadInt(Read_Model).ToString(); break; case VarType.DInt: OutMsg = S7NetModels[0].plcClient.ReadDInt(Read_Model).ToString(); break; case VarType.Real: OutMsg = S7NetModels[0].plcClient.ReadReal(Read_Model).ToString(); break; case VarType.LReal: break; case VarType.String: OutMsg = S7NetModels[0].plcClient.ReadString(Read_Model).ToString(); break; case VarType.S7String: break; case VarType.S7WString: break; case VarType.Timer: break; case VarType.Counter: break; case VarType.DateTime: break; case VarType.DateTimeLong: break; default: break; } if (Read_Model.VarType == VarType.Bit) { if (OutMsg == Read_Model.RefPass.ToString()) { break; } } else { break; } } }catch (Exception ex) { isErr = true; LogHelper.Error("S7读取运行错误 "+ex.Message); } } } }