Class: Memopri::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/memopri/printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 16402) ⇒ Printer

Returns a new instance of Printer.



8
9
10
11
12
# File 'lib/memopri/printer.rb', line 8

def initialize(host, port=16402)
  @hostname = host
  @port = port
  @socket = TCPSocket.open(@hostname, @port)
end

Instance Method Details

#_recv(len) ⇒ Object



20
21
22
# File 'lib/memopri/printer.rb', line 20

def _recv(len)
  return @socket.read(len)
end

#_send(cmd) ⇒ Object



14
15
16
17
18
# File 'lib/memopri/printer.rb', line 14

def _send(cmd)
  cmd = cmd.pack('C*')
  @socket.write(cmd)
  @socket.flush()
end


24
25
26
27
28
29
30
31
32
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
# File 'lib/memopri/printer.rb', line 24

def print(data)
  cmd = [0x1b, 0x5a]
  _send(cmd)
  _recv(1)

  cmd = [0x05]
  _send(cmd)
  _recv(6)

  cmd = [0x06]
  _send(cmd)
  _recv(1)

  cmd = [0x1b, 0x49]
  _send(cmd)
  _recv(1)

  cmd = [0x05]
  _send(cmd)
  _recv(8)

  cmd = [0x06]
  _send(cmd)
  _recv(1)

  cmd = [0x1b, 0x50]
  _send(cmd)
  _recv(1)

  length = data.size

  cmd = [
    0x02, 0x80, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x02,
    0x80, 0x00,
    (length/16) & 0x00FF, ((length/16) >> 8) & 0x00FF,
    (length)    & 0x00FF, ((length)    >> 8) & 0x00FF,
    0x00, 0x00,
  ]
  _send(cmd)
  _recv(1)

  cmd = [0x1b, 0x56]
  _send(cmd)
  _recv(1)

  data.each_slice(64){|x|
    _send(x)
    _recv(1)
  }

  cmd = [0x1b, 0x42]
  _send(cmd)
  _recv(1)
end