A write to the printer's socket returns once the operating system holds the bytes, not once the printer has them. On the TM-T88IV with its cover open, Windows took all of a 128 KB receipt into its send buffer at once, so the job read done; once the cover closed, only its first third printed, without its cut, and the printer was left inside the image command, waiting for the rest. With the new option printer.Confirm(true), Send asks for status with GS r 1, which the printer answers only once it has run every byte before it, and, on the TM-T88IV, only while it can print: - Before beforeWrite, it sends DLE EOT 1, GS a 0 and GS r 1 in one write. The printer answers DLE EOT on receipt, ahead of anything it receives after it, so the answer is a fence: every byte before it left the printer before the request reached it, and Send drops it. That takes care of answers owed to requests the printer ran on an earlier connection or for an earlier run of the daemon, of the last bytes of a status block whose header went to an earlier connection, and of late DLE EOT answers, however long a lost segment holds the request back. Send waits for the fence until the write timeout or the context's deadline, then for an answer to GS r 1 after it. Without them, as with the cover open, no paper, or a printer stuck inside an earlier job, Send returns a plain error with nothing of the job written, as after a failed dial. Requests still queued in the printer ahead of this one are answered after the fence too, and answers carry nothing that tells them apart, so the wait reads every answer until 100 ms have passed since the last and drops them all, and none can be taken for a later job's confirmation. Each job waits those 100 ms. Only answers keep the wait open, so status blocks cannot, and a wait that the deadline or the connection's end cuts short counts as not ready, as one without an answer does. A printer may run GS r 1 while offline, so Send then asks DLE EOT 1 again, for up to 2 s, and writes the job only if the printer answers that it is online. Last, it reads on for 100 ms. Once the answers have settled none is owed, so an answer to GS r 1 during the online check or after it means one taken for the ready answer was not, as when a late DLE EOT answer from an earlier connection stood in for the fence and an owed answer followed it; Send then returns a plain error with nothing written, and the job is tried again. A cancellation stops the wait at once: it sets a flag that every reset of the read deadline checks, and the wait returns only once a cancellation that raced its end has finished with the connection. - After the job, it sends GS a 0 and GS r 1 again and returns once the answer arrives. If it does not arrive before the write deadline, or the connection fails or closes after the job was written in full, Send returns an *UnconfirmedError: the job may have printed in full, in part or not at all. The error has an Unconfirmed method, so the queue can tell it apart without importing the package. Automatic status blocks are what GS a 0 is for. The UB-E02 and UB-E03 boards turn them on at power-on, and a job's own ESC @ does not turn them off on a TM-T88VII with DIP switch 2-1 on. A block's last three bytes have an answer's fixed bits (bits 4 and 7 clear), and the board can send them on the next connection, where they would stand for the ready answer. The printer runs GS a 0 in order, so no block follows the answer to a GS r 1 sent after it. Blocks that come before it are passed over by their header's fixed bits: each connection keeps its place in a block across reads, and once Send has the confirmation it reads on for up to 100 ms for the rest of a block it ended inside. A block cut short ends with its connection, and last bytes that still begin a connection, before the first job after a power-on has turned blocks off, come before the fence's answer and are dropped. Status sends no GS a 0, which the printer does not run while it is offline. It passes over blocks, answers to DLE EOT n that a job's own bytes hold, and a late answer to GS r 1, and before it closes it reads what has come, to the end of any block, so that the next connection gets none of it. It returns ErrStatusUnsupported at once on a byte that fits no answer, and within 100 ms when a byte like an answer to GS r is not followed by an answer to DLE EOT, rather than after its 2 s. The printers plan and the job-store plan say so. The TM-T88IV answered GS r 1 with 0x00 within 4 ms when asked on its own. Nothing turns the option on yet. Tests pin the bytes Send sends with the option on, refuse a printer that answers DLE EOT 1 as offline, put an owed answer, a block's last bytes or both before the fence and delay the ready answer behind it, as after a lost segment or while the printer feeds and cuts at cover close, answer the ready request after an answer owed to an earlier run, and after one owed since a job's own GS r 1, split a status block across reads and across a confirmation and the 50 ms after it, and put owed answers around a block; in each, Send waits for the job's own answer. Others cut a block short and find the next jobs and Status unharmed, send a cover-open block in place of the ready answer, start a connection with the last bytes of a block whose header came after a confirmation, before the confirmation's deadline, with the confirmation at the write deadline, before the ready deadline or after Status's last answer, and find nothing written to a printer with its cover open. Others cut the ready wait short by its deadline and find nothing written, send status blocks every 50 ms and find the job sent and confirmed, and answer DLE EOT with bytes that are not DLE EOT answers and find Status unsupported within 100 ms. Others flood the online check with bytes like answers and Status with status blocks, and find both bounded, and turn the option off and find the job's bytes sent alone. Others put a late DLE EOT answer and an owed answer ahead of a held-back request, or an answer during the online check, and find nothing written; cancel during a steady stream and a flood of answers and find the wait stops at once; and play two cases the client leaves open, a long pause between queued answers and a fence answer between a block's last bytes, logging how they end. Part of stage 1d of docs/plans/job-store.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
150 lines
4.5 KiB
Go
150 lines
4.5 KiB
Go
package escpos
|
|
|
|
import "fmt"
|
|
|
|
// StatusKind selects which real-time status byte DLE EOT requests.
|
|
type StatusKind byte
|
|
|
|
// StatusKind values for DLE EOT n.
|
|
const (
|
|
StatusPrinter StatusKind = 1
|
|
StatusOffline StatusKind = 2
|
|
StatusError StatusKind = 3
|
|
StatusPaper StatusKind = 4
|
|
)
|
|
|
|
// StatusKinds lists every status kind in the order a full query sends them.
|
|
func StatusKinds() []StatusKind {
|
|
return []StatusKind{StatusPrinter, StatusOffline, StatusError, StatusPaper}
|
|
}
|
|
|
|
// Status is the printer state decoded from DLE EOT responses.
|
|
type Status struct {
|
|
Online bool
|
|
|
|
// CoverOpen and PaperEndStop come from DLE EOT 2, which reports why the
|
|
// printer is offline. They only mean something while Online is false;
|
|
// see Normalized.
|
|
CoverOpen bool
|
|
PaperEndStop bool
|
|
|
|
// PaperNearEnd and PaperOut come from the roll paper sensors (DLE EOT 4).
|
|
// The TM-T88VII does not support the near-end bits, so PaperNearEnd is
|
|
// always false on that model.
|
|
PaperNearEnd bool
|
|
PaperOut bool
|
|
|
|
CutterError bool
|
|
Unrecoverable bool
|
|
// AutoRecoverable reports an error the printer clears by itself, such as
|
|
// a print head that is too hot.
|
|
AutoRecoverable bool
|
|
}
|
|
|
|
// Ready reports whether the printer can print right now.
|
|
func (s Status) Ready() bool {
|
|
return s.Online && !s.CoverOpen && !s.PaperEndStop && !s.PaperOut &&
|
|
!s.CutterError && !s.Unrecoverable && !s.AutoRecoverable
|
|
}
|
|
|
|
// Normalized returns s with the offline causes cleared when the printer
|
|
// reports itself online. Epson printers always go offline while the cover is
|
|
// open or printing has stopped at the paper end, so an online printer cannot
|
|
// have either cause; a TM-T88IV has been seen to report a stale cover-open
|
|
// bit while busy printing and cutting a long receipt.
|
|
func (s Status) Normalized() Status {
|
|
if s.Online {
|
|
s.CoverOpen = false
|
|
s.PaperEndStop = false
|
|
}
|
|
return s
|
|
}
|
|
|
|
// StatusRequest returns the real-time DLE EOT n command for kind. Real-time
|
|
// commands are executed immediately, even while the printer is busy.
|
|
func StatusRequest(kind StatusKind) []byte {
|
|
return []byte{dle, eot, byte(kind)}
|
|
}
|
|
|
|
// statusFixedMask and statusFixedBits describe the bits every DLE EOT
|
|
// response has fixed: bits 0 and 7 are 0, bits 1 and 4 are 1.
|
|
const (
|
|
statusFixedMask byte = 0x93
|
|
statusFixedBits byte = 0x12
|
|
)
|
|
|
|
// Apply decodes resp, the printer's answer to StatusRequest(kind), into s.
|
|
func (s *Status) Apply(kind StatusKind, resp byte) error {
|
|
if resp&statusFixedMask != statusFixedBits {
|
|
return fmt.Errorf("%w: 0x%02X is not a DLE EOT %d response", ErrInvalidArgument, resp, kind)
|
|
}
|
|
bit := func(n uint) bool { return resp&(1<<n) != 0 }
|
|
switch kind {
|
|
case StatusPrinter:
|
|
s.Online = !bit(3)
|
|
case StatusOffline:
|
|
s.CoverOpen = bit(2)
|
|
s.PaperEndStop = bit(5)
|
|
case StatusError:
|
|
s.CutterError = bit(3)
|
|
s.Unrecoverable = bit(5)
|
|
s.AutoRecoverable = bit(6)
|
|
case StatusPaper:
|
|
s.PaperNearEnd = bit(2) && bit(3)
|
|
s.PaperOut = bit(5) && bit(6)
|
|
default:
|
|
return fmt.Errorf("%w: unknown status kind %d", ErrInvalidArgument, kind)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TransmitStatusRequest returns GS r 1, which asks for the paper sensor
|
|
// status. Unlike the real-time DLE EOT, the printer answers it only once it
|
|
// has processed every byte sent before it, so its answer confirms that a
|
|
// job reached the printer and ran.
|
|
func TransmitStatusRequest() []byte {
|
|
return []byte{gs, 'r', 1}
|
|
}
|
|
|
|
// AutomaticStatusOff returns GS a 0, which stops the printer sending
|
|
// automatic status blocks. Like GS r, it runs in order with the bytes
|
|
// before it, so no block follows an answer to a GS r sent after it.
|
|
func AutomaticStatusOff() []byte {
|
|
return []byte{gs, 'a', 0}
|
|
}
|
|
|
|
// Reply is what a byte the printer sends is, by its fixed bits.
|
|
type Reply int
|
|
|
|
// Kinds of Reply.
|
|
const (
|
|
// ReplyOther is a byte none of the others describe.
|
|
ReplyOther Reply = iota
|
|
|
|
// ReplyRealTime answers DLE EOT: bits 0 and 7 are 0, bits 1 and 4 are
|
|
// 1. Bytes of a job, such as an image's, can hold DLE EOT n, which the
|
|
// printer answers too.
|
|
ReplyRealTime
|
|
|
|
// ReplyAutomaticHeader starts a four-byte automatic status block: bits
|
|
// 0, 1 and 7 are 0 and bit 4 is 1. The three bytes after it have bits 4
|
|
// and 7 at 0, as a ReplyTransmit has.
|
|
ReplyAutomaticHeader
|
|
|
|
// ReplyTransmit answers GS r: bits 4 and 7 are 0.
|
|
ReplyTransmit
|
|
)
|
|
|
|
// ClassifyReply returns what b is, by its fixed bits.
|
|
func ClassifyReply(b byte) Reply {
|
|
switch {
|
|
case b&statusFixedMask == statusFixedBits:
|
|
return ReplyRealTime
|
|
case b&statusFixedMask == 0x10:
|
|
return ReplyAutomaticHeader
|
|
case b&0x90 == 0:
|
|
return ReplyTransmit
|
|
}
|
|
return ReplyOther
|
|
}
|