Class: Smartware::Driver::Printer::EscPos

Inherits:
Object
  • Object
show all
Defined in:
lib/smartware/drivers/printer/esc_pos.rb

Constant Summary collapse

QUERY =
[
  0x10, 0x04, 20, # Transmit Full Status
].pack("C*")
MODEL_QUERY =
[
  0x1D, 0x49, 0x31,  # Printer model ID
].pack("C*")
VERSION_QUERY =
[
  0x1D, 0x49, 0x33,   # Printer ROM version
].pack("C*")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EscPos

Returns a new instance of EscPos.



22
23
24
25
26
27
28
29
30
31
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 22

def initialize(config)
  @sp = SerialPort.new config["port"], 115200, 8, 1, SerialPort::NONE
  @sp.read_timeout = 500

  @error = nil
  @status = :ready
  @model = ''
  @version = ''
  @buf = "".force_encoding("BINARY")
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



20
21
22
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 20

def error
  @error
end

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 20

def model
  @model
end

#statusObject (readonly)

Returns the value of attribute status.



20
21
22
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 20

def status
  @status
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 20

def version
  @version
end

Instance Method Details

#new_renderObject



104
105
106
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 104

def new_render
  Render.new
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 81

def print(data)
  begin
    @sp.write data

    start = Time.now

    loop do
      now = Time.now
      break if now - start > 30

      @sp.write QUERY
      magic, paper_status, user_status, recoverable, unrecoverable = read_response(6).unpack("nC*")

      raise "invalid magic: #{magic.to_s 16}" if magic != 0x100F

      # paper not in motion
      break if (user_status & 8) == 0
    end
  rescue => e
    Smartware::Logging.logger.warn "Printer communication error: #{e}"
  end
end

#queryObject



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
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 33

def query
  begin
    @sp.write QUERY
    magic, paper_status, user_status, recoverable, unrecoverable = read_response(6).unpack("nC*")

    raise "invalid magic: #{magic.to_s 16}" if magic != 0x100F

    if unrecoverable != 0
      @status = :error

      @error = Interface::Printer::HARDWARE_ERROR
    elsif (paper_status & 1) != 0
      @status = :error
      @error = Interface::Printer::OUT_OF_PAPER

    elsif (recoverable != 0) || ((user_status & 3) != 0)
      @status = :transient_error
      @error = Interface::Printer::HARDWARE_ERROR

    elsif (paper_status & 4) != 0
      @status = :warning
      @error = Interface::Printer::PAPER_NEAR_END

    else
      @status = :ready
      @error = nil
    end

    @sp.write MODEL_QUERY
    printer_model, = read_response(1).unpack("C*")

    @sp.write VERSION_QUERY
    printer_version = read_response(4)

    @model = "ESC/POS Printer #{printer_model.to_s 16}"
    @version = "ROM #{printer_version}"

  rescue => e
    Smartware::Logging.logger.warn "Printer communication error: #{e}"
    e.backtrace.each { |line| Smartware::Logging.logger.warn line }

    @buf = ""

    @error = Interface::Printer::COMMUNICATION_ERROR
    @status = :error
  end
end