BioImager  4.9.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
PythonMicroscope.cs
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using System.Diagnostics;
8using System.Windows.Forms;
9using System.IO;
10using AForge;
11namespace BioImager
12{
13 public class PMicroscope
14 {
15 public static Process deviceServer;
16 public static PointD location;
17 public static double focus;
18 public static string run_cmd(string cmd, string args)
19 {
20 ProcessStartInfo start = new ProcessStartInfo();
21 start.FileName = "python.exe";
22 start.Arguments = string.Format("{0} {1}", cmd, args);
23 start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
24 start.UseShellExecute = false;
25 start.RedirectStandardOutput = true;
26 start.CreateNoWindow = true;
27 string res;
28 using (Process process = Process.Start(start))
29 {
30 using (StreamReader reader = process.StandardOutput)
31 {
32 res = reader.ReadToEnd();
33 Console.Write(res);
34 }
35 }
36 Application.DoEvents();
37 return res;
38 }
39 public static void StartCommand(string cmd, string args)
40 {
41 ProcessStartInfo start = new ProcessStartInfo();
42 start.FileName = "python.exe";
43 start.Arguments = string.Format("{0} {1}", cmd, args);
44 start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
45 start.UseShellExecute = false;
46 start.RedirectStandardOutput = true;
47 start.CreateNoWindow = false;
48 }
49 public static void Start()
50 {
51 ProcessStartInfo start = new ProcessStartInfo();
52 start.FileName = "python.exe";
53 start.Arguments = string.Format("{0} {1}", "device_server.py", "config.txt");
54 start.WorkingDirectory = Application.StartupPath + "/PythonMicroscope/microscope";
55 start.UseShellExecute = false;
56 start.RedirectStandardOutput = false;
57 start.CreateNoWindow = false;
58 deviceServer = Process.Start(start);
59 }
60 public static void Stop()
61 {
62 if(deviceServer!=null)
63 if(!deviceServer.HasExited)
64 deviceServer.Kill();
65 }
66 public bool Initialize()
67 {
68 //We start the device server
69 Start();
70 string s = run_cmd("initialize.py", Properties.Settings.Default.PStage);
71 if (!s.Contains("OK"))
72 return false;
73 return true;
74 }
75
76 public bool SetPosition(Point3D p)
77 {
78 string s = run_cmd("setstagexyz.py", Properties.Settings.Default.PStage + " " + p.X + " " + p.Y + " " + p.Z);
79 if (s.Contains("OK"))
80 {
81 focus = p.Z;
82 location = new PointD(p.X, p.Y);
83 return true;
84 }
85 else
86 return false;
87 }
88 public bool SetPosition(PointD p)
89 {
90 string s = run_cmd("setstagexy.py", Properties.Settings.Default.PStage + " " + p.X + " " + p.Y);
91 if (s.Contains("OK"))
92 {
93 location = p;
94 return true;
95 }
96 else
97 return false;
98 }
99 public bool GetPosition3D(out Point3D p, bool update)
100 {
101 if(!update)
102 {
103 p = new Point3D(location.X, location.Y, focus);
104 return true;
105 }
106 string s = run_cmd("getstagexyz.py", Properties.Settings.Default.PStage);
107 if (s.Contains("OK"))
108 {
109 string[] sts = s.Split();
110 p = new Point3D(double.Parse(sts[0]), double.Parse(sts[2]), double.Parse(sts[4]));
111 focus = p.Z;
112 return true;
113 }
114 else
115 {
116 p = new Point3D();
117 return false;
118 }
119 }
120 public bool GetPosition(out PointD p,bool update)
121 {
122 if (!update)
123 {
124 p = location;
125 return true;
126 }
127 string s = run_cmd("getstagexy.py", Properties.Settings.Default.PStage);
128 if (s.Contains("OK"))
129 {
130 string[] sts = s.Split();
131 p = new PointD(double.Parse(sts[0]), double.Parse(sts[2]));
132 location = p;
133 return true;
134 }
135 else
136 {
137 p = new PointD();
138 return false;
139 }
140 }
141 public bool SetFilterWheelPosition(int p)
142 {
143 string s = run_cmd("setfilterwheel.py", Properties.Settings.Default.PFilterWheel + " " + p);
144 if (s.Contains("OK"))
145 return true;
146 else
147 return false;
148 }
149 public bool GetFilterWheelPosition(out int p)
150 {
151 string s = run_cmd("getfilterwheel.py", Properties.Settings.Default.PFilterWheel);
152 if (s.Contains("OK"))
153 {
154 string[] sts = s.Split();
155 p = int.Parse(sts[2]);
156 return true;
157 }
158 else
159 {
160 p = -1;
161 return false;
162 }
163 }
164 public bool TakeImage(string path)
165 {
166 string s = run_cmd("takeimage.py", path);
167 if (s.Contains("OK"))
168 return true;
169 else
170 return false;
171 }
172 public bool SetExposure(float f)
173 {
174 string s = run_cmd("setexposure.py", f.ToString());
175 if (s.Contains("OK"))
176 return true;
177 else
178 return false;
179 }
180 public bool GetSize(out int width, out int height)
181 {
182 string s = run_cmd("getcamrect.py",Properties.Settings.Default.PCamera);
183 width = 0;
184 height = 0;
185 if (!s.Contains("OK"))
186 return false;
187 string[] sts = s.Split();
188 width = int.Parse(sts[0]);
189 height = int.Parse(sts[2]);
190 return true;
191 }
192 }
193}