Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add communication error in status function. #5

Merged
merged 7 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ def test_signature_sleep(methods):

def test_signature_status(methods):
methods.remove(extract_stack(None, 2)[1][2].replace('test_signature_', ''))
sig = ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None)
sig = ArgSpec(args=['self', 'raise_on_error'], varargs=None,
keywords=None, defaults=(True,))
assert getargspec(ThermalPrinter.status) == sig


Expand Down
4 changes: 4 additions & 0 deletions thermalprinter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ class ThermalPrinterConstantError(ThermalPrinterError):

class ThermalPrinterValueError(ThermalPrinterError):
""" Value error handling class. """


class ThermalPrinterCommunicationError(ThermalPrinterError):
""" Communication error handling class """
12 changes: 10 additions & 2 deletions thermalprinter/thermalprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from .constants import (BarCodePosition, CharSet, Chinese, CodePage,
CodePageConverted, Command)
from .exceptions import ThermalPrinterAttributeError, ThermalPrinterValueError
from .exceptions import (ThermalPrinterAttributeError,
ThermalPrinterValueError,
ThermalPrinterCommunicationError)
from .validate import (validate_barcode, validate_barcode_position,
validate_charset, validate_chinese_format,
validate_codepage)
Expand Down Expand Up @@ -586,9 +588,12 @@ def sleep(self, seconds=1):
self.__is_sleeping = True
self.send_command(Command.ESC, 56, seconds, seconds >> 8)

def status(self):
def status(self, raise_on_error=True):
""" Check the printer status. If RX pin is not connected, all values
will be set to True.
If raise_on_error is set to true (default) will cause rising
ThermalPrinterCommunicationError in case lack of response
from the printer.

Return a dict:
movement: False if the movement is not connected
Expand All @@ -607,6 +612,9 @@ def status(self):
ret['paper'] = stat & 0b00000100 == 0
ret['voltage'] = stat & 0b00001000 == 0
ret['temp'] = stat & 0b01000000 == 0
elif raise_on_error:
raise ThermalPrinterCommunicationError()

return ret

def strike(self, state=False):
Expand Down