123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- 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<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
- public ObservableCollection<S7PlcModel> S7NetModels
- {
- get => s7NetModels;
- set
- {
- SetProperty(ref s7NetModels, value);
- }
- }
- private List<string> dataTypes=new List<string>();
- public List<string> 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<S7PlcModel> models)
- {
- S7NetModels = models;
- }
- private void TcpClientConfig_ItemAdded(ObservableCollection<S7PlcModel> 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);
- }
- }
- }
- }
|