-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotControlProcess.cs
176 lines (157 loc) · 4.47 KB
/
RotControlProcess.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Configuration;
using System.Timers;
namespace SDRSharp.Plugin.SatControl
{
public class RotControlProcess
{
static SerialPort _serialPort;
AppSettingsSection config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location).AppSettings;
Timer timer;
public RotInfo rotInfo;
public event Action rotorUpdate;
public class RotInfo
{
public double Az { get; set; }
public double El { get; set; }
}
public RotControlProcess()
{
_serialPort = new SerialPort();
_serialPort.PortName = config.Settings["PortName"].Value;
_serialPort.BaudRate = Convert.ToInt32(config.Settings["BaudRate"].Value);
_serialPort.DataBits = 8;
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
//_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += _serialPort_DataReceived;
timer = new Timer();
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
rotInfo = new RotInfo();
}
private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = _serialPort.ReadLine().Replace("\r", "");
if (data.Length == 10)
{
rotInfo.Az = Convert.ToDouble(data.Substring(1, 4));
rotInfo.El = Convert.ToDouble(data.Substring(6, 4));
}
rotorUpdate?.Invoke();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
_serialPort.Write("C2\r\n"); // Read Az El
}
catch { }
}
public string connect()
{
string ret = "";
try
{
_serialPort.Open();
timer.Start();
}
catch(Exception ex)
{
ret = ex.Message;
}
return ret;
}
public void disconnect()
{
rotateStop();
try
{
_serialPort.Close();
timer.Stop();
}
catch { }
}
public void setPosition(double setAz, double setEl)
{
double threshold = Convert.ToDouble(config.Settings["RotorTolerenceThreshold"].Value);
if(Math.Abs(setAz - rotInfo.Az) > threshold || Math.Abs(setEl-rotInfo.El) > threshold )
try
{
rotateLowSpeed();
string cmd = "W" + setAz.ToString("000") + " " + setEl.ToString("000") + "\r\n";
_serialPort.Write(cmd); // Read Az El
rotateHighSpeed();
}
catch { }
}
public void rotateLowSpeed()
{
try
{
string cmd = "X1\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateHighSpeed()
{
try
{
string cmd = "X4\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateUp()
{
try
{
string cmd = "U\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateDn()
{
try
{
string cmd = "D\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateLeft()
{
try
{
string cmd = "L\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateRight()
{
try
{
string cmd = "R\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
public void rotateStop()
{
try
{
string cmd = "S\r\n";
_serialPort.Write(cmd); // Read Az El
}
catch { }
}
}
}