If the only thing you will be printing is text to the receipt printer here is one possible solution. First, save the text you want to print to a text file (i.e. Myfile.txt). Then, use the launch command to open Windows Notepad.exe with the ‘/p’ option to automatically print the text file. See the code example below:
//Receipt Printer Test program
import processing.serial.*; //Use the serial library
import static javax.swing.JOptionPane.*; //Use the option pane dialog box library
String TS_file="Timeslip.txt"; //String variable for the Time Slip text file name
String Str1 = ""; //Working string variable
String Str2 = ""; //Working string variable
String Prt_Cmd = ""; //String for holding print command
String File_Path = "C:/Users/Bill/Processing/6-Sandbox/receipt_printer_test/Timeslip.txt";
PrintWriter TS_output; //Create an output variable for writing to the Timeslip.txt file
void setup() {
size(200,200);
background(255);
fill(0);
text("Printer Test",90,100);
Prt_Cmd = "notepad.exe /p " + File_Path; //Build the time slip print command string
}
void draw() {
Str1 = "PRINT REQUEST";
Str2 = "Do you wish to print the timeslip text file?";
int input = showConfirmDialog(null, Str2, Str1, YES_NO_OPTION); //Prompt: 0=yes, 1=no
if (input == 0) { //Did user respond with "YES"?
Create_Timeslip(); //Call subroutine to create timeslip text file
println("Timeslip file created"); //Optional debug line to let you know file was created
delay(100);
launch(Prt_Cmd); //Launch Windows command to print time slip
}
else {
exit(); //Exit program if "NO" was selected
}
delay (100);
}
void Create_Timeslip() { //Create the Time Slip text file
TS_output = createWriter(TS_file); //Open Time Slip text file in the sketch directory
TS_output.println(" DFW Winternationals");
TS_output.println(" Snowmobile Drag Racing");
TS_output.println("=================================");
TS_output.println(" Date: 09/02/2018");
TS_output.println(" Race# 04");
TS_output.println(" -Lane 1- -Lane 2-");
TS_output.println(" 00.276 React Time 00.271");
TS_output.println(" 03.781 60 Ft Time 03.632");
TS_output.println(" 27.641 Trap Speed MPH 36.320");
TS_output.println(" 07.934 Elapsed Time 07.725");
TS_output.println(" - - - Status WINNER");
TS_output.println("=================================");
TS_output.println(" Thanks for racing with us");
TS_output.println(" Bill V's Timers");
TS_output.flush(); //Writes any remaining data to the file
TS_output.close(); //Close the file
}