BioImager  5.0.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
SDK.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading;
9using System.Windows.Forms;
10using AForge;
11using System.Runtime.CompilerServices;
12using System.Runtime.InteropServices;
13
14namespace BioImager
15{
16 /* SDK is C# class wrapper for PriorScientificSDK DLL */
17 public class SDK
18 {
19 [DllImport("PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
20 public static extern int PriorScientificSDK_Version(StringBuilder version);
21
22 [DllImport("PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
23 public static extern int PriorScientificSDK_Initialise();
24
25 [DllImport("PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
26 public static extern int PriorScientificSDK_OpenNewSession();
27
28 [DllImport("PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
29 public static extern int PriorScientificSDK_CloseSession(int sessionID);
30
31 [DllImport("PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
32 public static extern int PriorScientificSDK_cmd(int session, StringBuilder tx, StringBuilder rx);
33 int err;
34 int sessionID = -1;
35
36
37 string userRx = "";
38 StringBuilder dllVersion = new StringBuilder();
39
40 /* create a c# wrapper class for the Prior DLL */
41 SDK priorSDK = new SDK();
42 public SDK()
43 {
44
45 }
46 private void Form1_Load(object sender, EventArgs e)
47 {
48 /* get the version number of the dll */
49 if ((err = priorSDK.GetVersion(dllVersion)) != Prior.PRIOR_OK)
50 {
51 MessageBox.Show("Error getting Prior SDK version (" + err.ToString() + ")");
52 return;
53 }
54 /* SDK must be initialised before any real use
55 */
56 if ((err = priorSDK.Initialise()) != Prior.PRIOR_OK)
57 {
58 MessageBox.Show("Error initialising Prior SDK (" + err.ToString() + ")");
59 return;
60 }
61
62 /* create a session in the DLL, this gives us one controller and currently an ODS and SL160 robot loader.
63 * Multiple connections allow control of multiple stage/loaders but is outside the brief for this demo
64 */
65 if ((sessionID = priorSDK.OpenSession()) < 0)
66 {
67 MessageBox.Show("Error (" + sessionID.ToString() + ") Creating session in SDK " + dllVersion);
68 return;
69 }
70
71 //specify path name or PriorSDK.log is written to working directory
72 //priorSDK.Cmd(sessionID, "dll.log.on", ref userRx);
73
74
75 /* my controller identifies on COM1, yours will probably be different.
76 */
77 int port = 1;
78 int open = 0;
79
80 /* try to connect to the ps3 */
81 open = priorSDK.Cmd(sessionID, "controller.connect " + port.ToString(), ref userRx, false);
82
83
84 if (open != Prior.PRIOR_OK)
85 {
86 MessageBox.Show("Error (" + open.ToString() + ") connecting to stage controller ", "",
87 MessageBoxButtons.OK, MessageBoxIcon.Error);
88 return;
89 }
90
91 systemCheck();
92
93 /* set orientation of stage +x+y = stage left and stage forward
94 * this gives us a co-ordinate/movement system that when viewed through objectives gives positions
95 * as you would see on graph paper. Just a personal preference, you can set host direction as you wish
96 * default is 1 1. +ve incrementing positions moves stage physically left and forwards
97 */
98 err = priorSDK.Cmd(sessionID, "controller.stage.hostdirection.set -1 1", ref userRx);
99
100 }
101
102 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
103 {
104
105 /* disconnect the controller and close the session down */
106 err = priorSDK.Cmd(sessionID, "controller.disconnect", ref userRx);
107 err = priorSDK.CloseSession(sessionID);
108 }
109
110 private void systemCheck()
111 {
112 /* im just doing the calls here as example but its a good idea to check the devices fitted */
113
114 err = priorSDK.Cmd(sessionID, "controller.stage.name.get", ref userRx);
115
116 err = priorSDK.Cmd(sessionID, "controller.z.name.get", ref userRx);
117 }
118
119 public int InitialiseStage()
120 {
121 /* do stage initialisation, goto back right limits (could be anywhere you wish it to be in reality )
122 * default positional units are in steps of 1micron
123 */
124
125 if ((err = priorSDK.Cmd(sessionID, "controller.stage.move-at-velocity " +
126 (-10000).ToString() + " " + (-10000).ToString(), ref userRx)) != Prior.PRIOR_OK)
127 {
128 return 1;
129 }
130
132
133 /* set temp zero pos */
134 if ((err = priorSDK.Cmd(sessionID, "controller.stage.position.set 0 0", ref userRx)) != Prior.PRIOR_OK)
135 {
136 return 1;
137 }
139 /* move off slightly */
140 if ((err = priorSDK.Cmd(sessionID, "controller.stage.goto-position 1000 1000", ref userRx)) != Prior.PRIOR_OK)
141 {
142 return 1;
143 }
144
145 return 0;
146 }
147 public bool SetPosition(PointD p)
148 {
149 if ((err = priorSDK.Cmd(sessionID, "controller.stage.position.set "+ p.X + " " + p.Y, ref userRx)) != Prior.PRIOR_OK)
150 {
151 return false;
152 }
154 return true;
155 }
157 {
158 if ((err = priorSDK.Cmd(sessionID, "controller.stage.position.get", ref userRx)) != Prior.PRIOR_OK)
159 {
160 return new PointD(0, 0);
161 }
162 string[] sts = userRx.Split(' ');
163 double dx = double.Parse(sts[0]);
164 double yx = double.Parse(sts[1]);
165 return new PointD(dx,yx);
166 }
167 public bool SetPosition(Point3D p)
168 {
169 if ((err = priorSDK.Cmd(sessionID, "controller.stage.position.set " + p.X + " " + p.Y, ref userRx)) != Prior.PRIOR_OK)
170 {
171 return false;
172 }
174 if ((err = priorSDK.Cmd(sessionID, "controller.z.position.set " + p.Z, ref userRx)) != Prior.PRIOR_OK)
175 {
176 throw new Exception(err.ToString());
177 }
179 return true;
180 }
181 public bool SetObjective(int p)
182 {
184 if ((err = priorSDK.Cmd(sessionID, "controller.nosepiece.goto-position " + p, ref userRx)) != Prior.PRIOR_OK)
185 {
186 return false;
187 }
189 return true;
190 }
191 public Point3D GetPosition3D()
192 {
193 if ((err = priorSDK.Cmd(sessionID, "controller.stage.position.get", ref userRx)) != Prior.PRIOR_OK)
194 {
195 throw new Exception(err.ToString());
196 }
197 string[] sts = userRx.Split(' ');
198 double dx = double.Parse(sts[0]);
199 double yx = double.Parse(sts[1]);
200 if ((err = priorSDK.Cmd(sessionID, "controller.z.position.get", ref userRx)) != Prior.PRIOR_OK)
201 {
202 throw new Exception(err.ToString());
203 }
204 double zx = double.Parse(userRx);
205 return new Point3D(dx, yx, zx);
206 }
207 public bool SetZ(double d)
208 {
210 if ((err = priorSDK.Cmd(sessionID, "controller.z.position.set " + d, ref userRx)) != Prior.PRIOR_OK)
211 {
212 throw new Exception(err.ToString());
213 }
215 return true;
216 }
217 public double GetZ()
218 {
219 if ((err = priorSDK.Cmd(sessionID, "controller.z.position.get", ref userRx)) != Prior.PRIOR_OK)
220 {
221 throw new Exception(err.ToString());
222 }
223 return double.Parse(userRx);
224 }
225 public bool SetNosePiece(int d)
226 {
228 if ((err = priorSDK.Cmd(sessionID, "controller.nosepiece.position.set " + d, ref userRx)) != Prior.PRIOR_OK)
229 {
230 throw new Exception(err.ToString());
231 }
233 return true;
234 }
235 public int GetNosePiece()
236 {
237 if ((err = priorSDK.Cmd(sessionID, "controller.nosepiece.position.get", ref userRx)) != Prior.PRIOR_OK)
238 {
239 throw new Exception(err.ToString());
240 }
241 return int.Parse(userRx);
242 }
244 {
245 if ((err = priorSDK.Cmd(sessionID, "controller.nosepiece.no-of-positions.get", ref userRx)) != Prior.PRIOR_OK)
246 {
247 throw new Exception(err.ToString());
248 }
249 return int.Parse(userRx);
250 }
251 public int stageBusy()
252 {
253 if ((err = priorSDK.Cmd(sessionID, "controller.stage.busy.get", ref userRx)) != Prior.PRIOR_OK)
254 {
255 return 0;
256 }
257 else
258 return Convert.ToInt32(userRx);
259 }
260
261 public int zBusy()
262 {
263 if ((err = priorSDK.Cmd(sessionID, "controller.z.busy.get", ref userRx)) != Prior.PRIOR_OK)
264 {
265 return 0;
266 }
267 else
268 return Convert.ToInt32(userRx);
269 }
270 public int nosePieceBusy()
271 {
272 if ((err = priorSDK.Cmd(sessionID, "controller.nosepiece.busy.get", ref userRx)) != Prior.PRIOR_OK)
273 {
274 return 0;
275 }
276 else
277 return Convert.ToInt32(userRx);
278 }
279 public bool shutterOpen()
280 {
281 if ((err = priorSDK.Cmd(sessionID, "controller.shutter.open", ref userRx)) != Prior.PRIOR_OK)
282 {
283 return false;
284 }
285 else
286 return true;
287 }
288 public bool shutterClose()
289 {
290 if ((err = priorSDK.Cmd(sessionID, "controller.shutter.close", ref userRx)) != Prior.PRIOR_OK)
291 {
292 return false;
293 }
294 else
295 return true;
296 }
297 public int shutterPosition()
298 {
299 if ((err = priorSDK.Cmd(sessionID, "controller.shutter.state.get", ref userRx)) != Prior.PRIOR_OK)
300 {
301 return 0;
302 }
303 else
304 return int.Parse(userRx);
305 }
306 public void waitUntilStageIdle()
307 {
308 do
309 {
310 Application.DoEvents();
311 Thread.Sleep(100);
312 }
313 while (stageBusy() != 0);
314 }
315 public void waitUntilZIdle()
316 {
317 do
318 {
319 Application.DoEvents();
320 Thread.Sleep(100);
321 }
322 while (zBusy() != 0);
323 }
325 {
326 do
327 {
328 Application.DoEvents();
329 Thread.Sleep(100);
330 }
331 while (nosePieceBusy() != 0);
332 }
333
334 public int GetVersion(StringBuilder version)
335 {
336 return PriorScientificSDK_Version(version);
337 }
338
339 public int Initialise()
340 {
342 }
343
344 public int OpenSession()
345 {
347 }
348
349 public int CloseSession(int sessionID)
350 {
351 return PriorScientificSDK_CloseSession(sessionID);
352 }
353
354 public int Cmd(int session, string usertx, ref string userrx, bool displayError = true)
355 {
356 int ret;
357
358 StringBuilder tx = new StringBuilder();
359 StringBuilder rx = new StringBuilder();
360
361 tx.Append(usertx);
362 ret = PriorScientificSDK_cmd(session,tx,rx);
363
364 if (ret == Prior.PRIOR_OK)
365 {
366 userrx = rx.ToString();
367 }
368 else
369 {
370 if (displayError)
371 {
372 MessageBox.Show("Sent " + usertx + "\rSDK error: " + ret.ToString() );
373 }
374 }
375
376 return ret;
377 }
378 }
379}
System.Windows.Forms.Application Application
Definition ImageView.cs:14
AForge.PointD PointD
const int PRIOR_OK
SDK ERROR CODES returned directly by the SDk.
Definition constants.cs:13
bool SetPosition(Point3D p)
Definition SDK.cs:167
static int PriorScientificSDK_CloseSession(int sessionID)
bool SetObjective(int p)
Definition SDK.cs:181
int GetNosePiece()
Definition SDK.cs:235
Point3D GetPosition3D()
Definition SDK.cs:191
PointD GetPosition()
Definition SDK.cs:156
int Initialise()
Definition SDK.cs:339
static int PriorScientificSDK_OpenNewSession()
int shutterPosition()
Definition SDK.cs:297
void waitUntilStageIdle()
Definition SDK.cs:306
bool shutterOpen()
Definition SDK.cs:279
int CloseSession(int sessionID)
Definition SDK.cs:349
int nosePieceBusy()
Definition SDK.cs:270
double GetZ()
Definition SDK.cs:217
int Cmd(int session, string usertx, ref string userrx, bool displayError=true)
Definition SDK.cs:354
static int PriorScientificSDK_Initialise()
int OpenSession()
Definition SDK.cs:344
void waitUntilZIdle()
Definition SDK.cs:315
void waitUntilNosePieceIdle()
Definition SDK.cs:324
bool shutterClose()
Definition SDK.cs:288
int stageBusy()
Definition SDK.cs:251
bool SetPosition(PointD p)
Definition SDK.cs:147
int InitialiseStage()
Definition SDK.cs:119
int GetNosePiecePositions()
Definition SDK.cs:243
bool SetZ(double d)
Definition SDK.cs:207
static int PriorScientificSDK_Version(StringBuilder version)
int GetVersion(StringBuilder version)
Definition SDK.cs:334
int zBusy()
Definition SDK.cs:261
bool SetNosePiece(int d)
Definition SDK.cs:225
static int PriorScientificSDK_cmd(int session, StringBuilder tx, StringBuilder rx)