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



129
130
131
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 129

def new_render
  Render.new
end


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 82

def print(data)
  start = Time.now

  data.force_encoding('BINARY')

  begin
    data.split(/(\n)/, -1).each do |line|
      @sp.write line

      loop do
        now = Time.now
        # timeout exceeded
        return if now - start > 10

        status = do_query
        next if status.nil?

        paper_status, user_status, recoverable, unrecoverable = status

        # Unrecoverable error or out of paper
        return if unrecoverable != 0 || (paper_status & 1) != 0

        # No error - continue printing
        break if (recoverable == 0) && ((user_status & 3) == 0)
      end
    end

    loop do
      now = Time.now
      # timeout exceeded
      return if now - start > 10

      status = do_query
      next if status.nil?

      paper_status, user_status, recoverable, unrecoverable = status

      # paper not in motion
      break if (user_status & 8) == 0
    end

  rescue => e
    Smartware::Logging.logger.error "Printer communication error: #{e}"
    e.backtrace.each { |line| Smartware::Logging.logger.error line }
  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
80
# File 'lib/smartware/drivers/printer/esc_pos.rb', line 33

def query
  begin
    paper_status, user_status, recoverable, unrecoverable = do_query

    raise "no response to query" if paper_status.nil?

    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)
    raise "no response to model query" if printer_model.nil?

    printer_model, = printer_model.unpack("C*")

    @sp.write VERSION_QUERY
    printer_version = read_response(4)
    raise "no response to version querty" if printer_version.nil?

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

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

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