You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.4 KiB
77 lines
2.4 KiB
using Common.Messaging;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
using MultiTerm.Protocols.Types;
|
|
|
|
namespace MultiTerm.Protocols.Serial;
|
|
|
|
public partial class SerialProtocolSettingsViewModel : ProtocolSettingsViewModel, ISerialProtocolSettings
|
|
{
|
|
public override ProtocolType ProtocolType => ProtocolType.Serial;
|
|
|
|
[ObservableProperty]
|
|
private int baudRate = 115200;
|
|
|
|
[ObservableProperty]
|
|
private string portName;
|
|
|
|
[ObservableProperty]
|
|
private Parity parity = Parity.None;
|
|
|
|
[ObservableProperty]
|
|
private int dataBits = 8;
|
|
|
|
[ObservableProperty]
|
|
private StopBits stopBits = StopBits.One;
|
|
|
|
[ObservableProperty]
|
|
private Handshake handshake = Handshake.None;
|
|
|
|
[ObservableProperty]
|
|
private IEnumerable<string> comPorts = Array.Empty<string>();
|
|
|
|
[RelayCommand]
|
|
private void ReloadComPorts()
|
|
{
|
|
this.ComPorts = SerialProtocol.GetPortNames();
|
|
// select first COM port, if none is selected yet and there are some available
|
|
if (String.IsNullOrEmpty(this.PortName) && this.ComPorts.Any())
|
|
{
|
|
this.PortName = this.ComPorts.First();
|
|
}
|
|
}
|
|
|
|
public SerialProtocolSettingsViewModel(IMessenger messenger) : base(messenger)
|
|
{
|
|
// load com ports initially
|
|
this.ReloadComPorts();
|
|
this.portName = this.ComPorts.FirstOrDefault(string.Empty);
|
|
}
|
|
|
|
public bool AreValid()
|
|
{
|
|
if (String.IsNullOrEmpty(this.PortName))
|
|
{
|
|
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidUIMessage(nameof(this.PortName), "Null or empty"));
|
|
return false;
|
|
}
|
|
if (this.PortName.ToLower().StartsWith("com") == false)
|
|
{
|
|
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidUIMessage(nameof(this.PortName), "Must start with COM"));
|
|
return false;
|
|
}
|
|
if ((this.DataBits < 5 || this.DataBits > 8) && this.DataBits != 16)
|
|
{
|
|
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidUIMessage(nameof(this.DataBits), "Must be 5...8 or 16"));
|
|
return false;
|
|
}
|
|
if(this.BaudRate < 0)
|
|
{
|
|
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidUIMessage(nameof(this.BaudRate), "Must be larger than 0"));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|