ZebraManger.cs
9.2 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Graphics;
using Zebra.Sdk.Printer;
using Zebra.Sdk.Printer.Discovery;
namespace ZebraPrinterHelper
{
public class ZebraManger
{
ConnectionType ConnectionType;
string ConnectionEndpoint;
public int PrinterDPI=203;
/// <summary>
/// 初始化打印机
/// </summary>
/// <param name="endpoint">连接地址</param>
/// <param name="connectionType">连接类型</param>
public void Init(string endpoint, ConnectionType connectionType) {
ConnectionEndpoint = endpoint;
ConnectionType = connectionType;
}
Connection printerConnection = null;
Connection GetConnection()
{
if (ConnectionType == ConnectionType.Network)
{
try
{
int port = 9100;
return new TcpConnection(ConnectionEndpoint, port);
}
catch (Exception e)
{
throw new ConnectionException(e.Message, e);
}
}
else if (ConnectionType == ConnectionType.UsbDirect)
{
try
{
return new UsbConnection(ConnectionEndpoint);
}
catch (Exception e)
{
throw new ConnectionException(e.Message, e);
}
}
else if (ConnectionType == ConnectionType.USB)
{
return new DriverPrinterConnection(ConnectionEndpoint);
}
else
{
try
{
return new BluetoothConnection(ConnectionEndpoint);
}
catch (Exception e)
{
throw new ConnectionException(e.Message, e);
}
}
}
/// <summary>
/// 连接打印机
/// </summary>
/// <param name="msg">错误消息</param>
/// <returns>是否成功</returns>
public bool Connection(out string msg) {
msg = "";
try
{
printerConnection = GetConnection();
printerConnection.Open();
PrinterDPI = GetPrinterDPI();
return true;
}
catch (ConnectionException e)
{
msg = "Connection Error:" + e.Message;
}
catch (ZebraPrinterLanguageUnknownException e)
{
msg = "Connection Error:" + e.Message;
}
catch (IOException e)
{
msg = "Image Error" + e.Message;
}
catch (ZebraIllegalArgumentException e)
{
msg = "Illegal Arguments" + e.Message;
}
catch (ArgumentException e)
{
msg = "Invalid File Path" + e.Message;
}
finally
{
}
return false;
}
/// <summary>
/// 关闭打印机连接
/// </summary>
public void Close() {
try
{
if (printerConnection != null)
{
printerConnection.Close();
printerConnection = null;
}
}
catch (ConnectionException)
{
}
finally
{
}
}
~ZebraManger() {
Close();
}
/// <summary>
/// 打印图像
/// </summary>
/// <param name="bmp"></param>
/// <param name="msg">错误消息</param>
/// <returns>是否成功</returns>
public bool PrintImage(Bitmap bmp, out string msg)
{
if (!CheckAndConnection(out msg))
return false;
CancelAll();
var status = GetStatus();
if (!status.isReadyToPrint) {
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
msg = string.Join("\r\n", printerStatusString);
return false;
}
if (IsLabelOnPeeler) {
msg = "上一个标签尚未移走";
return false;
}
try
{
ZebraImageI image = ZebraImageFactory.GetImage(bmp);
ZebraPrinterFactory.GetInstance(printerConnection).PrintImage(image, 0, 0, 0, 0, false);
return true;
}
catch (ConnectionException e)
{
msg = "Connection Error:" + e.Message;
}
catch (ZebraPrinterLanguageUnknownException e)
{
msg = "Connection Error:" + e.Message;
}
catch (IOException e)
{
msg = "Image Error" + e.Message;
}
catch (ZebraIllegalArgumentException e)
{
msg = "Illegal Arguments"+e.Message;
}
catch (ArgumentException e)
{
msg = "Invalid File Path" + e.Message;
}
finally
{
}
return false;
}
bool CheckAndConnection(out string msg) {
msg = "";
if (printerConnection == null || !printerConnection.Connected)
{
if (!Connection(out string m))
{
msg = m;
return false;
}
}
return true;
}
public bool IsLabelOnPeeler
{
get
{
try
{
string d = "! U1 getvar \"sensor.peeler\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
//log("sensor.peeler" + rs);
return rs.IndexOf("not clear") > -1;
}
catch {
return false;
}
}
}
public int GetPrinterDPI()
{
try
{
string d = "! U1 getvar \"head.resolution.in_dpi\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
if (rs.Length > 0)
{
rs = rs.Trim('\"');
return int.Parse(rs);
}
return 203;
}
catch
{
return 203;
}
}
public void CancelAll() {
try
{
string d = "~JA\n";
var b = Encoding.ASCII.GetBytes(d);
printerConnection.Write(b);
}
catch
{
}
}
public void PauseMode()
{
try
{
string d = "~PP\n";
var b = Encoding.ASCII.GetBytes(d);
printerConnection.Write(b);
}
catch
{
}
}
public void PrintStartMode()
{
string d = "~PS\n";
var b = Encoding.ASCII.GetBytes(d);
printerConnection.Write(b);
}
public bool IsPrinterReady(out string msg) {
msg = "";
var status = GetStatus();
string[] printerStatusString = new PrinterStatusMessages(status).GetStatusMessage();
if (printerStatusString.Length > 0)
{
msg = string.Join("\r\n", printerStatusString);
return false;
}
if (status.numberOfFormatsInReceiveBuffer > 0)
{
msg = "打印机缓冲池内有未打印文档";
return false;
}
return true;
}
public PrinterStatus GetStatus() {
if (printerConnection == null || !printerConnection.Connected)
{
if (!Connection(out string m))
{
//msg = m;
//return false;
}
}
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection);
ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);
return (linkOsPrinter != null) ? linkOsPrinter.GetCurrentStatus() : printer.GetCurrentStatus();
}
}
public enum ConnectionType
{
[Description("Network")]
Network,
[Description("USB")]
USB,
[Description("USB Direct")]
UsbDirect,
[Description("Bluetooth")]
Bluetooth
}
}