ZebraPrinter.cs
5.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
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
namespace DeviceLibrary
{
class ZebraPrinter
{
Connection printerConnection;
/// <summary>
///
/// </summary>
/// <param name="printname">"ZDesigner ZD420-300dpi ZPL"</param>
public ZebraPrinter(string printname) {
printerConnection = new DriverPrinterConnection(printname);
//Open();
}
~ZebraPrinter()
{
Close();
}
public bool Open(out string msg) {
bool rtn = false;
msg = "";
if (!printerConnection.Connected)
{
try
{
printerConnection.Open();
rtn = true;
}
catch (ConnectionException e)
{
msg = "Connection Error:" + e.Message;
}
catch (IOException e)
{
msg = "IO Error:" + e.Message;
}
catch (ZebraPrinterLanguageUnknownException e)
{
msg = "Connection Error:" + e.Message;
}
catch (Exception e)
{
msg = "Error:" + e.Message;
}
}
else {
rtn = true;
}
if (!string.IsNullOrEmpty(msg))
log(msg);
return rtn;
}
void log(string msg) {
LogUtil.info("ZebraPrinter: " + msg);
}
public void Close() {
if (printerConnection != null)
printerConnection.Close();
}
public bool HasLabel
{
get {
Open(out _);
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;
}
}
public bool HasPaper
{
get
{
Open(out _);
string d = "! U1 getvar \"sensor.paper_supply\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
log("sensor.paper_supply:"+rs);
return rs.IndexOf("ok") > -1;
}
}
bool Cover_Open
{
get
{
Open(out _);
string d = "! U1 getvar \"sensor.cover_open\"\n";
var b = Encoding.ASCII.GetBytes(d);
var rd = printerConnection.SendAndWaitForResponse(b, 5000, 500, null);
var rs = Encoding.ASCII.GetString(rd);
log("sensor.cover_open:" + rs);
return rs.IndexOf("yes") > -1;
}
}
void test()
{
Connection printerConnection = null;
try
{
printerConnection = new DriverPrinterConnection("ZDesigner ZD420-300dpi ZPL");
//var dp= new DriverPrinterConnection("ZD420 / Text Only");
//printerConnection = connectionSelector.GetConnection();
printerConnection.Open();
string d = "~PH";
//string d = "! U1 getvar \"sensor.peeler\"";
// d = @"^XA
//^FO100,100^BY3
//^B1N,N,150,Y,N
//^FD123456^FS
//^XZ
//";
//var b = Encoding.ASCII.GetBytes(d);
//printerConnection.Write(b);
//var rd = printerConnection.SendAndWaitForResponse(b, 5 * 1000, 5 * 1000,null);
//var rd = printerConnection.SendAndWaitForResponse(b, 5 * 1000, 5 * 1000,null);
//printerConnection.WaitForData(100*1000);
//var rd = printerConnection.Read();
//Console.WriteLine("Response:"+Encoding.ASCII.GetString(rd));
return;
}
catch (ConnectionException e)
{
//MessageBoxCreator.ShowError(e.Message, "Connection Error");
}
catch (IOException e)
{
//MessageBoxCreator.ShowError(e.Message, "IO Error");
}
catch (ZebraPrinterLanguageUnknownException e)
{
//MessageBoxCreator.ShowError(e.Message, "Connection Error");
}
catch (Exception e)
{
//MessageBoxCreator.ShowError(e.Message, "Send File Error");
}
finally
{
try
{
if (printerConnection != null)
printerConnection.Close();
}
catch (ConnectionException) { }
}
}
}
}