-
Notifications
You must be signed in to change notification settings - Fork 0
/
PT.cs
303 lines (255 loc) · 11.2 KB
/
PT.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.PointOfService;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Data;
using System.Threading;
namespace ConsoleApplication1
{
public class PT
{
private Thread pThread;
private Printer printer;
private PosPrinter m_Printer;
private bool mStop;
public void PrinterThread() //
{
mStop = false;
}
public void begin() //开始打印
{
pThread = new Thread(new ThreadStart(run));
mStop = false;
pThread.Start();
}
public void run() //运行打印
{
Printer printer = new Printer();
printer = new Printer();
bool initialized = false;
while (!mStop)
{
if (!initialized)
{
initialized = printer.init(true);
}
else
{
m_Printer = printer.getPrinter();
}
//System.Threading.Thread.Sleep(1000 * 60);
printer.print(1);
Stop();
}
}
public void Stop() //停止打印
{
mStop = true;
if (m_Printer != null)
{
m_Printer.Close();
}
}
}
public class Printer
{
const int MAX_LINE_WIDTHS = 2;
PosPrinter m_Printer = null;
bool async = false;
public bool init(bool asasynchronous) //初始化
{
async = asasynchronous;
string strLogicalName = "PosPrinter";
try
{
PosExplorer posExplorer = new PosExplorer();
DeviceInfo deviceInfo = null;
try
{
deviceInfo = posExplorer.GetDevice(DeviceType.PosPrinter, strLogicalName);
m_Printer = (PosPrinter)posExplorer.CreateInstance(deviceInfo);
}
catch (Exception)
{
//return false;
}
m_Printer.Open();
m_Printer.Claim(1000);
m_Printer.DeviceEnabled = true;
m_Printer.RecLetterQuality = true;
m_Printer.MapMode = MapMode.Metric;
}
catch (Exception ex)
{
System.IO.File.AppendAllText(@"C:\hot.txt", DateTime.Now.ToString() + ": " + ex.ToString() + "\r\n");
return false;
}
return true;
}
public PosPrinter getPrinter() //获取打印机名字
{
return m_Printer;
}
private int getChineseNum(string str) //获取中字数量
{
int len = 0;
char[] c = str.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
{
len += 1;
}
}
return len;
}
private String MakePrintString(int iLineChars, String name, String num, String price)
{
int iSpaces1 = 0;
int iSpaces2 = 0;
int coord1 = 27;
int ext = 4;
String tab1 = "";
String tab2 = "";
try
{
int lenCh = getChineseNum(name);
iSpaces1 = coord1 - (name.Length + lenCh);
for (int i = 0; i < iSpaces1; i++)
{
tab1 += " ";
}
iSpaces2 = iLineChars - (coord1 + num.Length + price.Length + ext);
for (int j = 0; j < iSpaces2; j++)
{
tab2 += " ";
}
}
catch (Exception)
{
}
int len1 = tab1.Length;
int len2 = tab2.Length;
return name + tab1 + num + tab2 + price;
}
public void print(int printNum) //打印内容
{
// bool status = false;
int mPrintNum = 1;
mPrintNum = printNum;
//Initialization
string iHeader = @"***功夫团膳***";
string iOrderid = " CC1001 ";
//iOrderid = "TS02000201506060020";
string iConsumer = "顾客:" + " 郭灿 ";
string iAddress = "地址:" + " 三林镇如日商务园 ";
string iTelephone = "电话:" + " 13312345678 ";
string iRemark = "备注:" + " 无 ";
string iSalesSlip = "发票: " + " 无 ";
string iSubmitTime = "订单提交时间:" + " 2017-07-11 12:01:02";
string iDeliveredTime = "预计送达时间:" + " 2017-07-11 13:00:00 ";
string iDivisionLine = "************************************************";
/**************************************************/
string iItemTitle = @"名称 数量 原价(元) ";
/****************************************************/
string iItemDisTitle = @"优惠名称 数量 优惠(元)";
/*****************************************************/
string iFreight = "外 送 费:" + " 3元 ";
string iTotalAmount = "订单合计:" + " 29元 ";
string iDiscountAmount = "优惠金额:" + " 2元 ";
string iPayAmount = "应付金额:" + " 27元 ";
string payTypeStr = " 微信支付 ";
string iPayType = "支付方式:" + payTypeStr;
string iCash = "应收现金:" + " 0元 ";
/*************** 订单编码 ************/
string foot1 = "感谢惠顾功夫团膳";
string foot2 = "功夫团膳餐厅" + "XX0001";
string foot3 = "查询电话:" + "15112345678";
int[] RecLineChars = new int[MAX_LINE_WIDTHS] { 0, 0 };
//When outputting to a printer,a mouse cursor becomes like a hourglass.
//Cursor.Current = Cursors.WaitCursor;
//for (int i = 0; i < mPrintNum; i++)
//{
// //if (m_Printer.CapRecPresent)
// //{
// // if (async == true)
// // {
// // m_Printer.AsyncMode = true;
// // }
// try
// {
// //m_Printer.TransactionPrint(PrinterStation.Receipt
// // , PrinterTransactionControl.Transaction);
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|cA" + "\u001b|bC" + "\u001b|2C"
+ iHeader + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|200uF");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|cA" + "\u001b|bC"
+ iOrderid + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iConsumer + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iAddress + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iTelephone + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iRemark + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iSalesSlip + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|80uF");
m_Printer.PrintNormal(PrinterStation.Receipt, iSubmitTime + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iDeliveredTime + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|300uF");
m_Printer.PrintNormal(PrinterStation.Receipt, iDivisionLine + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|100uF");
m_Printer.PrintNormal(PrinterStation.Receipt, iItemTitle + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iDivisionLine + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|100uF");
m_Printer.PrintNormal(PrinterStation.Receipt, iItemDisTitle + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iDivisionLine + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|100uF");
m_Printer.PrintNormal(PrinterStation.Receipt, iFreight + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iTotalAmount + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iDiscountAmount + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iPayAmount + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, iPayType + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|200uF");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|4C" + "\u001b|bC"
+ iCash + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|200uF");
// if (m_Printer.CapRecBarCode == true)
// {
// m_Printer.PrintBarCode(PrinterStation.Receipt,
// iOrderid,
// BarCodeSymbology.Code128, 1000,
// m_Printer.RecLineWidth, PosPrinter.PrinterBarCodeCenter,
// BarCodeTextPosition.None);
// }
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|200uF");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|cA" + "\u001b|4C"
+ foot1 + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|100uF");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|cA"
+ foot2 + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|300uF");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|cA"
+ foot3 + "\n");
m_Printer.PrintNormal(PrinterStation.Receipt, "\u001b|fP");
// m_Printer.TransactionPrint(PrinterStation.Receipt
// , PrinterTransactionControl.Normal);
// }
// catch (PosControlException e)
// {
// Console.WriteLine("**********************************");
// Console.WriteLine(e.ErrorCode);
// Console.WriteLine("+++++++++++++++++++++++++++++++++++");
// System.IO.File.AppendAllText(@"C:\hot.txt", DateTime.Now.ToString() + ": " + e.ToString() + "\r\n");
// }
// }
// m_Printer.AsyncMode = false;
}
}
}
//}