-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescpos.go
303 lines (256 loc) · 5.37 KB
/
escpos.go
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
// escpos project escpos.go
package escpos
import (
"errors"
"fmt"
"net"
"os"
"time"
//"github.com/alexbrainman/printer"
"github.com/tarm/serial"
)
type escpos struct {
name string
dst net.Conn
dst_serial *serial.Port
dst_usb *os.File
//dst_usb_win *printer.Printer
under int
bold int
bw int
type_printer int
}
const (
UPC_A = 65
UPC_E = 66
EAN13 = 67
EAN8 = 68
CODE39 = 69
I25 = 70
CODEBAR = 71
CODE93 = 72
CODE128 = 73
CODE11 = 74
MSI = 75
)
const (
ETH = 1
SER = 2
USB = 3
USB_W = 4
)
func NewPrinter() escpos {
return escpos{}
}
func (e escpos) SetSerialDst(port string, baudrate int, timeout int) error {
c := &serial.Config{Name: port, Baud: baudrate, ReadTimeout: time.Second * time.Duration(timeout)}
s, err := serial.OpenPort(c)
if err != nil {
return err
} else {
e.dst_serial = s
e.type_printer = SER
return nil
}
}
func (e escpos) SetTCPDst(ip, port string, timeout int) error {
wait := time.Duration(timeout) * time.Second
cn, err := net.DialTimeout("tcp", ip+":"+port, wait)
if err != nil {
return err
} else {
e.dst = cn
e.type_printer = ETH
return nil
}
}
func (e escpos) SetUSBDst(printer_path string) error {
f, err := os.OpenFile(printer_path, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
defer f.Close()
if err != nil {
return err
} else {
e.dst_usb = f
e.type_printer = USB
return nil
}
}
/*
func (e escpos) SetWINDst(printer_name, doc_name string) error {
p, err := printer.Open(printer_name)
if err != nil {
return err
}
defer p.Close()
err = p.StartDocument(doc_name, "RAW")
if err != nil {
return err
}
defer p.EndDocument()
err = p.StartPage()
if err != nil {
return err
} else {
e.dst_usb_win = p
e.type_printer = USB_W
return nil
}
}*/
/**/
func (e escpos) CloseDst() error {
switch e.type_printer {
case ETH:
return e.dst.Close()
case SER:
return e.dst_serial.Close()
case USB:
return nil
/*case USB_W:
//return e.dst_usb_win.EndPage()*/
default:
return e.dst.Close()
}
}
func (e escpos) Send(msg string) {
switch e.type_printer {
case ETH:
fmt.Fprintf(e.dst, msg)
case SER:
e.dst_serial.Write([]byte(msg))
case USB:
e.dst_usb.Write([]byte(msg))
case USB_W:
//fmt.Fprintf(e.dst_usb_win, msg)
default:
fmt.Fprintf(e.dst, msg)
}
}
func (e *escpos) toggleBW() {
if e.bw == 1 {
e.bw = 0
} else {
e.bw = 1
}
t := fmt.Sprintf("\x1DB%c", e.bw)
e.Send(t)
}
func (e escpos) init() {
e.Send("\x1B\x40")
}
func (e escpos) cut() {
e.Send("\x1DVA0")
}
func (e escpos) ff() {
e.Send("\n")
}
func (e escpos) ffn(n int) {
t := fmt.Sprintf("\x1Bd%c", n)
e.Send(t)
}
func (e escpos) codePage858() {
t := fmt.Sprintf("\x1BR%c", 2)
e.Send(t)
}
func (e escpos) fontA() {
e.Send("\x1BM0")
}
func (e escpos) fontB() {
e.Send("\x1BM1")
}
func (e escpos) fontC() {
e.Send("\x1B!1")
}
func (e escpos) doubleStrike() {
e.Send("\x1BG\x01")
}
func (e *escpos) toggleBold() {
if e.bold == 1 {
e.bold = 0
} else {
e.bold = 1
}
t := fmt.Sprintf("\x1BG%c", e.bold)
e.Send(t)
}
func (e escpos) underline() {
e.Send("\x1B-\x01")
e.under = 1
}
func (e *escpos) toggleUnderline() {
if e.under == 1 {
e.under = 0
} else {
e.under = 1
}
t := fmt.Sprintf("\x1B-%c", e.under)
e.Send(t)
}
func (e escpos) left() {
e.Send("\x1Ba\x00")
}
func (e escpos) centre() {
e.Send("\x1Ba\x01")
}
func (e escpos) right() {
e.Send("\x1Ba\x02")
}
func (e escpos) reallywide() {
e.Send("\x1D\x21\x70")
}
func (e escpos) normalwide() {
e.Send("\x1D\x21\x00")
}
func (e escpos) printBarCode(bcs int, hr int, msg string) error {
var err error
length := len(msg)
switch bcs {
case UPC_A, UPC_E:
if length < 11 || length > 12 {
err = errors.New("UPC : Message length must be between 11 and 12 characters")
}
case EAN13:
if length < 12 || length > 13 {
err = errors.New("EAN13: Message length must be between 12 and 13 characters")
}
case EAN8:
if length < 7 || length > 8 {
err = errors.New("EAN8: Message length must be between 7 and 8 characters")
}
case I25:
if length < 1 || (length%2 != 0) {
err = errors.New("I25: Message length must be greater than 1 and even")
}
default:
if length < 1 {
err = errors.New("Message Length must be greater than 1")
}
}
if err == nil {
t := ""
if hr > 0 {
t = fmt.Sprintf("\x1DH%c", hr)
}
t += fmt.Sprintf("\x1Dk%c%c%s", bcs, length, msg)
e.Send(t)
}
return err
}
func (e escpos) printQrCode(model int, size int, ec int, message string) {
//GS (k pL pH cn fn parameters
/*
GS (k 4 0 "1" 65 50 0
GS (k 3 0 "1" 67 5
GS (k 3 0 "1" 69 48
GS (k len(msg)+3 0 "1" 80 48 "msg"
GS (k 3 0 "1" 81 48
*/
//size = 1,2,3,4,5,10,16
//model = 1,2,3( aka micro)
//ec = L(1),M(2),Q(3),H(4)
t := fmt.Sprintf("\x1D\x28k%c%c%s%c%c%c", 4, 0, "1", 65, 48+model, 0)
t += fmt.Sprintf("\x1D\x28k%c%c%s%c%c", 3, 0, "1", 67, size)
t += fmt.Sprintf("\x1D\x28k%c%c%s%c%c", 3, 0, "1", 69, 48+ec)
t += fmt.Sprintf("\x1D\x28k%c%c%s%c%c%s", len(message)+3, 0, "1", 80, 48, message)
t += fmt.Sprintf("\x1D\x28k%c%c%s%c%c", 3, 0, "1", 81, 48)
e.Send(t)
}