123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using Prism.Commands;
- using S7NetModule.Model;
- using S7NetModule.S7;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Prism.Mvvm;
- using System.Threading.Tasks;
- using S7.Net;
- using System.Collections.ObjectModel;
- using CommonUtils.Log4;
- namespace S7NetModule.ViewModels
- {
- public class S7WriteViewModel: CommonUtils.ViewModelBase.BaseViewModel
- {
- private S7WriteModel _WriteModel=new S7WriteModel();
- public S7WriteModel Write_Model
- {
- get => _WriteModel;
- set
- {
- SetProperty(ref _WriteModel, value);
- }
- }
- private ObservableCollection<S7PlcModel> s7NetModels =new ObservableCollection<S7PlcModel>();
- public ObservableCollection<S7PlcModel> S7NetModels
- {
- get => s7NetModels;
- set
- {
- SetProperty(ref s7NetModels, value);
- }
- }
- private DelegateCommand _S7Writecommand;
- public DelegateCommand S7Writecommand =>
- _S7Writecommand ?? ( _S7Writecommand = new DelegateCommand(WriteS7ClientCommand) );
- public S7WriteViewModel()
- {
- 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;
- }
- private List<string> dataTypes=new List<string>();
- public List<string> DataTypes { get => dataTypes; set => dataTypes = value; }
- public void WriteS7ClientCommand()
- {
- try {
- switch ( Write_Model.VarType )
- {
- case VarType.Bit:
- if ( Write_Model.Value.ToString().ToUpper() == "TRUE" || Write_Model.Value.ToString() == "1" )
- {
- Write_Model.Value = true;
- }
- else
- {
- Write_Model.Value = false;
- }
- S7NetModels[ 0 ].plcClient.WriteBool(Write_Model);
- break;
- case VarType.Byte:
-
- break;
- case VarType.Word:
- break;
- case VarType.DWord:
- break;
- case VarType.Int:
- S7NetModels[ 0 ].plcClient.WriteShort(Write_Model);
- break;
- case VarType.DInt:
- S7NetModels[ 0 ].plcClient.WriteDint(Write_Model);
- break;
- case VarType.Real:
- S7NetModels[ 0 ].plcClient.WriteReal(Write_Model);
- break;
- case VarType.LReal:
- break;
- case VarType.String:
- S7NetModels[ 0 ].plcClient.WriteString(Write_Model);
- 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;
- }
- }
- catch ( Exception ex )
- {
- isErr = true;
- LogHelper.Error("S7写入运行错误 " + ex.Message);
- }
- }
- }
- }
|