Merged in feature/PortSpeed (pull request #40)

Feature/PortSpeed

* Added support for changing the speed of the autostar

* Added support for changing the speed of the autostar

* Merge remote-tracking branch 'origin/feature/PortSpeed' into feature/PortSpeed

* Upgraded unit testing to make sure that speed changes are properly tested
This commit is contained in:
2022-05-08 11:52:45 +00:00
parent a68e86d8cd
commit 35a76fe52c
4 changed files with 449 additions and 6 deletions
+21 -4
View File
@@ -25,7 +25,7 @@ namespace ASCOM.Meade.net
SetItemsFromEnum(cboStopBits.Items, typeof(SerialStopBits));
SetItemsFromEnum(cboParity.Items, typeof(SerialParity));
SetItemsFromEnumValues(cboSpeed.Items, typeof(SerialSpeed));
SetItemFromEnumValues(cboSpeed.Items, SerialSpeed.ps1200, SerialSpeed.ps57600);
SetItemsFromEnum(cboHandShake.Items, typeof(SerialHandshake));
SetItemsFromEnum(cboParkedBehaviour.Items, typeof(ParkedBehaviour));
}
@@ -41,13 +41,30 @@ namespace ASCOM.Meade.net
}
}
private void SetItemsFromEnumValues(IList items, Type enumItems)
//private void SetItemsFromEnumValues(IList items, Type enumItems)
//{
// items.Clear();
// foreach (int item in Enum.GetValues(enumItems))
// {
// items.Add(item);
// }
//}
private void SetItemFromEnumValues<T>(IList items, T minValue, T maxValue)
{
items.Clear();
foreach (int item in Enum.GetValues(enumItems))
var type = typeof(T);
var intMinValue = (int)Convert.ChangeType(minValue, typeof(int));
var intMaxValue = (int)Convert.ChangeType(maxValue, typeof(int));
foreach (int item in Enum.GetValues(type))
{
items.Add(item);
if ((item >= intMinValue) && (item <= intMaxValue))
items.Add(item);
}
}
+81 -2
View File
@@ -18,7 +18,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ASCOM.DeviceInterface;
@@ -389,8 +389,70 @@ namespace ASCOM.Meade.net
SharedSerial.DataBits = profileProperties.DataBits;
SharedSerial.StopBits = (SerialStopBits)Enum.Parse(typeof(SerialStopBits), profileProperties.StopBits);
SharedSerial.Parity = (SerialParity)Enum.Parse(typeof(SerialParity), profileProperties.Parity);
SharedSerial.Speed = (SerialSpeed)profileProperties.Speed;
SharedSerial.Handshake = (SerialHandshake)Enum.Parse(typeof(SerialHandshake), profileProperties.Handshake);
SharedSerial.Speed = SerialSpeed.ps9600;
var wantedSpeed = (SerialSpeed)profileProperties.Speed;
if (wantedSpeed != SerialSpeed.ps9600)
{
SharedSerial.Speed = wantedSpeed;
SharedSerial.Connected = true;
var speedRampNeeded = false;
//Test if communication is working.
try
{
string utcOffSet = SendString("GG");
}
catch (Exception)
{
speedRampNeeded = true;
}
if (speedRampNeeded)
{
SharedSerial.Connected = false;
SharedSerial.Speed = SerialSpeed.ps9600;
SharedSerial.Connected = true;
int newSpeedIndex = GetSpeedIndex(wantedSpeed);
//:SBn# Set Baud Rate n, where n is an ASCII digit (1..9) with the following interpertation
// 1 56.7K
// 2 38.4K
// 3 28.8K
// 4 19.2K
// 5 14.4K
// 6 9600
// 7 4800
// 8 2400
// 9 1200
//Returns:
// 1 At the current baud rate and then changes to the new rate for further communication
//SendBlind($"SB{newSpeedIndex}");
try
{
var speedChanged = SendChar($"SB{newSpeedIndex}");
if (speedChanged == "1")
{
SharedSerial.Connected = false;
SharedSerial.Speed = wantedSpeed;
traceLogger.LogIssue("Connect",
$"Telescope serial port speed change, connecting at {SharedSerial.Speed}.");
}
else
{
throw new Exception("Autostar not signalled speed change.");
}
}
catch (Exception)
{
SharedSerial.Connected = false;
SharedSerial.Speed = SerialSpeed.ps9600;
traceLogger.LogIssue("Connect", $"Telescope not responding to speed change, connecting at {SharedSerial.Speed}.");
}
}
}
SharedSerial.Connected = true;
try
@@ -451,6 +513,23 @@ namespace ASCOM.Meade.net
}
}
private static int GetSpeedIndex(SerialSpeed speed)
{
switch (speed)
{
case SerialSpeed.ps57600: return 1;
case SerialSpeed.ps38400: return 2;
case SerialSpeed.ps28800: return 3;
case SerialSpeed.ps19200: return 4;
case SerialSpeed.ps14400: return 5;
case SerialSpeed.ps9600: return 6;
case SerialSpeed.ps4800: return 7;
case SerialSpeed.ps2400: return 8;
case SerialSpeed.ps1200: return 9;
default: throw new NotSupportedException($"{SpeedDefault} not supported");
}
}
public static void Disconnect(string deviceId, string driverId)
{
lock (LockObject)