-
-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathProgram.cs
More file actions
316 lines (294 loc) · 12.4 KB
/
Program.cs
File metadata and controls
316 lines (294 loc) · 12.4 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
using ESCPOS_NET.Emitters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using System.Threading;
using ESCPOS_NET.Printers;
namespace ESCPOS_NET.ConsoleTest
{
internal class Program
{
private static BasePrinter printer;
private static ICommandEmitter e;
static void Main(string[] args)
{
Console.WriteLine("Welcome to the ESCPOS_NET Test Application!");
Console.Write("Would you like to see all debug messages? (y/n): ");
var response = Console.ReadLine().Trim().ToLowerInvariant();
var logLevel = LogLevel.Information;
if (response.Length >= 1 && response[0] == 'y')
{
Console.WriteLine("Debugging enabled!");
logLevel = LogLevel.Trace;
}
var factory = LoggerFactory.Create(b => b.AddConsole().SetMinimumLevel(logLevel));
var logger = factory.CreateLogger<Program>();
ESCPOS_NET.Logging.Logger = logger;
Console.WriteLine("1 ) Test Serial Port");
Console.WriteLine("2 ) Test Network Printer");
Console.WriteLine("3 ) Test Samba-Shared Printer");
Console.WriteLine("4 ) Test USB Printer");
Console.Write("Choice: ");
string comPort = "";
string ip;
string networkPort;
string smbPath;
string usbPort = string.Empty;
response = Console.ReadLine();
var valid = new List<string> { "1", "2", "3", "4" };
if (!valid.Contains(response))
{
response = "1";
}
int choice = int.Parse(response);
if (choice == 1)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
while (!comPort.StartsWith("COM"))
{
Console.Write("COM Port (enter for default COM5): ");
comPort = Console.ReadLine();
if (string.IsNullOrWhiteSpace(comPort))
{
comPort = "COM5";
}
}
Console.Write("Baud Rate (enter for default 115200): ");
if (!int.TryParse(Console.ReadLine(), out var baudRate))
{
baudRate = 115200;
}
printer = new SerialPrinter(portName: comPort, baudRate: baudRate);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.Write("File / virtual com path (eg. /dev/usb/lp0): ");
comPort = Console.ReadLine();
if (string.IsNullOrWhiteSpace(comPort))
{
comPort = "/dev/usb/lp0";
}
printer = new FilePrinter(filePath: comPort, false);
}
}
else if (choice == 2)
{
Console.Write("IP Address (eg. 192.168.1.240): ");
ip = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ip))
{
ip = "192.168.254.202";
}
Console.Write("TCP Port (enter for default 9100): ");
networkPort = Console.ReadLine();
if (string.IsNullOrWhiteSpace(networkPort))
{
networkPort = "9100";
}
printer = new NetworkPrinter(settings: new NetworkPrinterSettings() { ConnectionString = $"{ip}:{networkPort}" });
}
else if (choice == 3)
{
Console.Write(@"SMB Share Name (eg. \\computer\printer): ");
smbPath = Console.ReadLine();
printer = new SambaPrinter(tempFileBasePath: @"C:\Temp", filePath: smbPath);
}
else if (choice == 4)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var usboptions = DeviceFinder.GetDevices();//gets the usb devices connected to the pc
if (usboptions.Count > 0)
{
int i = 0;
int num = 1;
while (i < usboptions.Count)
{
Console.WriteLine($"{i + num}. Name: {usboptions[i].BusName} S/N: {usboptions[i].SerialNum}");
i++;
//serial number and name for printer. Name reported might just be USB Printing Support or something generic
//the property necessary for printing is Device Path this is just for UI
}
Console.Write("Choose Printer (eg. 1): ");
string c = Console.ReadLine();
if (int.TryParse(c, out int chosen) && chosen > 0)
{
usbPort = usboptions[chosen - 1].DevicePath;
}
}
printer = new USBPrinter(usbPort);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Console.Write("File / usb path (eg. /dev/usb/lp0): ");
usbPort = Console.ReadLine();
if (string.IsNullOrWhiteSpace(usbPort))
{
comPort = "/dev/usb/lp0";
}
printer = new FilePrinter(filePath: usbPort, false);
}
}
bool monitor = false;
Thread.Sleep(500);
if (choice != 3) // SMB printers do not support reads so status back will not work.
{
Console.Write("Turn on Live Status Back Monitoring? (y/n): ");
response = Console.ReadLine().Trim().ToLowerInvariant();
if (response.Length >= 1 && response[0] == 'y')
{
monitor = true;
}
}
e = new EPSON();
var testCases = new Dictionary<Option, string>()
{
{ Option.SingleLinePrinting, "Single Line Printing" },
{ Option.MultiLinePrinting, "Multi-line Printing" },
{ Option.LineSpacing, "Line Spacing" },
{ Option.BarcodeStyles, "Barcode Styles" },
{ Option.BarcodeTypes, "Barcode Types" },
{ Option.TwoDimensionCodes, "2D Codes" },
{ Option.TextStyles, "Text Styles" },
{ Option.FullReceipt, "Full Receipt" },
{ Option.CodePages, "Code Pages (Euro, Katakana, Etc)" },
{ Option.Images, "Images" },
{ Option.LegacyImages, "Legacy Images" },
{ Option.LargeByteArrays, "Large Byte Arrays" },
{ Option.CashDrawerPin2, "Cash Drawer Pin2" },
{ Option.CashDrawerPin5, "Cash Drawer Pin5" },
{ Option.Exit, "Exit" }
};
while (true)
{
foreach (var item in testCases)
{
Console.WriteLine($"{(int)item.Key} : {item.Value}");
}
Console.Write("Execute Test: ");
if (!int.TryParse(Console.ReadLine(), out choice) || !Enum.IsDefined(typeof(Option), choice))
{
Console.WriteLine("Invalid entry. Please try again.");
continue;
}
var enumChoice = (Option)choice;
if (enumChoice == Option.Exit)
{
return;
}
Console.Clear();
if (monitor)
{
printer.Write(e.Initialize());
printer.Write(e.Enable());
printer.Write(e.EnableAutomaticStatusBack());
}
Setup(monitor);
printer?.Write(e.PrintLine($"== [ Start {testCases[enumChoice]} ] =="));
switch (enumChoice)
{
case Option.SingleLinePrinting:
printer.Write(Tests.SingleLinePrinting(e));
break;
case Option.MultiLinePrinting:
printer.Write(Tests.MultiLinePrinting(e));
break;
case Option.LineSpacing:
printer.Write(Tests.LineSpacing(e));
break;
case Option.BarcodeStyles:
printer.Write(Tests.BarcodeStyles(e));
break;
case Option.BarcodeTypes:
printer.Write(Tests.BarcodeTypes(e));
break;
case Option.TwoDimensionCodes:
printer.Write(Tests.TwoDimensionCodes(e));
break;
case Option.TextStyles:
printer.Write(Tests.TextStyles(e));
break;
case Option.FullReceipt:
printer.Write(Tests.Receipt(e));
break;
case Option.Images:
printer.Write(Tests.Images(e, false));
break;
case Option.LegacyImages:
printer.Write(Tests.Images(e, true));
break;
case Option.LargeByteArrays:
try
{
printer.Write(Tests.TestLargeByteArrays(e));
}
catch (Exception e)
{
Console.WriteLine($"Aborting print due to test failure. Exception: {e?.Message}, Stack Trace: {e?.GetBaseException()?.StackTrace}");
}
break;
case Option.CashDrawerPin2:
printer.Write(Tests.CashDrawerOpenPin2(e));
break;
case Option.CashDrawerPin5:
printer.Write(Tests.CashDrawerOpenPin5(e));
break;
default:
Console.WriteLine("Invalid entry.");
break;
}
Setup(monitor);
printer?.Write(e.PrintLine($"== [ End {testCases[enumChoice]} ] =="));
printer?.Write(e.PartialCutAfterFeed(5));
// TODO: also make an automatic runner that runs all tests (command line).
}
}
public enum Option
{
SingleLinePrinting = 1,
MultiLinePrinting,
LineSpacing,
BarcodeStyles,
BarcodeTypes,
TwoDimensionCodes,
TextStyles,
FullReceipt,
CodePages,
Images,
LegacyImages,
LargeByteArrays,
CashDrawerPin2,
CashDrawerPin5,
Exit = 99
}
private static void StatusChanged(object sender, EventArgs ps)
{
var status = (PrinterStatusEventArgs)ps;
if (status == null) { Console.WriteLine("Status was null - unable to read status from printer."); return; }
Console.WriteLine($"Printer Online Status: {status.IsPrinterOnline}");
Console.WriteLine(JsonConvert.SerializeObject(status));
}
private static bool _hasEnabledStatusMonitoring = false;
private static void Setup(bool enableStatusBackMonitoring)
{
if (printer != null)
{
// Only register status monitoring once.
if (!_hasEnabledStatusMonitoring)
{
printer.StatusChanged += StatusChanged;
_hasEnabledStatusMonitoring = true;
}
printer?.Write(e.Initialize());
printer?.Write(e.Enable());
if (enableStatusBackMonitoring)
{
printer.Write(e.EnableAutomaticStatusBack());
}
}
}
}
}