添加配置连接窗口已经相关S7与ModbusTCP连接功能
This commit is contained in:
237
MainModule/ViewModels/ConnectionConfigViewModel.cs
Normal file
237
MainModule/ViewModels/ConnectionConfigViewModel.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
using MainModule.Events;
|
||||
using Prism.Commands;
|
||||
using Prism.Mvvm;
|
||||
using S7.Net;
|
||||
using System.Collections.Generic;
|
||||
using System.IO.Ports;
|
||||
using System.Windows;
|
||||
|
||||
namespace MainModule.ViewModels
|
||||
{
|
||||
public class ConnectionConfigViewModel : BindableBase
|
||||
{
|
||||
#region 私有字段
|
||||
private Events.ProtocolType _selectedProtocol = Events.ProtocolType.S7;
|
||||
private string _ipAddress = "127.0.0.1";
|
||||
private int _port = 102;
|
||||
private string _serialPort = "COM1";
|
||||
private int _baudRate = 9600;
|
||||
private Parity _parity = Parity.None;
|
||||
private int _dataBits = 8;
|
||||
private StopBits _stopBits = StopBits.One;
|
||||
private CpuType _cpuType = CpuType.S71200;
|
||||
private short _rack = 0;
|
||||
private short _slot = 1;
|
||||
private int _sendTimeout = 3000;
|
||||
private int _receiveTimeout = 5000;
|
||||
#endregion
|
||||
|
||||
#region 公共属性
|
||||
public Events.ProtocolType SelectedProtocol
|
||||
{
|
||||
get => _selectedProtocol;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _selectedProtocol, value))
|
||||
{
|
||||
RaisePropertyChanged(nameof(IsS7Protocol));
|
||||
RaisePropertyChanged(nameof(IsModbusTCPProtocol));
|
||||
RaisePropertyChanged(nameof(IsModbusRTUProtocol));
|
||||
RaisePropertyChanged(nameof(IsTCPProtocol));
|
||||
RaisePropertyChanged(nameof(IsHTTPProtocol));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string IpAddress
|
||||
{
|
||||
get => _ipAddress;
|
||||
set => SetProperty(ref _ipAddress, value);
|
||||
}
|
||||
|
||||
public int Port
|
||||
{
|
||||
get => _port;
|
||||
set => SetProperty(ref _port, value);
|
||||
}
|
||||
|
||||
public string SerialPort
|
||||
{
|
||||
get => _serialPort;
|
||||
set => SetProperty(ref _serialPort, value);
|
||||
}
|
||||
|
||||
public int BaudRate
|
||||
{
|
||||
get => _baudRate;
|
||||
set => SetProperty(ref _baudRate, value);
|
||||
}
|
||||
|
||||
public Parity Parity
|
||||
{
|
||||
get => _parity;
|
||||
set => SetProperty(ref _parity, value);
|
||||
}
|
||||
|
||||
public int DataBits
|
||||
{
|
||||
get => _dataBits;
|
||||
set => SetProperty(ref _dataBits, value);
|
||||
}
|
||||
|
||||
public StopBits StopBits
|
||||
{
|
||||
get => _stopBits;
|
||||
set => SetProperty(ref _stopBits, value);
|
||||
}
|
||||
|
||||
public CpuType CpuType
|
||||
{
|
||||
get => _cpuType;
|
||||
set => SetProperty(ref _cpuType, value);
|
||||
}
|
||||
|
||||
public short Rack
|
||||
{
|
||||
get => _rack;
|
||||
set => SetProperty(ref _rack, value);
|
||||
}
|
||||
|
||||
public short Slot
|
||||
{
|
||||
get => _slot;
|
||||
set => SetProperty(ref _slot, value);
|
||||
}
|
||||
|
||||
public int SendTimeout
|
||||
{
|
||||
get => _sendTimeout;
|
||||
set => SetProperty(ref _sendTimeout, value);
|
||||
}
|
||||
|
||||
public int ReceiveTimeout
|
||||
{
|
||||
get => _receiveTimeout;
|
||||
set => SetProperty(ref _receiveTimeout, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 协议类型判断属性
|
||||
public bool IsS7Protocol => SelectedProtocol == Events.ProtocolType.S7;
|
||||
public bool IsModbusTCPProtocol => SelectedProtocol == Events.ProtocolType.ModbusTCP;
|
||||
public bool IsModbusRTUProtocol => SelectedProtocol == Events.ProtocolType.ModbusRTU;
|
||||
public bool IsTCPProtocol => SelectedProtocol == Events.ProtocolType.TCP;
|
||||
public bool IsHTTPProtocol => SelectedProtocol == Events.ProtocolType.HTTP;
|
||||
#endregion
|
||||
|
||||
#region 下拉列表数据源
|
||||
public List<CpuType> CpuTypes { get; } = new List<CpuType>
|
||||
{
|
||||
CpuType.S71200,
|
||||
CpuType.S71500,
|
||||
CpuType.S7200,
|
||||
CpuType.S7300,
|
||||
CpuType.S7400,
|
||||
CpuType.S7200Smart
|
||||
};
|
||||
|
||||
public List<int> BaudRates { get; } = new List<int>
|
||||
{
|
||||
9600,
|
||||
19200,
|
||||
38400,
|
||||
57600,
|
||||
115200
|
||||
};
|
||||
|
||||
public List<int> DataBitsList { get; } = new List<int> { 7, 8 };
|
||||
|
||||
public List<Parity> ParityList { get; } = new List<Parity>
|
||||
{
|
||||
Parity.None,
|
||||
Parity.Odd,
|
||||
Parity.Even,
|
||||
Parity.Mark,
|
||||
Parity.Space
|
||||
};
|
||||
|
||||
public List<StopBits> StopBitsList { get; } = new List<StopBits>
|
||||
{
|
||||
StopBits.None,
|
||||
StopBits.One,
|
||||
StopBits.OnePointFive,
|
||||
StopBits.Two
|
||||
};
|
||||
|
||||
public List<string> SerialPorts { get; } = new List<string>
|
||||
{
|
||||
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8"
|
||||
};
|
||||
#endregion
|
||||
|
||||
#region 命令
|
||||
public DelegateCommand ConfirmCommand { get; }
|
||||
public DelegateCommand CancelCommand { get; }
|
||||
public DelegateCommand SelectS7Command { get; }
|
||||
public DelegateCommand SelectModbusTCPCommand { get; }
|
||||
public DelegateCommand SelectModbusRTUCommand { get; }
|
||||
public DelegateCommand SelectTCPCommand { get; }
|
||||
public DelegateCommand SelectHTTPCommand { get; }
|
||||
#endregion
|
||||
|
||||
#region 配置数据(供外部获取)
|
||||
public ConnectionConfigEvent.ConnectionConfigData ConnectionConfigData { get; private set; }
|
||||
#endregion
|
||||
|
||||
public ConnectionConfigViewModel()
|
||||
{
|
||||
ConfirmCommand = new DelegateCommand(ExecuteConfirm);
|
||||
CancelCommand = new DelegateCommand(ExecuteCancel);
|
||||
SelectS7Command = new DelegateCommand(() => SelectedProtocol = Events.ProtocolType.S7);
|
||||
SelectModbusTCPCommand = new DelegateCommand(() => SelectedProtocol = Events.ProtocolType.ModbusTCP);
|
||||
SelectModbusRTUCommand = new DelegateCommand(() => SelectedProtocol = Events.ProtocolType.ModbusRTU);
|
||||
SelectTCPCommand = new DelegateCommand(() => SelectedProtocol = Events.ProtocolType.TCP);
|
||||
SelectHTTPCommand = new DelegateCommand(() => SelectedProtocol = Events.ProtocolType.HTTP);
|
||||
}
|
||||
|
||||
#region 命令执行
|
||||
private void ExecuteConfirm()
|
||||
{
|
||||
ConnectionConfigData = new ConnectionConfigEvent.ConnectionConfigData
|
||||
{
|
||||
Protocol = SelectedProtocol,
|
||||
IpAddress = IpAddress,
|
||||
Port = Port,
|
||||
SerialPort = SerialPort,
|
||||
BaudRate = BaudRate,
|
||||
Parity = Parity,
|
||||
DataBits = DataBits,
|
||||
StopBits = StopBits,
|
||||
CpuType = CpuType,
|
||||
Rack = Rack,
|
||||
Slot = Slot,
|
||||
SendTimeout = SendTimeout,
|
||||
ReceiveTimeout = ReceiveTimeout
|
||||
};
|
||||
|
||||
var window = Application.Current.Windows.OfType<Views.ConnectionConfigView>().FirstOrDefault();
|
||||
if (window != null)
|
||||
{
|
||||
window.DialogResult = true;
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteCancel()
|
||||
{
|
||||
ConnectionConfigData = null;
|
||||
var window = Application.Current.Windows.OfType<Views.ConnectionConfigView>().FirstOrDefault();
|
||||
if (window != null)
|
||||
{
|
||||
window.DialogResult = false;
|
||||
window.Close();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user