最近写了一些关于小票打印机的程序,不难,但是记录下来,作为足迹吧。
现在市场上的小票机基本都支持EPSON指令。指令集文档
对指令集进行了自己的封装,方便以后调用:
package aheiziUtil;
import java.io.UnsupportedEncodingException;
public class PrinterCmdUtils {
public static final byte ESC = 27;//换码
public static final byte FS = 28;//文本分隔符
public static final byte GS = 29;//组分隔符
public static final byte DLE = 16;//数据连接换码
public static final byte EOT = 4;//传输结束
public static final byte ENQ = 5;//询问字符
public static final byte SP = 32;//空格
public static final byte HT = 9;//横向列表
public static final byte LF = 10;//打印并换行(水平定位)
public static final byte CR = 13;//归位键
public static final byte FF = 12;//走纸控制(打印并回到标准模式(在页模式下) )
public static final byte CAN = 24;//作废(页模式下取消打印数据 )
//------------------------换行-----------------------------
/**
* 换行
* @param lineNum要换几行
* @return
*/
public static byte[] nextLine(int lineNum)
{
byte[] result = new byte[lineNum];
for(int i=0;i
{
result[i] = LF;
}
return result;
}
//------------------------加粗-----------------------------
//选择加粗模式
public static String boldOn()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 69;
result[2] = 0xF;
return new String(result);
}
//取消加粗模式
public static String boldOff()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 69;
result[2] = 0;
return new String(result);
}
//------------------------对齐-----------------------------
/**
* 左对齐
* @return
* @throws UnsupportedEncodingException
*/
public static String alignLeft()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 0;
return new String(result);
}
/**
* 居中对齐
* @return
* @throws UnsupportedEncodingException
*/
public static String alignCenter()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 1;
return new String(result);
}
/**
* 右对齐
* @return
*/
public static byte[] alignRight()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 97;
result[2] = 2;
return result;
}
/**
* 水平方向向右移动col列
* @param col
* @return
*/
public static byte[] set_HT_position( byte col )
{
byte[] result = new byte[4];
result[0] = ESC;
result[1] = 68;
result[2] = col;
result[3] = 0;
return result;
}
/**
* Select Font A
* ESC M n
* @return bytes for this command
* @throws UnsupportedEncodingException
*/
public static String select_fontA()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 77;
result[2] = 0;
return new String(result);
}
/**
* Select Font B
* ESC M n
* @return bytes for this command
* @throws UnsupportedEncodingException
*/
public static String select_fontB()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 77;
result[2] = 1;
return new String(result);
}
/**
* Select Font C ( some printers don't have font