From 1f57b04974fa016eb91148e09142c25acac8680a Mon Sep 17 00:00:00 2001 From: csoft2k Date: Thu, 27 Jul 2017 23:05:50 +0200 Subject: [PATCH] Paper sensor querying command (#242) The DLE EOT command allows querying the status of several features of the printer. Added to the online/offline status developed in #237, this commit adds a paper sensor querying. Tested with an Epson TM-T20II, which only has an end-paper sensor. The near-end paper sensor should be tested with a compatible printer. However, the implementation is quite straight-forward. --- src/escpos/constants.py | 9 +++++++-- src/escpos/escpos.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/escpos/constants.py b/src/escpos/constants.py index a764290..75bdc1b 100644 --- a/src/escpos/constants.py +++ b/src/escpos/constants.py @@ -251,5 +251,10 @@ S_RASTER_2H = _PRINT_RASTER_IMG(b'\x02') # Set raster image double height S_RASTER_Q = _PRINT_RASTER_IMG(b'\x03') # Set raster image quadruple # Status Command -RT_STATUS_ONLINE = DLE + EOT + b'\x01'; -RT_MASK_ONLINE = 8; +RT_STATUS = DLE + EOT +RT_STATUS_ONLINE = RT_STATUS + b'\x01' +RT_STATUS_PAPER = RT_STATUS + b'\x04' +RT_MASK_ONLINE = 8 +RT_MASK_PAPER = 18 +RT_MASK_LOWPAPER = 30 +RT_MASK_NOPAPER = 114 \ No newline at end of file diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index cb72204..f13948b 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -36,6 +36,7 @@ from .constants import HW_RESET, HW_SELECT, HW_INIT from .constants import CTL_VT, CTL_CR, CTL_FF, CTL_LF, CTL_SET_HT, PANEL_BUTTON_OFF, PANEL_BUTTON_ON from .constants import TXT_STYLE from .constants import RT_STATUS_ONLINE, RT_MASK_ONLINE +from .constants import RT_STATUS_PAPER, RT_MASK_PAPER, RT_MASK_LOWPAPER, RT_MASK_NOPAPER from .exceptions import BarcodeTypeError, BarcodeSizeError, TabPosError from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError @@ -754,19 +755,40 @@ class Escpos(object): else: self._raw(PANEL_BUTTON_OFF) - def query_status(self): + def query_status(self, mode): """ Queries the printer for its status, and returns an array of integers containing it. + :param mode: Integer that sets the status mode queried to the printer. + RT_STATUS_ONLINE: Printer status. + RT_STATUS_PAPER: Paper sensor. :rtype: array(integer)""" - self._raw(RT_STATUS_ONLINE) + self._raw(mode) time.sleep(1) status = self._read() - return status or [RT_MASK_ONLINE] + return status def is_online(self): """ Queries the printer its online status. When online, returns True; False otherwise. :rtype: bool: True if online, False if offline.""" - return not (self.query_status()[0] & RT_MASK_ONLINE) + status = self.query_status(RT_STATUS_ONLINE) + if len(status) == 0: + return False + return not (status & RT_MASK_ONLINE) + + def paper_status(self): + """ Queries the printer its paper status. + Returns 2 if there is plenty of paper, 1 if the paper has arrived to + the near-end sensor and 0 if there is no paper. + :rtype: int: 2: Paper is adequate. 1: Paper ending. 0: No paper.""" + status = self.query_status(RT_STATUS_PAPER) + if len(status) == 0: + return 2 + if (status[0] & RT_MASK_NOPAPER == RT_MASK_NOPAPER): + return 0 + if (status[0] & RT_MASK_LOWPAPER == RT_MASK_LOWPAPER): + return 1 + if (status[0] & RT_MASK_PAPER == RT_MASK_PAPER): + return 2 class EscposIO(object):