We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Sending text to printer with exec command
Page Index Toggle Pages: 1
Sending text to printer with exec command (Read 1574 times)
Sending text to printer with exec command
Aug 23rd, 2008, 7:45pm
 
So this code has been working in a separate project, but it only works once. How do I clear out the process so it continues to send new print jobs after the first time?

Code:

String[] command;

void setup()
{
 command = new String[3];
 command[0] = "/bin/bash";
 command[1] = "-c";
 command[2] = "echo test | lpr -P ReceiptPrinter -l";
 sendToPrinter("Now we will start up and begin this entire process");
}

void sendToPrinter(String printText) {
 command[2] = "echo "+printText+" | lpr -P ReceiptPrinter -l";
 try {
   Process p = exec(command);
   BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   String line = null;
   while ((line = in.readLine()) != null) {
     System.out.println(line);
   }
 }
 catch (IOException e) {
   e.printStackTrace();
 }
}
Re: Sending text to printer with exec command
Reply #1 - Aug 24th, 2008, 9:35pm
 
alright well this is getting a bit more complicated. I can send plain text whenever I get a packet, but I can't print the message for whatever reason. I'm gonna try converting the text to whatever I can but I'm at a loss here. Here is all of my code, if you have any idea how to fix this I would be very much obliged.

I'm making a politically charged art piece for my senior year about the NSA and other domestic spying. The plan is to send the parsed data to a receipt printer, then allow the public to use black markers and highlighters on the unencrypted messages sent over Wi-Fi.

Code:

import org.rsg.carnivore.*;
import org.rsg.lib.Log;

String input;
String[] command;

void setup()
{
 size(800,600);
 CarnivoreP5 c = new CarnivoreP5(this);
 //the command is set up so that the text from the AIM message gets piped to the print job
 command = new String[3];
 command[0] = "/bin/bash";
 command[1] = "-c";
 command[2] = "echo test | lpr -P ReceiptPrinter -l";
 //this is some test text to check the printer connection
 sendToPrinter("Now we will start up and begin this entire process");
}

void draw () {
}

//triggered whenever a packet comes in
void packetEvent(CarnivorePacket p){
 String packetparsed = packetHtmlParser(p.ascii());
 if (packetparsed != "") {
   println(">> "+packetparsed);
   sendToPrinter(packetparsed.ascii);
 }
}

//a string function to grab the HTML string out of a packet (thanks fry)
String packetHtmlParser(String packetData) {
 // StringBuffer is more efficient than String
 StringBuffer plain = new StringBuffer();
 // better use lowercase
 int start = packetData.toLowerCase().indexOf("<html");
 int stop = packetData.toLowerCase().indexOf("</html>");
 if (start == -1 || stop == -1) return "";
 // get the portion inside the html tags
 String useful = packetData.substring(start, stop);
 // split on each < or >
 String[] pieces = split(useful, '<');
 // split each line into halves, and use the second half
 for (int i = 1; i < pieces.length; i++) {
   plain.append(pieces[i].substring(pieces[i].indexOf('>') + 1));
 }
 return plain.toString();
}

//function to send text to the printer
void sendToPrinter(String printText) {
 command[2] = "echo '"+printText+"' | lpr -P ReceiptPrinter -l";
 try {
   Process p = exec(command);
   BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
   String line = null;
   while ((line = in.readLine()) != null) {
     //print console errors and other output
     System.out.println(line);
   }
   //kill the process
   p.destroy();
 }
 catch (IOException e) {
   e.printStackTrace();
 }
}


edit:
found the problem, the printer was waiting until the entire paragraph was written before it printed, I just need to add in some newlines and I'll be set. Feel free to modify and use non-commercially.
Re: Sending text to printer with exec command
Reply #2 - Jul 13th, 2009, 12:33pm
 
how do i get the name of the printer? it doesn't seem to work with ReceiptPrinter. but that might also be because i haven't installed a driver...
Page Index Toggle Pages: 1