-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.cs
299 lines (264 loc) · 8.43 KB
/
API.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
//--------------------------------------------------------------------
// FILENAME: API.cs
//
// Copyright © 2011 Motorola Solutions, Inc. All rights reserved.
//
// DESCRIPTION: Implements the code which communicates with the
// EMDK for .NET scanner API - Symbol.Barcode2.
//
// NOTES:
//
//
//--------------------------------------------------------------------
using System;
using System.Windows.Forms;
using Symbol.Barcode2;
/// <summary>
/// The namespace for CS_Barcode2Sample1.
/// </summary>
namespace PriceChecker
{
/// <summary>
/// The class which communicates with the EMDK for .NET scanner API
/// - Symbol.Barcode2.
/// </summary>
class API
{
private Barcode2 myBarcode2 = null;
/// <summary>
/// Initialize the Barcode2 object.
/// </summary>
public bool InitBarcode()
{
// If the Barcode2 object is already initialized then fail the initialization.
if (myBarcode2 != null)
{
return false;
}
else // Else initialize the reader.
{
try
{
// Get the device selected by the user.
Device MyDevice = Symbol.Barcode2.Devices.SupportedDevices[0];
// Create the reader, based on selected device.
myBarcode2 = new Barcode2(MyDevice);
// In this sample, we are setting the aim type to trigger.
switch (myBarcode2.Config.Reader.ReaderType)
{
case READER_TYPES.READER_TYPE_IMAGER:
myBarcode2.Config.Reader.ReaderSpecific.ImagerSpecific.AimType = AIM_TYPE.AIM_TYPE_TRIGGER;
break;
case READER_TYPES.READER_TYPE_LASER:
myBarcode2.Config.Reader.ReaderSpecific.LaserSpecific.AimType = AIM_TYPE.AIM_TYPE_TRIGGER;
break;
}
myBarcode2.Config.Reader.Set();
}
catch (OperationFailureException ex)
{
MessageBox.Show(Resources.GetString("InitBarcode")+"\n" +
Resources.GetString("OperationFailure") + "\n" + ex.Message +
"\n" +
Resources.GetString("Result") +" = " + (Results)((uint)ex.Result)
);
return false;
}
catch (InvalidRequestException ex)
{
MessageBox.Show(Resources.GetString("InitBarcode") + "\n" +
Resources.GetString("InvalidRequest") + "\n" +
ex.Message);
return false;
}
catch (InvalidIndexerException ex)
{
MessageBox.Show(Resources.GetString("InitBarcode") + "\n" +
Resources.GetString("InvalidIndexer") + "\n" +
ex.Message);
return false;
};
return true;
}
}
/// <summary>
/// Stop reading and disable/close the Barcode2 object.
/// </summary>
public void TermBarcode()
{
// If we have a reader
if (myBarcode2 != null)
{
try
{
// stop all the notifications.
StopScan();
// Free it up.
myBarcode2.Dispose();
// Make the reference null.
myBarcode2 = null;
}
catch (OperationFailureException ex)
{
MessageBox.Show(Resources.GetString("TermBarcode") + "\n" +
Resources.GetString("OperationFailure") + "\n" + ex.Message +
"\n" +
Resources.GetString("Result") + " = " + (Results)((uint)ex.Result)
);
}
catch (InvalidRequestException ex)
{
MessageBox.Show(Resources.GetString("TermBarcode") + "\n" +
Resources.GetString("InvalidRequest") + "\n" +
ex.Message);
}
catch (InvalidIndexerException ex)
{
MessageBox.Show(Resources.GetString("TermBarcode") + "\n" +
Resources.GetString("InvalidIndexer") + "\n" +
ex.Message);
};
}
}
/// <summary>
/// Start a scan.
/// </summary>
public void StartScan(bool TriggerSoftAlways)
{
if (myBarcode2 != null)
{
try
{
if (TriggerSoftAlways)
{
if (myBarcode2.Config.TriggerMode != TRIGGERMODES.SOFT_ALWAYS)
{
myBarcode2.Config.TriggerMode = TRIGGERMODES.SOFT_ALWAYS;
}
}
else
{
myBarcode2.Config.TriggerMode = TRIGGERMODES.HARD;
}
// By default, the assemly Symbol.Barcode2 will decode the barcodes whose data length is less than or equal to 55.
// The user will have to increase ScanDataSize in order to get any longer lengths decoded.
myBarcode2.Config.ScanDataSize = (int)(Symbol.Barcode2.ReaderDataLengths.MaximumLabel);
// Submit a scan.
myBarcode2.Scan(5000);
}
catch (Symbol.Exceptions.OperationFailureException ex)
{
MessageBox.Show(Resources.GetString("StartScan") + "\n" +
Resources.GetString("OperationFailure") + "\n" + ex.Message +
"\n" +
Resources.GetString("Result") + " = " + (Symbol.Results)((uint)ex.Result));
}
catch (Symbol.Exceptions.InvalidRequestException ex)
{
MessageBox.Show(Resources.GetString("StartScan") + "\n" +
Resources.GetString("InvalidRequest") + "\n" +
ex.Message);
}
catch (Symbol.Exceptions.InvalidIndexerException ex)
{
MessageBox.Show(Resources.GetString("StartScan") + "\n" +
Resources.GetString("InvalidIndexer") + "\n" +
ex.Message);
}
}
}
/// <summary>
/// Stop all reads on the reader.
/// </summary>
public void StopScan()
{
//If we have a reader
if (myBarcode2 != null)
{
try
{
// Flush (Cancel all pending reads).
myBarcode2.ScanCancel();
}
catch (OperationFailureException ex)
{
MessageBox.Show(Resources.GetString("StopScan") + "\n" +
Resources.GetString("OperationFailure") + "\n" + ex.Message +
"\n" +
Resources.GetString("Result") + " = " + (Results)((uint)ex.Result)
);
}
catch (InvalidRequestException ex)
{
MessageBox.Show(Resources.GetString("StopScan") + "\n" +
Resources.GetString("InvalidRequest") + "\n" +
ex.Message);
}
catch (InvalidIndexerException ex)
{
MessageBox.Show(Resources.GetString("StopScan") + "\n" +
Resources.GetString("InvalidIndexer") + "\n" +
ex.Message);
};
}
}
/// <summary>
/// Provides the access to the Symbol.Barcode.Reader reference.
/// The user can use this reference for his additional Reader - related operations.
/// </summary>
public Barcode2 Barcode2
{
get
{
return myBarcode2;
}
}
/// <summary>
/// Attach a ScanNotify handler.
/// </summary>
public void AttachScanNotify(Barcode2.OnScanHandler ScanNotifyHandler)
{
// If we have a reader
if( myBarcode2 != null)
{
// Attach the read notification handler.
myBarcode2.OnScan += ScanNotifyHandler;
}
}
/// <summary>
/// Detach the ScanNotify handler.
/// </summary>
public void DetachScanNotify(Barcode2.OnScanHandler ScanNotifyHandler)
{
if (myBarcode2 != null)
{
// Detach the read notification handler.
myBarcode2.OnScan -= ScanNotifyHandler;
}
}
/// <summary>
/// Attach a StatusNotify handler.
/// </summary>
public void AttachStatusNotify(Barcode2.OnStatusHandler StatusNotifyHandler)
{
// If we have a reader
if( myBarcode2 != null)
{
// Attach status notification handler.
myBarcode2.OnStatus += StatusNotifyHandler;
}
}
/// <summary>
/// Detach a StatusNotify handler.
/// </summary>
public void DetachStatusNotify(Barcode2.OnStatusHandler StatusNotifyHandler)
{
// If we have a reader registered for receiving the status notifications
if (myBarcode2 != null)
{
// Detach the status notification handler.
myBarcode2.OnStatus -= StatusNotifyHandler;
}
}
}
}