Skip to content
This repository
tree: 3a369594c7
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 145 lines (114 sloc) 3.543 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 78 79 80 81 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#include <SoftwareSerial.h>

#define rxPin 6
#define txPin 7

SoftwareSerial printer = SoftwareSerial(rxPin, txPin);

const byte command = 0x1B;

const byte fullcut = 0x69;
const byte partialcut = 0x6D;

const byte doubleprint = 0x47;
const byte flipchars = 0x7B;
const byte rotatechars = 0x56;

const byte commandBarcode = 0x1D;
const byte commandBarcodePrint = 0x6B;
const byte commandBarcodeWidth = 0x77;
const byte commandBarcodeHeight = 0x68;
const byte commandBarcodeTextPosition = 0x48;

const byte barcodeNarrow = 0x02;
const byte barcodeMedium = 0x03;
const byte barcodeWide = 0x04;

const byte barcodePrintNone = 0x00;
const byte barcodePrintAbove = 0x01;
const byte barcodePrintBelow = 0x02;
const byte barcodePrintBoth = 0x03;

// (0 = UPC-A, 1 = UPC-E, 2 = JAN13 (EAN), 3 = JAN 8 (EAN),
// 4 = code 39, 5 = ITF, 6 = codabar, 7 = code 128)
const byte barcodeModeUPCA = 0x00;
const byte barcodeModeUPCE = 0x01;
const byte barcodeModeJAN13AEN = 0x02;
const byte barcodeModeJAN8EAN = 0x03;
const byte barcodeModeCODE39 = 0x04;
const byte barcodeModeITF= 0x05;
const byte barcodeModeCODEABAR= 0x06;
const byte barcodeModeCODE128 = 0x07;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  printer.begin(9600);
  Serial.begin(9600); //open the USB connection too

  delay(1000);

  feed();
  
  println("Hello, World!");
  
  feed();
  
  setBarcodeHeight(50);
  setBarcodeWidth(barcodeNarrow);
  setBarcodeTextPosition(barcodePrintBelow);
  printBarcode("123456789012");
  
  feed();
  cut();
}

void print(char text[]) {
  printer.print(text);
}

void println(char text[]) {
  printer.println(text);
}

void printBarcode(char barcode[]) {
  printBarcode(barcode, barcodeModeUPCA);
}

void feed() {
  printer.println("");
  printer.println("");
  printer.println("");
}

void cut() {
  printer.print(command, BYTE);
  printer.print(fullcut, BYTE);
}

void partialCut() {
  printer.print(command, BYTE);
  printer.print(partialcut, BYTE);
}

void printBarcode(char barcode[], byte barcodeMode) {
  printer.print(commandBarcode, BYTE); //"[1D]h + [6B]h + n + Dn + [00]h"
  printer.print(commandBarcodePrint, BYTE);
  printer.print(barcodeMode, BYTE); //barcode system.
  printer.print(barcode); //barcode value
  printer.print(0x00, BYTE); //end barcode sequence
}

void setBarcodeHeight(byte height) { //in dots. default = 162
  printer.print(commandBarcode, BYTE); //"[1d]H + [68]H + N"
  printer.print(commandBarcodeHeight, BYTE);
  printer.print(height, BYTE); //barcode height in dots. default = 162
}

void setBarcodeWidth(byte width) { // 2, 3 or 4. default = 3
  if (width < 2) width = 2;
  if (width > 4) width = 4;
  printer.print(commandBarcode, BYTE); //"[1d]H + [77]H + N" (where N = 2, 3 or 4)
  printer.print(commandBarcodeWidth, BYTE);
  printer.print(width, BYTE);
}

void setBarcodeTextPosition(byte position) {
  if (position < 0) position = 0;
  if (position > 3) position = 3;
  printer.print(commandBarcode, BYTE); // "[1d]H + [48]H + N"
  printer.print(commandBarcodeTextPosition, BYTE);
  printer.print(position, BYTE); // 0 = none, 1 = above, 2 = below, 3 = both
}

void setDoublePrintOn() {
  setDoublePrint(0x01);
}

void setDoublePrintOff() {
  setDoublePrint(0x00);
}

void setDoublePrint(byte mode) {
  printer.print(command, BYTE);
  printer.print(doubleprint, BYTE);
  printer.print(mode, BYTE);
}

void loop() {
  // while there are bytes to read from the computer, retransmit them
  if (Serial.available() > 0) {
    byte inchar = Serial.read();
    printer.print(inchar);
  }
}
Something went wrong with that request. Please try again.