HamBook – Rev 1
?pathlinks?
using HamBook.Radios.Generic;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static HamBook.Radios.Yaesu.FT_891.Constants;
namespace HamBook.Radios.Yaesu.FT_891
{
[Radio("Yaesu FT-891")]
public class PS : Generic.PS
{
private static readonly Regex readRegex = new Regex(@"^PS(?<state>[0,1])$", RegexOptions.Compiled);
public PS(SerialPort serialPort) : base(serialPort)
{
}
public override async Task Set(PowerState state)
{
var taskCompletionSource = new TaskCompletionSource<bool>();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(ca =>
{
switch(state)
{
case PowerState.ON:
SerialPort.Write($"{Name}1{EOT}");
Task.Delay(Constants.TIME_TO_WARM).ContinueWith(cb =>
{
taskCompletionSource.TrySetResult(true);
});
return;
case PowerState.OFF:
SerialPort.Write($"{Name}0{EOT}");
break;
}
taskCompletionSource.TrySetResult(true);
});
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
SerialPort.Write($"POWER");
await taskCompletionSource.Task;
}
public override async Task<PowerState> Read()
{
var taskCompletionSource = new TaskCompletionSource<int>();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ =>
{
SerialPort.Write($"{Name}{EOT}");
var result = SerialPort.ReadTo(EOT);
var match = readRegex.Match(result);
var state = int.Parse(match.Result("${state}"));
taskCompletionSource.TrySetResult(state);
});
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
SerialPort.Write($"POWER");
var onoff = await taskCompletionSource.Task;
switch(onoff)
{
case 1:
return PowerState.ON;
case 0:
return PowerState.OFF;
}
return PowerState.NONE;
}
}
}