-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPythonMicroscope.cs
More file actions
193 lines (192 loc) · 6.22 KB
/
PythonMicroscope.cs
File metadata and controls
193 lines (192 loc) · 6.22 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using AForge;
namespace BioImager
{
public class PMicroscope
{
public static Process deviceServer;
public static PointD location;
public static double focus;
public static string run_cmd(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.CreateNoWindow = true;
string res;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
res = reader.ReadToEnd();
Console.Write(res);
}
}
Application.DoEvents();
return res;
}
public static void StartCommand(string cmd, string args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.CreateNoWindow = false;
}
public static void Start()
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "python.exe";
start.Arguments = string.Format("{0} {1}", "device_server.py", "config.txt");
start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
start.UseShellExecute = false;
start.RedirectStandardOutput = false;
start.CreateNoWindow = false;
deviceServer = Process.Start(start);
}
public static void Stop()
{
if(deviceServer!=null)
if(!deviceServer.HasExited)
deviceServer.Kill();
}
public bool Initialize()
{
//We start the device server
Start();
string s = run_cmd("initialize.py", Properties.Settings.Default.PStage);
if (!s.Contains("OK"))
return false;
return true;
}
public bool SetPosition(Point3D p)
{
string s = run_cmd("setstagexyz.py", Properties.Settings.Default.PStage + " " + p.X + " " + p.Y + " " + p.Z);
if (s.Contains("OK"))
{
focus = p.Z;
location = new PointD(p.X, p.Y);
return true;
}
else
return false;
}
public bool SetPosition(PointD p)
{
string s = run_cmd("setstagexy.py", Properties.Settings.Default.PStage + " " + p.X + " " + p.Y);
if (s.Contains("OK"))
{
location = p;
return true;
}
else
return false;
}
public bool GetPosition3D(out Point3D p, bool update)
{
if(!update)
{
p = new Point3D(location.X, location.Y, focus);
return true;
}
string s = run_cmd("getstagexyz.py", Properties.Settings.Default.PStage);
if (s.Contains("OK"))
{
string[] sts = s.Split();
p = new Point3D(double.Parse(sts[0]), double.Parse(sts[2]), double.Parse(sts[4]));
focus = p.Z;
return true;
}
else
{
p = new Point3D();
return false;
}
}
public bool GetPosition(out PointD p,bool update)
{
if (!update)
{
p = location;
return true;
}
string s = run_cmd("getstagexy.py", Properties.Settings.Default.PStage);
if (s.Contains("OK"))
{
string[] sts = s.Split();
p = new PointD(double.Parse(sts[0]), double.Parse(sts[2]));
location = p;
return true;
}
else
{
p = new PointD();
return false;
}
}
public bool SetFilterWheelPosition(int p)
{
string s = run_cmd("setfilterwheel.py", Properties.Settings.Default.PFilterWheel + " " + p);
if (s.Contains("OK"))
return true;
else
return false;
}
public bool GetFilterWheelPosition(out int p)
{
string s = run_cmd("getfilterwheel.py", Properties.Settings.Default.PFilterWheel);
if (s.Contains("OK"))
{
string[] sts = s.Split();
p = int.Parse(sts[2]);
return true;
}
else
{
p = -1;
return false;
}
}
public bool TakeImage(string path)
{
string s = run_cmd("takeimage.py", path);
if (s.Contains("OK"))
return true;
else
return false;
}
public bool SetExposure(float f)
{
string s = run_cmd("setexposure.py", f.ToString());
if (s.Contains("OK"))
return true;
else
return false;
}
public bool GetSize(out int width, out int height)
{
string s = run_cmd("getcamrect.py",Properties.Settings.Default.PCamera);
width = 0;
height = 0;
if (!s.Contains("OK"))
return false;
string[] sts = s.Split();
width = int.Parse(sts[0]);
height = int.Parse(sts[2]);
return true;
}
}
}