-
Notifications
You must be signed in to change notification settings - Fork 2
/
SetupDialogForm.cs
151 lines (130 loc) · 4.58 KB
/
SetupDialogForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using ASCOM.Utilities;
using ASCOM.EQFocuser;
using System.IO.Ports;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ASCOM.EQFocuser
{
[ComVisible(false)] // Form not registered for COM!
public partial class SetupDialogForm : Form
{
public SetupDialogForm(string driverInfo)
{
InitializeComponent();
// Initialise current values of user settings from the ASCOM Profile
InitUI();
lblInfo.Text = "EQFocuser ASCOM Driver " + driverInfo;
}
private void cmdOK_Click(object sender, EventArgs e) // OK button event handler
{
// Place any validation constraint checks here
// Update the state variables with results from the dialogue
Focuser.comPort = (string)comboBoxComPort.SelectedItem;
Focuser.traceState = chkTrace.Checked;
Focuser.showUI = showUI.Checked;
}
private void cmdCancel_Click(object sender, EventArgs e) // Cancel button event handler
{
Close();
}
private void BrowseToAscom(object sender, EventArgs e) // Click on ASCOM logo event handler
{
try
{
System.Diagnostics.Process.Start("http://ascom-standards.org/");
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}
}
private void InitUI()
{
chkTrace.Checked = Focuser.traceState;
showUI.Checked = Focuser.showUI;
// set the list of com ports to those that are currently available
comboBoxComPort.Items.Clear();
String[] ports = SerialPort.GetPortNames();
foreach(string port in ports)
{
Debug.WriteLine("Port here: " + port);
if (DetectEQFocuser(port))
{
comboBoxComPort.Items.Add(port);
}
}
//comboBoxComPort.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames()); // use System.IO because it's static
// select the current port if possible
if (null != Focuser.comPort)
{
if (comboBoxComPort.Items.Contains(Focuser.comPort))
{
comboBoxComPort.SelectedItem = Focuser.comPort;
}
}
}
private bool DetectEQFocuser(string portName)
{
SerialPort testPort = new SerialPort(portName, 115200);
try
{
testPort.Open();
testPort.WriteLine("Z"); // command to get the name of the device
Thread.Sleep(100);
string returnMessage = testPort.ReadExisting().ToString();
testPort.Close();
Debug.WriteLine(returnMessage);
if (returnMessage.Contains("EQFOCUSER_STEPPER") || returnMessage.Contains("POSITION"))
{
Focuser.motorDriver = Focuser.stepperMotor;
lblMotorDriver.Text = lblMotorDriver.Text + " STEPPER";
return true;
}
else if (returnMessage.Contains("EQFOCUSER_SERVO"))
{
Focuser.motorDriver = Focuser.servoMotor;
lblMotorDriver.Text = lblMotorDriver.Text + " SERVO";
return true;
}
else
{
return false;
}
}
catch( Exception e)
{
Debug.WriteLine(e.Message);
return false;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void comboBoxComPort_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void btnReScan_Click(object sender, EventArgs e)
{
this.InitUI();
}
}
}