forked from gilbertfl/escpos-netprinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathescpos-netprinter.py
More file actions
1615 lines (1299 loc) · 81 KB
/
escpos-netprinter.py
File metadata and controls
1615 lines (1299 loc) · 81 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from flask import Flask, redirect, render_template, request, url_for
from os import getenv
from io import BufferedWriter
import csv
import subprocess
from subprocess import CompletedProcess
from pathlib import PurePath
from lxml import html, etree
from datetime import datetime
from zoneinfo import ZoneInfo
import threading
import socketserver
#Network ESC/pos printer server
class ESCPOSServer(socketserver.TCPServer):
def handle_timeout(self) -> None:
print ('Print service timeout!', flush=True)
return super().handle_timeout()
#Network ESC/pos printer request handling
class ESCPOSHandler(socketserver.StreamRequestHandler):
"""
Voir l'APG Epson section "Processing the Data Received from the Printer"
"""
timeout = 10 #On abandonne une réception après 10 secondes - un compromis pour assurer que tout passe sans se bourrer de connections zombies.
netprinter_debugmode = "true"
# Receive the print data and dump it in a file.
def handle(self):
print (f"Address connected: {self.client_address}", flush=True)
self.netprinter_debugmode = getenv('ESCPOS_DEBUG', "false")
bin_filename = PurePath('web', 'tmp', "reception.bin")
with open(bin_filename, "wb") as binfile:
#Read everything until we get EOF, and keep everything in a receive buffer
receive_buffer:bytes = b''
try:
# Implement the "Real-time command processing" block described in the Epson APG.
# How: Skim the received data byte by byte to respond to status checks as they come.
# We are making this as simple as possible so we do not slow down the print:
# 1) watch for ESC/POS commands that could lead to a status request
# 2) If this byte is none of those, send it forward without further processing
# 3) If this byte is a candidate command:
# a) Check a second byte for a status request
# b) if the second byte does not indicate a status request, send the two bytes forward without further processing
# c) if the second byte indicates a status request, reply appropriately then send all processed data bytes forward
while (indata_statuscheck := self.rfile.read(1)):
match indata_statuscheck:
case b'\x1D' : # GS
#This is potentially a status request.
indata_statuscheck = indata_statuscheck + self.rfile.read(1) #Get the second command byte
match indata_statuscheck:
case b'\x1D\x72':
#Respond to GS r status requests
gs_r_data:bytes = self.respond_gs_r()
indata_statuscheck = indata_statuscheck + gs_r_data
if self.netprinter_debugmode == True:
print(f"GS r received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x49':
# Respond to GS I printer ID request
gs_i_data:bytes = self.respond_gs_i()
indata_statuscheck = indata_statuscheck + gs_i_data
if self.netprinter_debugmode == True:
print(f"GS i received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x67':
# Respond to GS g maintenance counter requests
gs_g2_data:bytes = self.respond_gs_g()
indata_statuscheck = indata_statuscheck + gs_g2_data
if self.netprinter_debugmode == True:
print(f"GS g received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x28':
# Respond to GS ( E and GS ( H requests
gs_parens_data:bytes = self.respond_gs_parens()
indata_statuscheck = indata_statuscheck + gs_parens_data
if self.netprinter_debugmode == True:
print(f"GS ( received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x6A':
# Respond to GS j request ASB for ink
gs_j_data:bytes = self.respond_gs_j()
indata_statuscheck = indata_statuscheck + gs_j_data
if self.netprinter_debugmode == True:
print(f"GS j received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x3A' | b'\x1D\x63':
#Requests with zero argument bytes
pass
case b'\x1D\x21' | b'\x1D\x42' | b'\x1D\x62'| b'\x1D\x2F' | b'\x1D\x48' | b'\x1D\x54' | b'\x1D\x56' | b'\x1D\x62' | b'\x1D\x66' | b'\x1D\x68' | b'\x1D\x6A' | b'\x1D\x77':
#Requests with one argument byte
# NOTE: the GS V command has 1 or 2 arguments, but the second cannot be mistaken for a command so we wait next loop to read it in.
#Munch on it and pass it on
indata_statuscheck = indata_statuscheck + self.rfile.read(1)
case b'\x1D\x4C' | b'\x1D\x50' | b'\x1D\x57' | b'\x1D\x5C' :
#Requests with two argument bytes
#Munch on em and pass it on
indata_statuscheck = indata_statuscheck + self.rfile.read(2)
case b'\x1D\x7A' | b'\x1D\x5E' :
#Requests with three argument bytes
#Munch on em and pass it on
indata_statuscheck = indata_statuscheck + self.rfile.read(3)
case b'\x1D\x43' :
# GS C: obsolete commands
#Munch on it and pass it on
next_byte:bytes = self.rfile.read(1)
match next_byte:
case b'\x30':
# GS C 0 - counter print mode
next_byte = next_byte + self.rfile.read(2)
case b'\x31':
# GS C 1 Select count mode
next_byte = next_byte + self.rfile.read(6)
case b'\x32':
# GS C 2
next_byte = next_byte + self.rfile.read(2)
case b'\x3B':
# GS C ; - 5 bytes with separators
next_byte = next_byte + self.rfile.read(10)
indata_statuscheck = indata_statuscheck + next_byte
case b'\x1D\x61':
# GS a - request enable automatic status back
n:bytes = self.rfile.read(1)
indata_statuscheck = indata_statuscheck + n
# We send the ASB once, in case the client checks for it.
if n==b'\x00':
pass #The request is disable ASB -> we send nothing back.
else:
self.send_basic_ASB_OK()
case b'\x1D\x44':
# GS D has 2 functions.
m:bytes = self.rfile.read(1)
fn:bytes = self.rfile.read(1)
indata_statuscheck = indata_statuscheck + m + fn
match fn:
case b'\x43':
# <fn=63> Define Windows BMP NV graphics data
# read the bytes before the BMP
indata_statuscheck = indata_statuscheck + self.rfile.read(5)
#We are at the start of the BMP here.
indata_statuscheck = indata_statuscheck + self.consume_bmp_file()
if self.netprinter_debugmode == True:
print(f"GS D <fn=63> BMP NV graphics data received: {indata_statuscheck}", flush=True)
case b'\x53':
# <fn=83> Define Windows BMP download graphics data
# read the bytes before the BMP
indata_statuscheck = indata_statuscheck + self.rfile.read(5)
#We are at the start of the BMP here.
indata_statuscheck = indata_statuscheck + self.consume_bmp_file()
if self.netprinter_debugmode == True:
print(f"GS D <fn=83> BMP download data received: {indata_statuscheck}", flush=True)
case _:
if self.netprinter_debugmode == True:
print(f"Unknown GS D command received : {indata_statuscheck}", flush=True)
if self.netprinter_debugmode == True:
print(f"GS D command received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x6B':
# GS k - print barcode request
# Find out the function
m:bytes = self.rfile.read(1)
# Read the barcode data. There are 2 functions with different formats, depending on m
barcode_data:bytes = b''
match m:
case b'\x00':
# Function A - the data is null-terminated.
# Read one byte at a time until \x00 comes
while True:
chunk:bytes = self.rfile.read(1)
if not chunk:
break
barcode_data = barcode_data + chunk
if b'\x00' in chunk:
break
case b'\x65':
# Function B - the data length is specified
n:bytes = self.rfile.read(1)
barcode_data = n + self.rfile.read(int.from_bytes(n))
# Now send all that data forward
indata_statuscheck = indata_statuscheck + m + barcode_data
if self.netprinter_debugmode == True:
print(f"GS k received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x51':
# GS Q 0 - print bit image
m:bytes = self.rfile.read(2) # the 0 plus the m
xL:bytes = self.rfile.read(1)
xH:bytes = self.rfile.read(1)
yL:bytes = self.rfile.read(1)
yH:bytes = self.rfile.read(1)
# Read the image then send it forward
indata_statuscheck = indata_statuscheck + m + xL + xH + yL + yH + self.consume_byte_array(xL, xH, yL, yH)
if self.netprinter_debugmode == True:
print(f"GS Q 0 received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1D\x2A':
# GS * define downloaded image
x:bytes = self.rfile.read(1)
y:bytes = self.rfile.read(1)
indata_statuscheck = indata_statuscheck + self.rfile.read(int.from_bytes(x) * int.from_bytes(y) * 8)
if self.netprinter_debugmode == True:
print(f"GS * received containing {len(indata_statuscheck)} bytes", flush=True)
case _:
#This is not a status request
if self.netprinter_debugmode == True:
print(f"Almost-status bytes: {indata_statuscheck}", flush=True)
case b'\x10' : # DLE
#This is potentially a status request.
indata_statuscheck = indata_statuscheck + self.rfile.read(1) #Get the second command byte
match indata_statuscheck:
case b'\x10\x04':
# Respond to DLE EOT status requests
dle_eot_data:bytes = self.respond_dle_eot()
indata_statuscheck = indata_statuscheck + dle_eot_data #append the DLE EOT bytes to the processed bytes
if self.netprinter_debugmode == True:
print(f"DLE EOT received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x10\x14':
# Respond to DLE DC4
dle_dc4_data: bytes = self.respond_dle_dc4()
indata_statuscheck = indata_statuscheck + dle_dc4_data
if self.netprinter_debugmode == True:
print(f"DLE EOT received containing {len(indata_statuscheck)} bytes", flush=True)
case _:
#This is not a status request
if self.netprinter_debugmode == True:
print(f"Almost-status bytes: {indata_statuscheck}", flush=True)
case b'\x1B' : # ESC
#This is potentially a status request.
indata_statuscheck = indata_statuscheck + self.rfile.read(1) #Get the second command byte
match indata_statuscheck:
case b'\x1B\x76':
# Respond to ESC v request
self.wfile.write(b'\x00') #Respond roll paper present and adequate
self.wfile.flush()
if self.netprinter_debugmode == True:
print(f"ESC v received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1B\x75':
#Respond to ESC u request
#Read the n byte
n:bytes = self.rfile.read(1)
self.wfile.write(b'\x00') #Respond drawer kick-out LOW
self.wfile.flush()
indata_statuscheck = indata_statuscheck + n
if self.netprinter_debugmode == True:
print(f"ESC u received containing {len(indata_statuscheck)} bytes", flush=True)
case _:
# All other ESC commands have one n byte
indata_statuscheck = indata_statuscheck + self.rfile.read(1)
if self.netprinter_debugmode == True:
print(f"Non-blocking ESC command received: {indata_statuscheck}", flush=True)
case b'\x1C': #FS
indata_statuscheck = indata_statuscheck + self.rfile.read(1) #Get the second command byte
match indata_statuscheck:
case b'\x1C\x28':
#This an FS ( request
fs_parens_data:bytes = self.respond_fs_parens()
indata_statuscheck = indata_statuscheck + fs_parens_data
if self.netprinter_debugmode == True:
print(f"FS ( received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1C\x26' | b'\x1C\x2E':
#Subrequests with zero argument bytes
pass
case b'\x1C\x21' | b'\x1C\x2D' | b'\x1C\x43'| b'\x1C\x57':
#Subrequests with one argument byte
#Munch on it and pass it on
indata_statuscheck = indata_statuscheck + self.rfile.read(1)
case b'\x1C\x3F' | b'\x1C\x53' | b'\x1C\x70':
#Subrequests with 2 argument bytes
#Munch on em and pass it on
indata_statuscheck = indata_statuscheck + self.rfile.read(2)
case b'\x1C\x32':
#FS 2 command has c1 c2 then k bits (arbitrarily decided by the printer. We'll munch 32 as in the APG)
indata_statuscheck = indata_statuscheck + self.rfile.read(4) # Munch 4 bytes (32 bits)
case b'\x1C\x67':
# FS g
next_byte:bytes = self.rfile.read(1)
match next_byte:
case b'\x31':
# FS g 1 - write to NV memory
# FS g 1 has m then 4 a bytes then nl, ng then (nL + nH × 256) data bytes
next_byte = next_byte + self.rfile.read(5) #munch on unused bytes: m a1 a2 a3 a4
# then read pL and pH
nL:bytes = self.rfile.read(1)
nH:bytes = self.rfile.read(1)
#send all that data forward
next_byte = next_byte + nL + nH + self.consume_parameter_data(nL, nH)
case b'\x32':
# FS g 2 - read from NV user memory
# FS g 2 has m then 4 a bytes then nl, ng. Must send back "header to NUL"
next_byte = next_byte + self.rfile.read(5) #munch on unused bytes: m a1 a2 a3 a4
# Get the expected number of bytes to send
nL:bytes = self.rfile.read(1)
nH:bytes = self.rfile.read(1)
nb_bytes = int.from_bytes(nL) + (int.from_bytes(nH) * 256)
self.wfile.write(b'\x5f') #Send the header
self.wfile.write(b'\x0F' * nb_bytes) #Send the expected amount of "data"
self.wfile.write(b'\x00') #Send NULL
self.wfile.flush()
#send all the received data forward
next_byte = next_byte + nL + nH
indata_statuscheck = indata_statuscheck + next_byte
if self.netprinter_debugmode == True:
print(f"FS g received containing {len(indata_statuscheck)} bytes", flush=True)
case b'\x1C\x71':
# FS q - store non-volatile raster graphics.
# get n - the number if images to munch on
n:bytes = self.rfile.read(1)
indata_statuscheck = indata_statuscheck + n
for i in range(int.from_bytes(n)):
# munch on one image and pass it on
xL = self.rfile.read(1)
xH = self.rfile.read(1)
yL = self.rfile.read(1)
yH = self.rfile.read(1)
indata_statuscheck = indata_statuscheck + self.consume_byte_array(xL, xH, yL, yH)
if self.netprinter_debugmode == True:
print(f"FS q received containing {len(indata_statuscheck)} bytes", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown FS request received: {indata_statuscheck}", flush=True)
case _:
#This byte is uninteresting data for this block's purposes, no processing necessary.
pass
#Append the processed byte(s) to the receive buffer
receive_buffer = receive_buffer + indata_statuscheck
except TimeoutError:
print("Timeout while reading")
self.connection.close()
if len(receive_buffer) > 0:
print(f"{len(receive_buffer)} bytes received.")
if self.netprinter_debugmode == 'True':
print("-----start of data-----\n", flush=True)
print(receive_buffer, flush=True)
print("\n-----end of data-----", flush=True)
else:
print("No data received!", flush=True)
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
else:
#Quand on a reçu le signal de fin de transmission
print(f"{len(receive_buffer)} bytes received.", flush=True)
if self.netprinter_debugmode == 'True':
print("-----start of data-----\n", flush=True)
print(receive_buffer, flush=True)
print("\n-----end of data-----", flush=True)
#Écrire les données reçues dans le fichier.
if len(receive_buffer) > 0:
binfile.write(receive_buffer)
binfile.close() #Écrire le fichier et le fermer
#traiter le fichier reception.bin pour en faire un HTML
self.print_toHTML(binfile, bin_filename)
elif self.netprinter_debugmode == 'True':
print("No data received: nothing will be printed.", flush=True)
#The binfile should auto-close here.
self.wfile.write(b"ESCPOS-netprinter: All done!") #A enlever plus tard? On dit au client qu'on a fini.
self.wfile.flush()
self.connection.close()
print ("Data reception finished, signature sent.", flush=True)
def consume_bmp_file(self) -> bytes:
""" Consume a BMP file for the GS D command
Returns:
bytes: The complete BMP file
"""
bmp_file:bytes = b''
# BMP header has the size in byte
# The first 2 bytes are supposed to be "BM" (\x42\x4D)
header_field:bytes = self.rfile.read(2)
bmp_file = bmp_file + header_field
if header_field == b'\x42\x4D':
#Confirmed BMP. Get the size (4 bytes)
bmp_size:bytes = self.rfile.read(4)
bmp_data:bytes = b''
if int.from_bytes(bmp_size) == 0:
print("Error: zero-byte-long argument specified", flush=True)
else:
bmp_data = self.rfile.read(int.from_bytes(bmp_size)) # Send these bytes forward in all cases
bmp_file = bmp_file + bmp_size + bmp_data
else:
if self.netprinter_debugmode == True:
print(f"GS D received non-BMP file - did not read the data", flush=True)
return bmp_file
def respond_gs_j(self) -> bytes:
#Consume a GS j request and respond to the client if necessary
gs_j_data:bytes = self.rfile.read(1)
match gs_j_data:
case b'\x00':
# request to stop ASB - we do nothing
pass
case _:
#Now we send the 4-byte ASB all-clear for ink
self.wfile.write(b'\x35\x40\x40\x00')
return gs_j_data
def respond_dle_dc4(self) -> bytes:
#Consume a DLE DC4 request and respond to functions 1, 2, 3, 7 and 8
if self.netprinter_debugmode == 'True':
print("DLE DC4 request", flush=True)
next_in:bytes = self.rfile.read(1) #Get the first byte
match next_in:
case b'\x07':
m:bytes = self.rfile.read(1)
match m:
case b'\x01':
#Transmit the 4 bytes of the all-clear ASB status like GS a
self.send_basic_ASB_OK()
case b'\x02':
#Transmit the 4 bytes of the all-clear extended ASB status like FS ( e
self.send_extended_ASB_OK()
case b'\x04':
#Transmit the offline response like GS ( H <f=49>
self.wfile.write(b'\x37\x23\x00') #Send the empty Offline response to the client
case b'\x05':
#Transmit battery status - printer dependent so we send anything
self.wfile.write(b'\x01')
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown DLE DC4 <fn=7> request received: {next_in}", flush=True)
next_in = next_in + m
case b'\x01':
#Generate pulse - no need to respond, so we munch on the 2 extra bytes
next_in = next_in + self.rfile.read(2)
case b'\x02':
#Power-off sequence
#Munch on the 2 extra fixed bytes
next_in = next_in + self.rfile.read(2)
#send the power-off notice
self.wfile.write(b'\x3B\x30\x00')
#Here a physical printer would stop everything, but we will go on.
case b'\x03':
#Sound buzzer - no need to respond, so we munch on the 4 extra bytes.
next_in = next_in + self.rfile.read(4)
case b'\x08':
#Clear buffer(s)
#Munch on the 7 extra fixed bytes
next_in = next_in + self.rfile.read(7)
#Send the Buffer Clear response
self.wfile.write(b'\x37\x25\x00')
case _:
#This request is about something else, nothing to do.
if self.netprinter_debugmode == 'True':
print(f"Unknown DLE DC4 request received: {next_in}", flush=True)
return next_in
def respond_fs_parens(self) -> bytes:
#Consume and process one FS ( request
if self.netprinter_debugmode == 'True':
print("FS ( request", flush=True)
next_in:bytes = self.rfile.read1(1) #Get the command's next byte
match next_in:
case b'\x65': # e
# FS ( e Enable/disable Automatic Status Back (ASB) for optional functions (extended status)
if self.netprinter_debugmode == 'True':
print("FS ( e request", flush=True)
# First, get the next bytes
pL:bytes = self.rfile.read(1) # Get pL byte
pH:bytes = self.rfile.read(1) # Get pH byte
m:bytes = self.rfile.read(1) # Get m byte
n:bytes = self.rfile.read(1) # Get n byte
next_in = next_in + pL + pH + m + n # Send these bytes forward in all cases
match n:
case b'\x00':
#Request to disable ASB: nothing to return
pass
case _:
#Enabling any status (specifying n != 0) starts extended ASB
self.send_extended_ASB_OK()
case b'\x45': # FS ( E receipt enhancement control
if self.netprinter_debugmode == 'True':
print(f"FS ( E request", flush=True)
# First, get the size and fn bytes
pL:bytes = self.rfile.read(1) # Get pL byte
pH:bytes = self.rfile.read(1) # Get pH byte
fn:bytes = self.rfile.read(1) # Get fn byte
match fn:
case b'\x3d': #fn 61 needs a response
m:bytes = self.rfile.read(1)
c:bytes = self.rfile.read(1)
next_in = next_in + pL + pH + fn + m + c
match c:
case b'\x30': #48
# Send back top logo info: m kc1 kc2 a n
self.wfile.write(b'\x02\x32\x32\x49\x04')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("FS ( E <fn=61> top logo codes sent", flush=True)
case b'\x31': #49
# Send back bottom logo info: m kc1 kc2 a
self.wfile.write(b'\x02\x32\x32\x49')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("FS ( E <fn=61> bottom logo codes sent", flush=True)
case b'\x32': #50
# Send back extended top/bottom logo info: m [a1 n1] [a2 n2] ... [ak nk]
# if nk = 48 the setting is disabled; let's do that.
# NOTE: the spec fixes m = 2. The specification does not specify how to tell the value of k, but there a 5 possible pairs. Let's do 2 pairs.
self.wfile.write(b'\x02'+ b'\x40\x30' + b'\x41\x30')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("FS ( E <fn=61> extended top/bottom logo codes sent", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown FS ( E <fn=61> request received: {next_in}", flush=True)
case _:
#In other cases, no response needed
next_in = next_in + pL + pH + fn + self.consume_parameter_data(pL, pH) # Send these bytes forward in all cases
if fn in [b'\x3c', b'\x3e', b'\x3f', b'\x40', b'\x41', b'\x43']:
if self.netprinter_debugmode == 'True':
print(f"No-response-needed FS ( E <fn={fn}> request", flush=True)
else:
if self.netprinter_debugmode == 'True':
print(f"Unknown FS ( E request received: {next_in}", flush=True)
case b'\x4C': # FS ( L Select label and black mark control function(s)
if self.netprinter_debugmode == 'True':
print(f"FS ( L request", flush=True)
# First, get the size and fn bytes
pL:bytes = self.rfile.read(1) # Get pL byte
pH:bytes = self.rfile.read(1) # Get pH byte
fn:bytes = self.rfile.read(1) # Get fn byte
match fn:
case b'\x22': #fn=34 needs a response
# FS ( L <fn=34> Paper layout information transmission
n:bytes = self.rfile.read(1)
next_in = next_in + n
le_n:bytes = str.encode(f'{int.from_bytes(n)}') #Convert n as text
match n:
case b'\x40':
# n=64 Paper layout setting value (in mm)
#Send the response Header to null with each value expressed as text
self.wfile.write(b'\x37\x4b')
self.wfile.write(le_n)
self.wfile.write(b'\x1f')
self.wfile.write(b'0\x1f') #sm=0 This is a Receipt (no black mark)
self.wfile.write(b'0\x1f') #sa=0 Does not specify the distance from the print reference to the next print reference
self.wfile.write(b'\x1f'*4) #sb, sc, sd, se are omitted, not pertinent for sm=0
self.wfile.write(b'800') #The receipt width is 80mm (the unit is 0.1mm)
self.wfile.write(b'\x00')
self.wfile.flush() #Send the response back.
if self.netprinter_debugmode == 'True':
print(f"FS ( L <fn=34> <n=64> Paper layout settings sent", flush=True)
case b'\x50':
#n=80 Paper layout effective value (in dots)
#Send the response Header to null with each value expressed as text
self.wfile.write(b'\x37\x4b')
self.wfile.write(le_n)
self.wfile.write(b'\x1f')
self.wfile.write(b'0\x1f') #sm=0 This is a Receipt (no black mark)
self.wfile.write(b'0\x1f') #sa=0 Does not specify the distance from the print reference to the next print reference
self.wfile.write(b'\x1f'*4) #sb, sc, sd, se are omitted, not pertinent for sm=0
self.wfile.write(b'512') #The receipt width is 80mm (the unit is dots, we choose 512 dots @ 180 DPI like the EPSON TM-88V)
self.wfile.write(b'\x00')
self.wfile.flush() #Send the response back.
if self.netprinter_debugmode == 'True':
print(f"FS ( L <fn=34> <n=80> Paper layout settings sent", flush=True)
case _:
#Unknown n, we send nothing back.
if self.netprinter_debugmode == 'True':
print(f"Unknown FS ( L <fn=34> <n={le_n}> Paper layout request received", flush=True)
case b'\x30': #fn=48 needs a response
# FS ( L <fn=48> Transmit the positioning information
# The paper layout is "No reference (do not use layout)"
m:bytes = self.rfile.read(1)
next_in = next_in + m
#Send the response Header to null
self.wfile.write(b'\x37\x38')
self.wfile.write(b'\x40') #Information a: Bits 0,1,2 are 0 and bit 6 is fixed at 1.
self.wfile.write(b'\x43') #Information b: Bits 0 and 1 are 1 and bit 6 is fixed at 1.
self.wfile.write(b'\x00')
self.wfile.flush() #Send the response back.
if self.netprinter_debugmode == 'True':
print(f"FS ( L <fn=48> Paper positioning info sent", flush=True)
case b'\x21' | b'\x41' | b'\x42' | b'\x43' | b'\x43': #fn=33, 65, 66, 67, 80
#No response needed
#WARNING: the documentation says that <function 80> is hex 43, but it should be 50. I'll follow the documentation.
next_in = next_in + self.consume_parameter_data(pL, pH) # Send these bytes forward
if self.netprinter_debugmode == 'True':
print(f"No-response-needed FS ( E <fn={fn}> request", flush=True)
case _:
next_in = next_in + self.consume_parameter_data(pL, pH) # Send these bytes forward
if self.netprinter_debugmode == 'True':
print(f"Unknown FS ( E request received: {next_in}", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown FS ( request received: {next_in}", flush=True)
return next_in
def send_basic_ASB_OK(self) -> None:
"""Transmit the 4 bytes of the all-clear ASB status
"""
self.wfile.write(b'\x00\x00\x00\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("4-byte ASB status sent", flush=True)
def send_extended_ASB_OK(self) -> None:
"""Return all-clear extended ASB status
"""
self.wfile.write(b'\x39\x00\x40\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("4-byte extended ASB status sent", flush=True)
def respond_gs_g(self) -> bytes:
""" Consume a GS G request and respond to <fn=2> if necessary
Returns:
bytes: the consumed request
"""
if self.netprinter_debugmode == 'True':
print("GS g request", flush=True)
next_in:bytes = self.rfile.read(2) #Get the 2 and m bytes
match next_in:
case b'\x32\x00':
#Respond to GS g 2 with a constant number
#TODO: someday implement counters in case some client checks their progress
next_in = next_in + self.rfile.read(2) #Get 2 more bytes (nL and nH)
self.wfile.write(b'\x5F\x01\x00') #Send one(1) for all counters
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("4-byte extended ASB status sent", flush=True)
case b'\x30\x00':
# Read in the 2 other bytes and send them on
next_in = next_in + self.rfile.read(2) #Get 2 more bytes (nL and nH)
case _:
if self.netprinter_debugmode == 'True':
print(f"Non-status GS g request received: {next_in}", flush=True)
return next_in
def respond_gs_i(self) -> bytes:
"""Consume and process one GS i request and respond to the client if necessary
Returns:
bytes: The consumed request
"""
#First, define inner helper functions
def send_gs_i_printer_info_A(contents:bytes) -> None:
#Helper to respond with Printer Info A
self.wfile.write(b'\x3D') #Header
self.wfile.write(contents[:80]) # Max 80 bytes here, so send only that slice
self.wfile.write(b'\x00') #NUL
self.wfile.flush()
def send_gs_i_printer_info_B(contents:bytes) -> None:
#Helper to respond with Printer Info B
self.wfile.write(b'\x5F') #Header
self.wfile.write(contents[:80]) # Max 80 bytes here, so send only that slice
self.wfile.write(b'\x00') #NUL
self.wfile.flush()
# Let's do this
if self.netprinter_debugmode == 'True':
print("GS i request", flush=True)
next_in:bytes = self.rfile.read1(1) #The n is at most 1 byte
match next_in:
case b'\x01' | b'\x31': #1 or 49
#Transmit some printer model ID byte
self.wfile.write(b'\x01') #TODO: choose a model ID
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Printer model ID byte sent", flush=True)
case b'\x02' | b'\x32': # 2 or 50
#Transmit Printer type ID
self.wfile.write(b'\x02') # No multi-byte chars, autocutter installed, no DM-D
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Printer type ID byte sent", flush=True)
case b'\x03' | b'\x33': # 3 or 51
#Transmit some version ID byte
self.wfile.write(b'\x23') #TODO: put the version ID somewhere central to facilitate releases
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Printer version ID byte sent", flush=True)
case b'\x21': # 33
#Transmit printer type information - supported functions
# We send a 3-byte all-clear response
first_byte = b'\x02' # No multi-byte chars, autocutter installed, no DM-D
second_byte = b'\x40' #Fixed
third_byte = b'\x40' #No peeler.
send_gs_i_printer_info_A(first_byte + second_byte + third_byte)
if self.netprinter_debugmode == 'True':
print("Printer supported functions sent", flush=True)
case b'\x41': # 65
#Transmit printer firmware version
send_gs_i_printer_info_B(b'release 2.3') #TODO: put the version number somewhere central to facilitate releases
if self.netprinter_debugmode == 'True':
print("Printer firmware version sent", flush=True)
case b'\x42' | b'\x43': # 66 or 67
#Transmit maker name or model name - could be different but not important.
send_gs_i_printer_info_B(b'ESCPOS-netprinter')
if self.netprinter_debugmode == 'True':
print("Printer maker or model name sent", flush=True)
case b'\x44': # 68
#Transmit printer serial number
send_gs_i_printer_info_B(b'netprinter_1') #TODO: create a serial number for each instance (??)
if self.netprinter_debugmode == 'True':
print("Printer serial sent", flush=True)
case b'\x45': # 69
#Transmit printer font of language
send_gs_i_printer_info_B(b'PC850 Multilingual')
if self.netprinter_debugmode == 'True':
print("Printer language sent", flush=True)
case b'\x23' | b'\x24' | b'\x60' | b'\x6E' :
#These are model-specific requests for Printer information A
send_gs_i_printer_info_A(b'\x01\x01\x01') # We can send anything, but the client will wait for Printer Information A before continuing.
if self.netprinter_debugmode == 'True':
print("Model-specific Printer Info A sent", flush=True)
case b'\x6F' :
#These are model-specific requests for Printer Information B
send_gs_i_printer_info_B(b'Netprinter')
if self.netprinter_debugmode == 'True':
print("Model-specific Printer Info B sent", flush=True)
case b'\x70':
#These are model-specific requests for Printer Information B
send_gs_i_printer_info_B(b'Netprinter')
if self.netprinter_debugmode == 'True':
print("Model-specific Printer Info B sent", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown GS i request received: {next_in}", flush=True)
return next_in
def respond_gs_r(self) -> bytes:
"""Consume one GS r request and respond to the client
Returns:
bytes: the consumed request
"""
if self.netprinter_debugmode == 'True':
print("GS r request", flush=True)
request:bytes = self.rfile.read1(1) #The n is at most 1 byte
match request:
case b'\x01': #n=1
#Send paper status adequate and present
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Paper status sent", flush=True)
case b'\x31': #n=49
#Send paper status adequate and present (alternate)
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Paper status sent", flush=True)
case b'\x02': #n=2
#Send drawer kick-out connector status
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Drawer kick-out status sent", flush=True)
case b'\x32': #n=50
#Send drawer kick-out connector status (alternate)
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Drawer kick-out status sent", flush=True)
case b'\x04': #n=4
#Send ink status adequate
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Ink status sent", flush=True)
case b'\x34': #n=52
#Send ink status adequate
self.wfile.write(b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Ink status sent", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown GS r request received: {request}", flush=True)
return request
def respond_gs_parens(self) -> bytes:
""" Consume and respond to one GS ( request
Returns:
bytes: the consumed request
"""
request:bytes = self.rfile.read(1) #Start by getting the next byte
match request:
case b'\x45': #E
#Set user setup command
request = request + self.process_gs_parens_E()
case b'\x48': #H
#Transmission + response or status (not for OPOS or Java POS, and very mysterious and printer-dependant)
request = request + self.process_gs_parens_H()
case _:
if self.netprinter_debugmode == 'True':
print(f"Non-status GS ( request received: {request}", flush=True)
return request
def process_gs_parens_E(self) -> bytes:
pL:bytes = self.rfile.read(1) # Get pL byte
pH:bytes = self.rfile.read(1) # Get pH byte
fn:bytes = self.rfile.read(1) # Get fn byte
request:bytes = pL + pH + fn # Send these bytes forward in all cases
match fn:
case b'\x01':
# Respond to user setting mode start request
request = request + self.rfile.read(2) #read d1 and d2
self.wfile.write(b'\x37\x20\x00') # Respond OK
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Mode change notice sent", flush=True)
case b'\x04':
# Respond with settings of the memory switches
a:bytes = self.rfile.read(1) #read a
request = request + a
match a:
case b'\x01':
# Respond with: Power-on notice disabled, Receive buffer large, busy when buffer full,
# receive error ignored, auto line-feed disabled, DM-D not connected,
# RS-232 pins 6 and 25 not used.
response:bytes = b'\x30\x30\x31\x31\x30\x30\x30\x30'
self.wfile.write(b'\x37\x21' + response + b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Msw1 switches sent", flush=True)
case b'\x02':
# Respond with: Autocutter enabled and chinese character code GB2312.
response:bytes = b'\x31\x31\x31\x30\x30\x30\x30\x30'
self.wfile.write(b'\x37\x21' + response + b'\x00')
self.wfile.flush()
if self.netprinter_debugmode == 'True':
print("Msw2 switches sent", flush=True)
case _:
if self.netprinter_debugmode == 'True':
print(f"Unknown switch set requested: {request}", flush=True)
case b'\x06': #6
# Transmit the customized setting values
a:bytes = self.rfile.read(1) #read a
request = request + a
response:bytes = b''
match int.from_bytes(a):
case 1:
#NV Memory capacity
response = b'128'
case 2:
#NV graphics capacity
response = b'256'
case 3:
#Paper width
response = b'80'
case 5:
#Print density
response = b'72'