Files
IOT/MainModule/ViewModels/ConnectionConfigViewModel.cs
“hsc” fa2f9f64c5 RTU添加
2026-06-12 15:29:18 +08:00

238 lines
7.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 = 502;
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
}
}