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
MicroscopeConsole.cs
Go to the documentation of this file.
1using BioImager;
2using Newtonsoft.Json;
3
4using System.Diagnostics;
5using PointD = AForge.PointD;
6namespace BioImager
7{
8 public static class MicroscopeConsole
9 {
10 public static bool synchronous = true;
11 public static Process console = null;
12 [Serializable]
13 public struct Command
14 {
44 public double[] doubles;
46 public Command()
47 {
48 doubles = null;
49 type = Type.SetStage;
50 }
51 public Command(Type t, double[] ds)
52 {
53 doubles = ds;
54 type = t;
55 }
56 }
57 public static void SetStagePosition(PointD p)
58 {
59 RunCommand(new Command(Command.Type.SetStage, new double[] { p.X, p.Y }));
60 }
61 static void Start()
62 {
63 ProcessStartInfo ps = new ProcessStartInfo();
64#if DEBUG
65 ps.CreateNoWindow = false;
66#else
67 ps.CreateNoWindow = true;
68#endif
69 ps.Arguments = Properties.Settings.Default.LibPath;
70 ps.Arguments = ps.Arguments.Replace(' ', '_');
71 ps.FileName = Application.StartupPath + "MicroscopeConsole\\MicroscopeConsole.exe";
72 ps.UseShellExecute = false;
73 ps.RedirectStandardError = true;
74 ps.RedirectStandardOutput = true;
75 ps.RedirectStandardInput = true;
76 console = Process.Start(ps);
77 }
78 public static Command RunCommand(Command c)
79 {
80 Process[] prs = Process.GetProcessesByName("MicroscopeConsole.exe");
81 if (prs.Length == 1)
82 console = prs[0];
83 if (console == null)
84 {
85 Start();
86 }
87 if (c.type.ToString().StartsWith("Get"))
88 {
89 string arg = JsonConvert.SerializeObject(c);
90 console.StandardInput.WriteLine("Command:" + arg);
91 A:
92 try
93 {
94 do
95 {
96 if (console.HasExited)
97 break;
98 string st = console.StandardOutput.ReadLine();
99 if(st != null)
100 if (st.Length > 0)
101 {
102 return JsonConvert.DeserializeObject<Command>(st);
103 }
104 Thread.Sleep(20);
105 } while (true);
106 }
107 catch (Exception e)
108 {
109 Console.WriteLine(e.Message);
110 goto A;
111 }
112
113 string er = console.StandardError.ReadToEnd();
114 string o = console.StandardOutput.ReadToEnd();
115 Console.WriteLine(er);
116 console = null;
117 return new Command();
118 }
119 else
120 {
121 string arg = JsonConvert.SerializeObject(c);
122 console.StandardInput.WriteLine("Command:" + arg);
123 if (synchronous)
124 {
125 //Now we need to wait till the command is finished.
126 do
127 {
128 if (console.HasExited)
129 break;
130 string st = console.StandardOutput.ReadLine();
131 if (st != null)
132 if (st.Length > 0)
133 {
134 return JsonConvert.DeserializeObject<Command>(st);
135 }
136 Application.DoEvents();
137 } while (true);
138 }
139 return new Command();
140 }
141 }
142 }
143}
System.Windows.Forms.Application Application
Definition ImageView.cs:14
AForge.PointD PointD
static Command RunCommand(Command c)
static void SetStagePosition(PointD p)