PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
SEIDrv.cpp
Go to the documentation of this file.
1 #include "SEIDrv.h"
2 #include <string>
3 #include <Windows.h> // for QueryDosDevice
4 #include <chrono>
5 
6 ref struct Globals
7 {
8  static System::Collections::Generic::List<USDigital::A2^> encoders;
9  static System::Collections::Generic::Dictionary<unsigned char, USDigital::A2^> addresses;
10  static unsigned initialized = 0; // how many times initialization was called
11  static long activeCOM = 0;
12 };
13 
14 void enumerateEncoders(long comPort, long mode, int devicesExpected)
15 {
16  System::String^ comPortString = "COM" + System::Convert::ToString(comPort);
17 
18  USDigital::SEIBusManager mgr;
19  USDigital::SEIBus^ mSEIBus = mgr.GetBus(comPortString);
20  mSEIBus->ScanSettings->LimitDeviceCount = devicesExpected;
21  mSEIBus->Initialize((USDigital::InitializationFlags)mode);
22 
23  for (int i = 0; i < mSEIBus->Devices->Length; i++)
24  {
25  USDigital::A2^ a2Dev = (USDigital::A2^)mSEIBus->Devices[i];
26  Globals::encoders.Add(a2Dev);
27  Globals::addresses.Add(a2Dev->Address, a2Dev);
28  }
29  Globals::activeCOM = comPort;
30 }
31 
32 void enumerateEncodersAll(long mode, int devicesExpected)
33 {
34  const unsigned bufferSize = 5000;
35  char lpTargetPath[bufferSize]; // buffer to store the path of the COMPORTS
36  for (long i = 0; i < 255; i++)
37  {
38  std::string str = "COM" + std::to_string(i);
39  DWORD test = QueryDosDevice(str.c_str(), lpTargetPath, bufferSize);
40 
41  if (test != 0) // port exists
42  {
43  //std::cout << str << ": " << lpTargetPath << std::endl;
44  try
45  {
46  enumerateEncoders(i, mode, devicesExpected);
47  }
48  catch (System::UnauthorizedAccessException^)
49  {
50  // port is open by somebody else, ignore it
51  }
52  }
53  }
54 }
55 
56 #define CatchAndReturn1 \
57 catch (System::Exception^ exc) \
58 { \
59  System::Console::WriteLine("A managed exception occurred in InitializeSEI!\n{0}", exc->ToString()); \
60 } \
61 catch (...) \
62 { \
63  System::Console::WriteLine("An unknown exception occurred in InitializeSEI!"); \
64 } \
65 return 1
66 
67 long InitializeSEI(long comm, long mode, int devicesExpected)
68 {
69  if (Globals::initialized > 0)
70  {
71  ++Globals::initialized; // just increase the count
72  return 0;
73  }
74 
75  try
76  {
77  int mRetryLimit = 5;
78  int retryCount = 0;
79  for (int retryCount = 0; retryCount < mRetryLimit; retryCount++)
80  {
81  if (comm == 0)
82  {
83  enumerateEncodersAll(mode, devicesExpected);
84  }
85  else
86  {
87  enumerateEncoders(comm, mode, devicesExpected);
88  }
89  if(GetNumberOfDevices() != devicesExpected && retryCount < mRetryLimit)
90  {
91  System::Console::WriteLine("SEI Retrying...Current number of devices: {0}", GetNumberOfDevices());
92  Globals::encoders.Clear();
93  Globals::addresses.Clear();
94  Sleep(30);
95  }
96  else
97  {
98  break;
99  }
100  }
101  Globals::initialized = 1;
102  return 0;
103  }
105 }
106 
108 {
109  return Globals::initialized;
110 }
111 
113 {
114  return Globals::encoders.Count;
115 }
116 
117 long GetDeviceInfo(long devnum, long& serialnum, long& addr, char* model, char* firmwareVersion)
118 {
119  try
120  {
121  USDigital::A2^ dev = Globals::encoders[devnum];
122  addr = dev->Address;
123  serialnum = dev->SerialNumber;
124  if (model != nullptr)
125  {
126  char* temp = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(dev->Model).ToPointer();
127  strcpy(model, temp);
128  System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)temp);
129  }
130  if (firmwareVersion != nullptr)
131  {
132  char* temp = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(dev->Model).ToPointer();
133  strcpy(firmwareVersion, temp);
134  System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)temp);
135  }
136  return 0;
137  }
139 }
140 
141 void CloseSEI()
142 {
143  --Globals::initialized;
144  if (Globals::initialized == 0) // clean up for real
145  {
146  Globals::encoders.Clear();
147  Globals::addresses.Clear();
148  }
149 
150  System::String^ comPortString = "COM" + System::Convert::ToString(Globals::activeCOM);
151  USDigital::SEIBusManager mgr;
152  USDigital::SEIBus^ mSEIBus = mgr.GetBus(comPortString);
153  mSEIBus->Close();
154  Globals::activeCOM = 0;
155 }
156 
157 
159 {
160  try
161  {
162  if (Globals::encoders.Count > 0)
163  if (Globals::encoders[0]->SEIBus->Strobe())
164  return 0;
165  }
167 }
168 
170 {
171  return 0; // sleeping not exposed in USDigital .net library
172 }
173 
175 {
176  return 0; // sleeping not exposed in USDigital .net library
177 }
178 
180 {
181  try
182  {
183  USDigital::A2^ dev;
184  if (Globals::addresses.TryGetValue(address, dev))
185  {
186  if (dev->SetOrigin())
187  return 0;
188  }
189  }
191 }
192 
193 long A2GetMode(long address, long * mode)
194 {
195  try
196  {
197  USDigital::A2^ dev;
198  if (Globals::addresses.TryGetValue(address, dev))
199  {
200  unsigned char value;
201  bool success = dev->GetCounterMode(value);
202  if (success)
203  {
204  *mode = value;
205  return 0;
206  }
207  }
208  }
210 }
211 
212 long A2SetMode(long address, long mode)
213 {
214  try
215  {
216  USDigital::A2^ dev;
217  if (Globals::addresses.TryGetValue(address, dev))
218  {
219  if (dev->SetCounterMode(mode))
220  return 0;
221  }
222  }
224 }
225 
226 long A2GetResolution(long address, long * res)
227 {
228  try
229  {
230  USDigital::A2^ dev;
231  if (Globals::addresses.TryGetValue(address, dev))
232  {
233  unsigned int value;
234  bool success = dev->GetResolution(value);
235  if (success)
236  {
237  *res = value;
238  return 0;
239  }
240  }
241  }
243 }
244 
245 long A2SetResolution(long address, long res)
246 {
247  try
248  {
249  USDigital::A2^ dev;
250  if (Globals::addresses.TryGetValue(address, dev))
251  {
252  if (dev->SetCounterMode(res))
253  return 0;
254  }
255  }
257 }
258 
259 long A2GetPosition(long address, long * pos)
260 {
261  try
262  {
263  USDigital::A2^ dev;
264  if (Globals::addresses.TryGetValue(address, dev))
265  {
266  // The GetCount(count) signature does not work properly
267  // so we use GetCount(count, status).
268  USDigital::A2::A2EncoderStatus status;
269  unsigned int value;
270  bool success = dev->GetCount(value, status);
271  if (success)
272  {
273  *pos = value;
274  return 0;
275  }
276  }
277  }
279 }
280 
281 long A2SetPosition(long address, long pos)
282 {
283  try
284  {
285  USDigital::A2^ dev;
286  if (Globals::addresses.TryGetValue(address, dev))
287  {
288  if (dev->SetCount(pos))
289  return 0;
290  }
291  }
293 }
bool IsInitialized()
Definition: SEIDrv.cpp:107
#define CatchAndReturn1
Definition: SEIDrv.cpp:56
Phidget_MeshMode mode
Definition: phidget22.h:1332
long GetDeviceInfo(long devnum, long &serialnum, long &addr, char *model, char *firmwareVersion)
Definition: SEIDrv.cpp:117
std::string to_string(ClariusAvailability avail)
for i
long A2SetWakeup()
Definition: SEIDrv.cpp:174
void CloseSEI()
Definition: SEIDrv.cpp:141
void enumerateEncoders(long comPort, long mode, int devicesExpected)
Definition: SEIDrv.cpp:14
void enumerateEncodersAll(long mode, int devicesExpected)
Definition: SEIDrv.cpp:32
long InitializeSEI(long comm, long mode, int devicesExpected)
Definition: SEIDrv.cpp:67
long A2GetMode(long address, long *mode)
Definition: SEIDrv.cpp:193
unsigned long DWORD
Definition: ATC3DGm.h:451
long A2SetOrigin(long address)
Definition: SEIDrv.cpp:179
long A2SetSleep()
Definition: SEIDrv.cpp:169
long A2SetPosition(long address, long pos)
Definition: SEIDrv.cpp:281
const char * address
Definition: phidget22.h:2552
long A2GetPosition(long address, long *pos)
Definition: SEIDrv.cpp:259
const char const char * value
Definition: phidget22.h:5111
long A2SetMode(long address, long mode)
Definition: SEIDrv.cpp:212
long A2SetResolution(long address, long res)
Definition: SEIDrv.cpp:245
long GetNumberOfDevices()
Definition: SEIDrv.cpp:112
long A2GetResolution(long address, long *res)
Definition: SEIDrv.cpp:226
long A2SetStrobe()
Definition: SEIDrv.cpp:158