如何在 python-escpos 中读取 ASB 状态

How to read ASB status in python-escpos

我想在 python-escpos 中读回 ASB 和其他状态结果。我认为 ._read() 方法会起作用,但我收到 "AttributeError: 'Serial' object has no attribute '_read'" 错误。我已经通过检查验证了 _read() 方法。

关于如何在 python-escpos 中读取状态的任何建议?

请尝试将 GS a 命令指定为 query_status() 方法中的参数并调用它。

GS a

[Name]
Enable/disable Automatic Status Back (ASB)
[Format]
ASCII   GS  a   n
Hex     1D  61  n
Decimal 29  97  n
[Range]
n = 0 – 255
[Default]
n: different depending on the printers

请尝试为 n 指定 0xFF。

query_status(mode)

Queries the printer for its status, and returns an array of integers containing it.

Parameters: mode – Integer that sets the status mode queried to the printer. - RT_STATUS_ONLINE: Printer status. - RT_STATUS_PAPER: Paper sensor. Return type: array(integer)

def query_status(self, mode):

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(mode)
    time.sleep(1)
    status = self._read()
    return status

def _raw(self, msg):

def _raw(self, msg):
    """ Print any command sent in raw format
    :param msg: arbitrary code to be printed
    :type msg: bytes
    """
    self.device.write(self.out_ep, msg, self.timeout)

def _read(self):

def _read(self):
    """ Reads a data buffer and returns it to the caller. """
    return self.device.read(self.in_ep, 16)

# Status Command

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