-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathYankePrinterDriver.cs
385 lines (350 loc) · 10.7 KB
/
YankePrinterDriver.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DigitalPlatform;
using DigitalPlatform.RFID;
using DigitalPlatform.Text;
namespace PrinterDriver.Yanke
{
/// <summary>
/// 研科 POS 打印机接口
/// </summary>
public class YankePrinterDriver : IPosPrinterDriver
{
// parameters:
// style 附加的子参数
public NormalResult InitializeDriver(string port,
string style)
{
try
{
var baudRateString = StringUtil.GetParameterByPrefix(style, "baudRate", ":");
int baudRate = 38400;
if (string.IsNullOrEmpty(baudRateString) == false)
{
if (Int32.TryParse(baudRateString, out baudRate) == false)
return new NormalResult
{
Value = -1,
ErrorInfo = $"style 字符串 '{style}' 中波特率值 '{baudRateString}' 格式错误,应为一个数字"
};
}
int iport = 0;
if (port.ToLower() == "usb")
{
StringBuilder sb = new StringBuilder();
int count = Printer.EnumUSBDeviceSerials(sb);
if (count > 0)
{
// String serial = sb.ToString();
iport = 13;
baudRate = 0;
}
else
return new NormalResult
{
Value = -1,
ErrorInfo = $"连接打印机 '{port}' 失败: 没有发现 USB 设备"
};
}
else if (port.ToLower().StartsWith("com") == true)
{
string number = port.Substring("com".Length).Trim();
if (Int32.TryParse(number, out iport) == false)
return new NormalResult
{
Value = -1,
ErrorInfo = $"端口号字符串 '{port}' 不合法: 数字部分 '{number}' 不合法"
};
}
else
{
return new NormalResult
{
Value = -1,
ErrorInfo = $"端口号字符串 '{port}' 不合法"
};
}
//函数功能:连接打印机设备
//返回值:0 -- 成功 -1 -- 失败
int ret = Printer.YkOpenDevice(iport, baudRate, 0);
if (ret == -1)
{
return new NormalResult
{
Value = -1,
ErrorInfo = $"连接打印机 '{port}' 失败(iport={iport})"
};
}
return new NormalResult();
}
catch (Exception ex)
{
return new NormalResult
{
Value = -1,
ErrorInfo = $"InitializeDriver() 出现异常: {ExceptionUtil.GetDebugText(ex)}"
};
}
}
public NormalResult ReleaseDriver()
{
try
{
int ret = Printer.YkCloseDevice();
return new NormalResult();
}
catch (Exception ex)
{
return new NormalResult
{
Value = -1,
ErrorInfo = $"ReleaseDriver() 出现异常: {ExceptionUtil.GetDebugText(ex)}"
};
}
}
// parameters:
// action 动作。为 feed/cut/cuthalf/printline/print/空 之一。空等于 print(不自动换行)
// style 附加的子参数
public NormalResult Print(
string action,
string text,
string style)
{
int nRet = 0;
if (action == "getstatus")
return GetStatus(style);
// 初始化打印机
/*
[名称] 初始化打印机
[格式] ASCII码 ESC @
十六进制码 1B 40
十进制码 27 64
[描述] 清除打印缓冲区中的数据,复位打印机模式到电源打开时打印机的有效模式。
[注意] * Memory Swithc的设置不再被检查
* 接收缓冲区中的数据不被清除。
* */
if (action == "init")
{
/*
nRet = Printer.POS_Reset();
if (nRet == Printer.POS_SUCCESS)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "ResetPrinter Fail"
};
*/
//返回值:0 -- 成功 -1 -- 失败
nRet = Printer.YkInitPrinter();
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "InitPrinter Fail"
};
}
if (action == "feed")
{
//返回值:0 -- 成功 -1 -- 失败
nRet = Printer.YkFeedPaper();
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "FeedPaper Fail"
};
}
if (action == "cut")
{
//返回值:0:成功 1:失败
nRet = Printer.CutPaper(0);
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "CutPaper Fail"
};
}
if (action == "cuthalf")
{
//返回值:0:成功 1:失败
nRet = Printer.CutPaper(1);
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "CutPaper Fail"
};
}
if (action == "printline")
{
//返回值:0:成功 1:失败
nRet = Printer.PrintLine(text);
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "PrintLine Fail"
};
}
// action 为空,或者 "print"
//返回值:0:成功 1:失败
nRet = Printer.PrintString(text);
if (nRet == 0)
return new NormalResult();
else
return new NormalResult
{
Value = -1,
ErrorInfo = "PrintString Fail"
};
}
// 获得打印机当前状态
public NormalResult GetStatus(string style)
{
byte[] status = new byte[4];
/*
status[0] = (byte)Printer.YkGetStatus((byte)1);
status[1] = (byte)Printer.YkGetStatus((byte)2);
status[2] = (byte)Printer.YkGetStatus((byte)3);
*/
int ret = Printer.YkGetStatus((byte)4);
if (ret == -1)
return new NormalResult
{
Value = -1,
ErrorInfo = "请求失败"
};
status[3] = (byte)ret;
List<string> errors = new List<string>();
/*
if ((status[0] & 0x40) > 0)
{
//进纸键接通
}
else
{
//进纸键断开
}
if ((status[0] & 0x20) > 0)
{
//等待联机回复错误
}
else
{
//未等待
}
if ((status[0] & 0x08) > 0)
{
//脱机
}
else
{
//联机
}
if ((status[1] & 0x40) > 0)
{
//发生错误
}
else
{
//正常
}
if ((status[1] & 0x20) > 0)
{
//打印机纸用完停止打印
}
else
{
//正常
}
if ((status[1] & 0x08) > 0)
{
//通过进纸键进纸
}
else
{
//不通过进纸键进纸
}
if ((status[1] & 0x04) > 0)
{
//机头抬杠打开
}
else
{
//机头抬杠关闭
}
if ((status[2] & 0x40) > 0)
{
//出现可自动恢复错误
}
else
{
//正常
}
if ((status[2] & 0x20) > 0)
{
//出现不可恢复错误
}
else
{
//正常
}
if ((status[2] & 0x08) > 0)
{
//发生自动切纸错误
}
else
{
//正常
}
if ((status[2] & 0x04) > 0)
{
//发生机械错误
}
else
{
//正常
}
*/
if ((status[3] & 0x60) > 0)
{
//纸尽
errors.Add("paperout");
}
else
{
//正常
}
if ((status[3] & 0x0c) > 0)
{
//纸将尽
errors.Add("paperwillout");
}
else
{
//正常
}
return new NormalResult
{
Value = 0,
ErrorCode = StringUtil.MakePathList(errors)
};
}
}
}