Multiprocotol Terminalprogram (BAT)
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.
MultiTerm/MultiTerm.Protocols/Serial/SerialProtocolSettingsViewM...

78 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();
}
// TODO
public string NewlineSequenceOnSend => throw new NotImplementedException();
public string NewlineOnReceivedSequence => throw new NotImplementedException();
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 ProtocolSettingsInvalidMessage(nameof(this.PortName), "Null or empty"));
return false;
}
if (this.PortName.ToLower().StartsWith("com") == false)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.PortName), "Must start with COM"));
return false;
}
if ((this.DataBits < 5 || this.DataBits > 8) && this.DataBits != 16)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.DataBits), "Must be 5...8 or 16"));
return false;
}
if(this.BaudRate < 0)
{
this.messenger.Send<IUserInterfaceMessage>(new ProtocolSettingsInvalidMessage(nameof(this.BaudRate), "Must be larger than 0"));
return false;
}
return true;
}
}