class SerialPortMTP

Constants

BOLD_MASK
CODE11
CODE128
CODE39
CODE93
CODEBAR
DOUBLE_HEIGHT_MASK
DOUBLE_WIDTH_MASK
EAN13
EAN8
I25
INVERSE_MASK

Character commands

MSI
STRIKE_MASK
UPC_A
UPC_E
UPDOWN_MASK

Public Class Methods

new(port: "/dev/ttyAMA0", baud_rate: 19200, heat_time: 150) click to toggle source
# File lib/serialport_mtp.rb, line 18
def initialize(port: "/dev/ttyAMA0", baud_rate: 19200, heat_time: 150)

  #params for serial port
  @port, @baud_rate, @data_bits, @stop_bits, @parity = port, baud_rate, 
                                                       8, 1, SerialPort::NONE    
  
  @heat_time = heat_time
  @print_mode = 0
  
end

Public Instance Methods

bold_off() click to toggle source
# File lib/serialport_mtp.rb, line 190
def bold_off
  unset_print_mode(BOLD_MASK)
end
bold_on() click to toggle source
# File lib/serialport_mtp.rb, line 186
def bold_on
  set_print_mode(BOLD_MASK)
end
double_height_off() click to toggle source
# File lib/serialport_mtp.rb, line 166
def double_height_off
  unset_print_mode(DOUBLE_HEIGHT_MASK)
end
double_height_on() click to toggle source
# File lib/serialport_mtp.rb, line 162
def double_height_on
  set_print_mode(DOUBLE_HEIGHT_MASK)
end
double_width_off() click to toggle source
# File lib/serialport_mtp.rb, line 174
def double_width_off
  unset_print_mode(DOUBLE_WIDTH_MASK)
end
double_width_on() click to toggle source
# File lib/serialport_mtp.rb, line 170
def double_width_on
  set_print_mode(DOUBLE_WIDTH_MASK)
end
feed(lines=1) click to toggle source

Feeds by the specified number of lines

# File lib/serialport_mtp.rb, line 70
def feed(lines=1)
  # The datasheet claims sending bytes 27, 100, <x> will work
  # but it feeds much much more.
  lines.times { write(10) }
end
feed_rows(rows) click to toggle source

Feeds by the specified number of rows of pixels

# File lib/serialport_mtp.rb, line 77
def feed_rows(rows)
  write_bytes(27, 74, rows)
end
flush() click to toggle source
# File lib/serialport_mtp.rb, line 81
def flush
  write_bytes(12)
end
inverse_off() click to toggle source
# File lib/serialport_mtp.rb, line 150
def inverse_off
  write_bytes ASCII_GS, 'B', 0
end
inverse_on() click to toggle source
# File lib/serialport_mtp.rb, line 146
def inverse_on
  write_bytes ASCII_GS, 'B', 1
end
justify(position) click to toggle source
# File lib/serialport_mtp.rb, line 219
def justify(position)
  byte = case position
  when :left
    0
  when :center
    1
  when :right
    2
  end

  write_bytes(0x1B, 0x61, byte)
end
normal() click to toggle source

This will reset bold, inverse, strikeout, upside down and font size It does not reset underline, justification or line height

# File lib/serialport_mtp.rb, line 141
def normal
  @print_mode = 0
  write_print_mode
end
offline() click to toggle source

Take the printer offline. Print commands sent after this will be ignored until `online` is called

# File lib/serialport_mtp.rb, line 324
def offline
  write_bytes(27, 61, 0)
end
online() click to toggle source

Take the printer back online. Subsequent print commands will be obeyed.

# File lib/serialport_mtp.rb, line 330
def online
  write_bytes(27, 61, 1)
end
print(string) click to toggle source
print_barcode(text, type) click to toggle source
print_bitmap(*args) click to toggle source
println(string) click to toggle source
# File lib/serialport_mtp.rb, line 103
def println(string)
  print(string + "\n")
end
reset() click to toggle source

reset printer

# File lib/serialport_mtp.rb, line 55
def reset
  write_bytes(27, 64)
end
set_barcode_height(val) click to toggle source

Barcodes

# File lib/serialport_mtp.rb, line 299
def set_barcode_height(val)
  # default is 50
  write_bytes(29, 104, val)
