From e339831f0c504fa0b2ab533174d9abdd11d49bac Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 29 Apr 2019 23:56:51 +0100 Subject: [PATCH] Implemented AbortSlew and did some code tidy up. --- .../TelescopeControllerUnitTests.cs | 14 +++++++- MeadeAutostar497/AscomClasses/Telescope.cs | 14 +++----- .../Controller/ISerialProcessor.cs | 26 ++++++++++++++ .../Controller/ITelescopeController.cs | 3 +- .../Controller/SerialProcessor.cs | 34 +++++++------------ .../Controller/TelescopeController.cs | 17 +++++----- MeadeAutostar497/MeadeAutostar497.csproj | 1 + 7 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 MeadeAutostar497/Controller/ISerialProcessor.cs diff --git a/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs b/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs index 3404796..9362784 100644 --- a/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs +++ b/MeadeAutostar497.UnitTests/TelescopeControllerUnitTests.cs @@ -143,7 +143,7 @@ namespace MeadeAutostar497.UnitTests } [Test] - public void SettingPortToInavalidPortFails() + public void SettingPortToInvalidPortFails() { var exception = Assert.Throws(() => _telescopeController.Port = "COM5"); @@ -151,5 +151,17 @@ namespace MeadeAutostar497.UnitTests Assert.That(_telescopeController.Port, Is.EqualTo("COM1")); //port hasn't changed } + + [Test] + public void AbortSlewWorks() + { + _isConnected = true; + + _telescopeController.Connected = true; + + _telescopeController.AbortSlew(); + + serialMock.Verify(x => x.Command("#:Q#"), Times.Once); + } } } diff --git a/MeadeAutostar497/AscomClasses/Telescope.cs b/MeadeAutostar497/AscomClasses/Telescope.cs index a56d4d6..bc4171d 100644 --- a/MeadeAutostar497/AscomClasses/Telescope.cs +++ b/MeadeAutostar497/AscomClasses/Telescope.cs @@ -27,11 +27,7 @@ #define Telescope using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text; using System.Runtime.InteropServices; - using ASCOM; using ASCOM.Astrometry; using ASCOM.Astrometry.AstroUtils; @@ -293,8 +289,8 @@ namespace ASCOM.MeadeAutostar497 #region ITelescope Implementation public void AbortSlew() { - tl.LogMessage("AbortSlew", "Not implemented"); - throw new ASCOM.MethodNotImplementedException("AbortSlew"); + tl.LogMessage("AbortSlew", "Aborting slew"); + _telescopeController.AbortSlew(); } public AlignmentModes AlignmentMode @@ -380,9 +376,9 @@ namespace ASCOM.MeadeAutostar497 tl.LogMessage("CanMoveAxis", "Get - " + Axis.ToString()); switch (Axis) { - case TelescopeAxes.axisPrimary: return false; - case TelescopeAxes.axisSecondary: return false; - case TelescopeAxes.axisTertiary: return false; + case TelescopeAxes.axisPrimary: return true; //RA or AZ + case TelescopeAxes.axisSecondary: return true; //Dev or Alt + case TelescopeAxes.axisTertiary: return false; //rotator / derotator default: throw new InvalidValueException("CanMoveAxis", Axis.ToString(), "0 to 2"); } } diff --git a/MeadeAutostar497/Controller/ISerialProcessor.cs b/MeadeAutostar497/Controller/ISerialProcessor.cs new file mode 100644 index 0000000..5f2e2c0 --- /dev/null +++ b/MeadeAutostar497/Controller/ISerialProcessor.cs @@ -0,0 +1,26 @@ +using System.IO.Ports; +using System.Runtime.InteropServices; + +namespace ASCOM.MeadeAutostar497.Controller +{ + [ComVisible(false)] + public interface ISerialProcessor + { + bool IsOpen { get; } + bool DtrEnable { get; set; } + bool RtsEnable { get; set; } + int BaudRate { get; set; } + int DataBits { get; set; } + StopBits StopBits { get; set; } + Parity Parity { get; set; } + string PortName { get; set; } + string[] GetPortNames(); + void Open(); + void Close(); + + string CommandTerminated(string command, string terminator); + char CommandChar(string command); + string ReadTerminated(string terminator); + void Command(string command); + } +} \ No newline at end of file diff --git a/MeadeAutostar497/Controller/ITelescopeController.cs b/MeadeAutostar497/Controller/ITelescopeController.cs index f0bf070..d903675 100644 --- a/MeadeAutostar497/Controller/ITelescopeController.cs +++ b/MeadeAutostar497/Controller/ITelescopeController.cs @@ -1,15 +1,14 @@ using System; -using System.IO.Ports; namespace ASCOM.MeadeAutostar497.Controller { public interface ITelescopeController { - ISerialProcessor SerialPort { get; set; } string Port { get; set; } bool Connected { get; set; } bool Slewing { get; } DateTime utcDate { get; set; } + void AbortSlew(); } } \ No newline at end of file diff --git a/MeadeAutostar497/Controller/SerialProcessor.cs b/MeadeAutostar497/Controller/SerialProcessor.cs index b49619f..b9fe333 100644 --- a/MeadeAutostar497/Controller/SerialProcessor.cs +++ b/MeadeAutostar497/Controller/SerialProcessor.cs @@ -1,31 +1,10 @@ using System; using System.IO.Ports; using System.Runtime.InteropServices; -using System.Text; using System.Threading; namespace ASCOM.MeadeAutostar497.Controller { - [ComVisible(false)] - public interface ISerialProcessor - { - bool IsOpen { get; } - bool DtrEnable { get; set; } - bool RtsEnable { get; set; } - int BaudRate { get; set; } - int DataBits { get; set; } - StopBits StopBits { get; set; } - Parity Parity { get; set; } - string PortName { get; set; } - string[] GetPortNames(); - void Open(); - void Close(); - - string CommandTerminated(string command, string terminator); - char CommandChar(string command); - string ReadTerminated(string terminator); - } - [ComVisible(false)] public class SerialProcessor : ISerialProcessor { @@ -134,5 +113,18 @@ namespace ASCOM.MeadeAutostar497.Controller serialMutex.ReleaseMutex(); } } + + public void Command(string command) + { + serialMutex.WaitOne(); + try + { + _serialPort.Write(command); + } + finally + { + serialMutex.ReleaseMutex(); + } + } } } diff --git a/MeadeAutostar497/Controller/TelescopeController.cs b/MeadeAutostar497/Controller/TelescopeController.cs index 2367d75..a2a3f64 100644 --- a/MeadeAutostar497/Controller/TelescopeController.cs +++ b/MeadeAutostar497/Controller/TelescopeController.cs @@ -1,11 +1,6 @@ using System; -using System.Configuration; -using System.Data; using System.IO.Ports; using System.Linq; -using System.Threading; -using ASCOM.Utilities; -using ASCOM.Utilities.Interfaces; namespace ASCOM.MeadeAutostar497.Controller { @@ -98,7 +93,7 @@ namespace ASCOM.MeadeAutostar497.Controller private void TestConnectionActive() { - var firmwareVersionNumber = SerialPort.CommandTerminated(":GVN#", "#"); + var firmwareVersionNumber = SerialPort.CommandTerminated("#:GVN#", "#"); if (string.IsNullOrEmpty(firmwareVersionNumber)) { throw new InvalidOperationException("Failed to communicate with telescope."); @@ -155,10 +150,16 @@ namespace ASCOM.MeadeAutostar497.Controller throw new InvalidOperationException("Failed to set local time"); } - SerialPort.ReadTerminated("#"); - SerialPort.ReadTerminated("#"); + //throwing away these two strings which represent + SerialPort.ReadTerminated("#"); //Updating Planetary Data# + SerialPort.ReadTerminated("#"); // # } } + + public void AbortSlew() + { + SerialPort.Command("#:Q#"); + } } } diff --git a/MeadeAutostar497/MeadeAutostar497.csproj b/MeadeAutostar497/MeadeAutostar497.csproj index 2640107..1fa4160 100644 --- a/MeadeAutostar497/MeadeAutostar497.csproj +++ b/MeadeAutostar497/MeadeAutostar497.csproj @@ -89,6 +89,7 @@ +