19 [DllImport(
"PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
20 public static extern int PriorScientificSDK_Version(StringBuilder version);
22 [DllImport(
"PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
23 public static extern int PriorScientificSDK_Initialise();
25 [DllImport(
"PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
26 public static extern int PriorScientificSDK_OpenNewSession();
28 [DllImport(
"PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
29 public static extern int PriorScientificSDK_CloseSession(
int sessionID);
31 [DllImport(
"PriorScientificSDK.dll", CallingConvention = CallingConvention.Cdecl)]
32 public static extern int PriorScientificSDK_cmd(
int session, StringBuilder tx, StringBuilder rx);
38 StringBuilder dllVersion =
new StringBuilder();
46 private void Form1_Load(
object sender, EventArgs e)
49 if ((err = priorSDK.GetVersion(dllVersion)) != Prior.PRIOR_OK)
51 MessageBox.Show(
"Error getting Prior SDK version (" + err.ToString() +
")");
56 if ((err = priorSDK.Initialise()) != Prior.PRIOR_OK)
58 MessageBox.Show(
"Error initialising Prior SDK (" + err.ToString() +
")");
65 if ((sessionID = priorSDK.OpenSession()) < 0)
67 MessageBox.Show(
"Error (" + sessionID.ToString() +
") Creating session in SDK " + dllVersion);
81 open = priorSDK.Cmd(sessionID,
"controller.connect " + port.ToString(), ref userRx,
false);
84 if (open != Prior.PRIOR_OK)
86 MessageBox.Show(
"Error (" + open.ToString() +
") connecting to stage controller ",
"",
87 MessageBoxButtons.OK, MessageBoxIcon.Error);
98 err = priorSDK.Cmd(sessionID,
"controller.stage.hostdirection.set -1 1", ref userRx);
102 private void Form1_FormClosing(
object sender, FormClosingEventArgs e)
106 err = priorSDK.Cmd(sessionID,
"controller.disconnect", ref userRx);
107 err = priorSDK.CloseSession(sessionID);
110 private void systemCheck()
114 err = priorSDK.Cmd(sessionID,
"controller.stage.name.get", ref userRx);
116 err = priorSDK.Cmd(sessionID,
"controller.z.name.get", ref userRx);
119 public int InitialiseStage()
125 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.move-at-velocity " +
126 (-10000).ToString() +
" " + (-10000).ToString(), ref userRx)) != Prior.PRIOR_OK)
131 waitUntilStageIdle();
134 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.position.set 0 0", ref userRx)) != Prior.PRIOR_OK)
138 waitUntilStageIdle();
140 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.goto-position 1000 1000", ref userRx)) != Prior.PRIOR_OK)
147 public bool SetPosition(PointD p)
149 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.position.set "+ p.X +
" " + p.Y, ref userRx)) != Prior.PRIOR_OK)
153 waitUntilStageIdle();
156 public PointD GetPosition()
158 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.position.get", ref userRx)) != Prior.PRIOR_OK)
160 return new PointD(0, 0);
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);
167 public bool SetPosition(Point3D p)
169 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.position.set " + p.X +
" " + p.Y, ref userRx)) != Prior.PRIOR_OK)
173 waitUntilStageIdle();
174 if ((err = priorSDK.Cmd(sessionID,
"controller.z.position.set " + p.Z, ref userRx)) != Prior.PRIOR_OK)
176 throw new Exception(err.ToString());
178 waitUntilStageIdle();
181 public bool SetObjective(
int p)
183 waitUntilNosePieceIdle();
184 if ((err = priorSDK.Cmd(sessionID,
"controller.nosepiece.goto-position " + p, ref userRx)) != Prior.PRIOR_OK)
188 waitUntilNosePieceIdle();
191 public Point3D GetPosition3D()
193 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.position.get", ref userRx)) != Prior.PRIOR_OK)
195 throw new Exception(err.ToString());
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)
202 throw new Exception(err.ToString());
204 double zx =
double.Parse(userRx);
205 return new Point3D(dx, yx, zx);
207 public bool SetZ(
double d)
210 if ((err = priorSDK.Cmd(sessionID,
"controller.z.position.set " + d, ref userRx)) != Prior.PRIOR_OK)
212 throw new Exception(err.ToString());
219 if ((err = priorSDK.Cmd(sessionID,
"controller.z.position.get", ref userRx)) != Prior.PRIOR_OK)
221 throw new Exception(err.ToString());
223 return double.Parse(userRx);
225 public bool SetNosePiece(
int d)
227 waitUntilNosePieceIdle();
228 if ((err = priorSDK.Cmd(sessionID,
"controller.nosepiece.position.set " + d, ref userRx)) != Prior.PRIOR_OK)
230 throw new Exception(err.ToString());
232 waitUntilNosePieceIdle();
235 public int GetNosePiece()
237 if ((err = priorSDK.Cmd(sessionID,
"controller.nosepiece.position.get", ref userRx)) != Prior.PRIOR_OK)
239 throw new Exception(err.ToString());
241 return int.Parse(userRx);
243 public int GetNosePiecePositions()
245 if ((err = priorSDK.Cmd(sessionID,
"controller.nosepiece.no-of-positions.get", ref userRx)) != Prior.PRIOR_OK)
247 throw new Exception(err.ToString());
249 return int.Parse(userRx);
251 public int stageBusy()
253 if ((err = priorSDK.Cmd(sessionID,
"controller.stage.busy.get", ref userRx)) != Prior.PRIOR_OK)
258 return Convert.ToInt32(userRx);
263 if ((err = priorSDK.Cmd(sessionID,
"controller.z.busy.get", ref userRx)) != Prior.PRIOR_OK)
268 return Convert.ToInt32(userRx);
270 public int nosePieceBusy()
272 if ((err = priorSDK.Cmd(sessionID,
"controller.nosepiece.busy.get", ref userRx)) != Prior.PRIOR_OK)
277 return Convert.ToInt32(userRx);
279 public bool shutterOpen()
281 if ((err = priorSDK.Cmd(sessionID,
"controller.shutter.open", ref userRx)) != Prior.PRIOR_OK)
288 public bool shutterClose()
290 if ((err = priorSDK.Cmd(sessionID,
"controller.shutter.close", ref userRx)) != Prior.PRIOR_OK)
297 public int shutterPosition()
299 if ((err = priorSDK.Cmd(sessionID,
"controller.shutter.state.get", ref userRx)) != Prior.PRIOR_OK)
304 return int.Parse(userRx);
306 public void waitUntilStageIdle()
310 Application.DoEvents();
313 while (stageBusy() != 0);
315 public void waitUntilZIdle()
319 Application.DoEvents();
322 while (zBusy() != 0);
324 public void waitUntilNosePieceIdle()
328 Application.DoEvents();
331 while (nosePieceBusy() != 0);
334 public int GetVersion(StringBuilder version)
336 return PriorScientificSDK_Version(version);
339 public int Initialise()
341 return PriorScientificSDK_Initialise();
344 public int OpenSession()
346 return PriorScientificSDK_OpenNewSession();
349 public int CloseSession(
int sessionID)
351 return PriorScientificSDK_CloseSession(sessionID);
354 public int Cmd(
int session,
string usertx, ref
string userrx,
bool displayError =
true)
358 StringBuilder tx =
new StringBuilder();
359 StringBuilder rx =
new StringBuilder();
362 ret = PriorScientificSDK_cmd(session,tx,rx);
364 if (ret == Prior.PRIOR_OK)
366 userrx = rx.ToString();
372 MessageBox.Show(
"Sent " + usertx +
"\rSDK error: " + ret.ToString() );