end
set_char_spacing(spacing) click to toggle source
# File lib/serialport_mtp.rb, line 358
def set_char_spacing(spacing)
  write_bytes(27, 32, 0, 10)
end
set_default() click to toggle source

reset formatting

# File lib/serialport_mtp.rb, line 60
def set_default
  online
  normal
  underline_off
  justify(:left)
  set_line_height(32)
  set_barcode_height(50)
end
set_line_height(val=32) click to toggle source
# File lib/serialport_mtp.rb, line 362
def set_line_height(val=32)
  write_bytes(27, 51, val) # default is 32
end
set_print_mode(mask) click to toggle source
# File lib/serialport_mtp.rb, line 125
def set_print_mode(mask)
  @print_mode |= mask;
  write_print_mode
end
set_size(size) click to toggle source
# File lib/serialport_mtp.rb, line 194
def set_size(size)
  byte = case size
  when :small
    0
  when :medium
    10
  when :large
    25
  end

  write_bytes(29, 33, byte, 10)
end
sleep() click to toggle source

Put the printer into a low-energy state immediately

# File lib/serialport_mtp.rb, line 335
def sleep
  sleep_after(0)
end
sleep_after(seconds) click to toggle source

Put the printer into a low-energy state after the given number of seconds

# File lib/serialport_mtp.rb, line 341
def sleep_after(seconds)
  write_bytes(27, 56, seconds)
end
start() click to toggle source
# File lib/serialport_mtp.rb, line 29
def start()
  
  @serial = SerialPort.new(@port, @baud_rate, @data_bits, @stop_bits, @parity)    
  @serial.sync = true    
  reset()

  heat_interval = 50 # 2 is default from page 23 of datasheet. Controls speed of printing and darkness
  print_density = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.
  print_break_time = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.

  write_bytes(27, 55)
  write_bytes(7) # Default 64 dots = 8*('7'+1)
  write_bytes(@heat_time) # Default 80 or 800us
  write_bytes(heat_interval) # Default 2 or 20us

  # Modify the print density and timeout
  write_bytes(18, 35)
  print_setting = (print_density << 4) | print_break_time
  write_bytes(print_setting) # Combination of print_density and print_break_time
end
strike_off() click to toggle source
# File lib/serialport_mtp.rb, line 182
def strike_off
  unset_print_mode(STRIKE_MASK)
end
strike_on() click to toggle source
# File lib/serialport_mtp.rb, line 178
def strike_on
  set_print_mode(STRIKE_MASK)
end
tab() click to toggle source

not working? ====

# File lib/serialport_mtp.rb, line 354
def tab
  write(9)
end
test_page() click to toggle source
# File lib/serialport_mtp.rb, line 50
def test_page
  write_bytes(18, 84)
end
underline_off() click to toggle source
# File lib/serialport_mtp.rb, line 215
def underline_off
  underline_on(0)
end
underline_on(weight=1) click to toggle source

Underlines of different weights can be produced: 0 - no underline 1 - normal underline 2 - thick underline

# File lib/serialport_mtp.rb, line 211
def underline_on(weight=1)
  write_bytes(27, 45, weight)
end
unset_print_mode(mask) click to toggle source
# File lib/serialport_mtp.rb, line 130
def unset_print_mode(mask)
  @print_mode &= ~mask;
  write_print_mode
end
upside_down_off() click to toggle source
# File lib/serialport_mtp.rb, line 158
def upside_down_off
  unset_print_mode(UPDOWN_MASK);
end
upside_down_on() click to toggle source
# File lib/serialport_mtp.rb, line 154
def upside_down_on
  set_print_mode(UPDOWN_MASK);
end
wake() click to toggle source

Wake the printer from a low-energy state. This command will wait for 50ms (as directed by the datasheet) before allowing further commands to be send.

# File lib/serialport_mtp.rb, line 348
def wake
  write_bytes(255)
  # delay(50) # ?
end
write(c) click to toggle source
# File lib/serialport_mtp.rb, line 107
def write(c)
  return if (c == 0x13)
  write_bytes(c)
end
write_bytes(*bytes) click to toggle source
# File lib/serialport_mtp.rb, line 112
def write_bytes(*bytes)
  bytes.each { |b| @serial.putc(b) }
end
write_print_mode() click to toggle source
# File lib/serialport_mtp.rb, line 135
def write_print_mode
  write_bytes(27, 33, @print_mode)
end