Skip to content

Instantly share code, notes, and snippets.

@tabuchid
Created October 19, 2011 18:07
Show Gist options
  • Save tabuchid/1299154 to your computer and use it in GitHub Desktop.
Save tabuchid/1299154 to your computer and use it in GitHub Desktop.
printer
#include <NewSoftSerial.h>
#include <Thermal.h>
int printer_RX_Pin = 2;
int printer_TX_Pin = 3;
Thermal printer(printer_RX_Pin, printer_TX_Pin);
void setup() {
Serial.begin(9600); // Initialize serial port
}
void loop()
{
serialReader();
}
void serialReader(){
int btSerialReadPosition;
int inByte;
char serialReadString[50];
inByte = Serial.read();
btSerialReadPosition=0;
if (inByte > 0 && inByte != 13) { //If we see data (inByte > 0) and that data isn't a carriage return
delay(100); //Allow serial data time to collect (I think. All I know is it doesn't work without this.)
while (inByte != 13 && Serial.available() > 0){ // As long as EOL not found and there's more to read, keep reading
serialReadString[btSerialReadPosition] = inByte; // Save the data in a character array
btSerialReadPosition++; //Increment position in array
//if (inByte > 0) Serial.println(inByte); // Debug line that prints the charcodes one per line for everything recieved over serial
inByte = Serial.read(); // Read next byte
}
if (inByte == 13) //If we terminated properly
{
serialReadString[btSerialReadPosition] = 0; //Null terminate the serialReadString (Overwrites last position char (13 - newline) with 0
printer.println(serialReadString);
printer.feed(); //advance one line
printer.feed(); //advance one line
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment