Discussions: Many customers, using Java, have
requested example code for Java applications
Goals: Provide example code for ZPL, EPL and CPCL
printers
Facts or Affected Items: Java, Application, Visual J++, ZPL,
EPL, CPCL
Details
The following examples demonstrate how Java can be used to send data to
the printer via the serial , parallel ports and via a network
connection.
ZPL
Parallel Port
import java.io.*;
public class lpt //create a class called "lpt"
{
public static void main (String[] argv)
{
try {
FileOutputStream os = new FileOutputStream("LPT1");
PrintStream ps = new PrintStream(os);
//ZPL goes here
ps.println("^XA^FO50,50^A050,50^FDTech Support^FS^XZ");
//flush buffer and close
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
Serial Port
import java.io.*;
public class comtest //creates a class called "comtest"
{
public static void main (String[] argv)
{
try {
FileOutputStream os = new FileOutputStream("COM1");
PrintStream ps = new PrintStream(os);
//ZPL goes here
ps.println("^XA^FO50,50^A050,50^FDTech Support^FS^XZ");
//flush buffer and close
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
EPL
import java.io.*;
public class lptepl //create a class called "lptepl"
{
public static void main (String[] argv)
{
try {
FileOutputStream os = new FileOutputStream("LPT1");
PrintStream ps = new PrintStream(os);
//EPL goes here
ps.println(" ");
ps.println("N");
ps.println("A50,150,0,4,1,1,N,\"Tech Support\"");
ps.println("P1");
//flush buffer and close
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
******The above example demonstrates how to send the "
character to the printer
CPCL
import java.io.*;
public class comcpcl //create a class called "comcpcl"
{
public static void main (String[] argv)
{
try {
FileOutputStream os = new FileOutputStream("COM1");
PrintStream ps = new PrintStream(os);
//CPCL goes here
ps.println("! 0 200 200 25 1");
ps.println("BAR-SENSE");
ps.println("TEXT 5 1 0 5 Tech. Support");
ps.println("FORM");
ps.println("PRINT");
//flush buffer and close
ps.close();
} catch (Exception e) {
System.out.println("Exception occurred: " + e);
}
}
}
NETWORK CONNECTION
import java.io.*;
import java.net.*;
class TCPClient
{
public static void main (String argv[]) throws Exception
{
// The line below illustrates the default port 6101 for mobile printers 9100
is the default port number
// for desktop and tabletop printers
Socket clientSocket=new Socket("10.17.50.105",6101);
DataOutputStream outToServer = new
DataOutputStream(clientSocket.getOutputStream() );
//The data being sent in the lines below illustrate CPCL one can change the
data for the corresponding
//language being used (ZPL, EPL)
outToServer.writeBytes("! 0 200 200 203 1" + '\n' + "CENTER" + '\n');
outToServer.writeBytes("TEXT 0 3 10 50 JAVA TEST" + '\n' + "PRINT" + '\n');
clientSocket.close();
}
}
Notice: Zebra Technologies Corporation makes no representations about
the suitability of this documentation for any purpose. It is provided "as
is", for your information only, without warranty of any kind, either
expressed or implied, including, but not limited to, implied warranties of
merchantability, fitness for a particular purpose and
non-infringement.
Date Updated: 10/10/2007 09:12:22 AM
|