小票打印机指令集封装(支持EPSON指令)
最近写了一些关于小票打印机的程序,不难,但是记录下来,作为足迹吧。
现在市场上的小票机基本都支持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<lineNum;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 C )
* ESC M n
* @return bytes for this command
* @throws UnsupportedEncodingException
*/
public static String select_fontC()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 77;
result[2] = 2;
return new String(result);
}
//反显打印
public static String reversePrint(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 66;
result[2] = 1;
return new String(result);
}
//取消反显
public static String reverseCancel(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 66;
result[2] = 0;
return new String(result);
}
/****************************下划线*******************************/
//下划线一点宽(不支持汉字)
public static String underlineOne(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;
result[2] = 1;
return new String(result);
}
//下划线两点宽(不支持汉字)
public static String underlineTwo(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;
result[2] = 2;
return new String(result);
}
//取消下划线(不支持汉字)
public static String UnderlineCancle(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 45;
result[2] = 0;
return new String(result);
}
/****************************跳格*******************************/
//设置横向跳格位置
public static String Jump(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 68;
result[2] = 0;
return new String(result);
}
/****************************倍数放大*******************************/
//纵向放大两倍(汉字,英文,数字都支持)
public static String longitudinalDouble(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 33;
result[2] = 1;
return new String(result);
}
//横向放大两倍(汉字,英文,数字都支持)
public static String transverseDouble(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 33;
result[2] = 16;
return new String(result);
}
//纵向,横向放大两倍(汉字,英文,数字都支持)
public static String bothDouble(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 33;
result[2] = 17;
return new String(result);
}
//纵向,横向放大取消(汉字,英文,数字都支持)
public static String ZoomCancel(){
byte[] result = new byte[3];
result[0] = GS;
result[1] = 33;
result[2] = 0;
return new String(result);
}
//倍宽,倍高
public static String doubleFont(){
byte[] result = new byte[3];
result[0] = FS;
result[1] = 87;
result[2] = 1;
return new String(result);
}
//取消倍宽,倍高
public static String doubleCancel(){
byte[] result = new byte[3];
result[0] = FS;
result[1] = 87;
result[2] = 0;
return new String(result);
}
//字體
public static String fontA(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 77;
result[2] = 0;
return new String(result);
}
public static String fontB(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 77;
result[2] = 1;
return new String(result);
}
//字符集
public static String fontji(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 82;
result[2] = 15;
return new String(result);
}
//字符
public static String fontzidingyi(){
byte[] result = new byte[5];
result[0] = FS;
result[1] = 50;
result[2] = 1;
result[3] = 1;
result[4] = 12;
return new String(result);
}
//设置汉字字符左右边距
public static String margin1(){
byte[] result = new byte[4];
result[0] = FS;
result[1] = 83;
result[2] = 1;
result[3] = 1;
return new String(result);
}
//设置汉字字符左右边距
public static String margin2(){
byte[] result = new byte[4];
result[0] = FS;
result[1] = 83;
result[2] = 2;
result[3] = 2;
return new String(result);
}
//设置汉字字符左右边距
public static String marginCancle(){
byte[] result = new byte[4];
result[0] = FS;
result[1] = 83;
result[2] = 0;
result[3] = 0;
return new String(result);
}
//设置行间距1
public static String rowSpacing1(){
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 51;
result[2] = 1;
return new String(result);
}
//设置行间距2
public static String rowSpacing2(){
byte[] result = new byte[3];
result[0] = FS;
result[1] = 51;
result[2] = 2;
return new String(result);
}
//设置行间距
public static String rowSpacingCancel(){
byte[] result = new byte[3];
result[0] = FS;
result[1] = 51;
result[2] = 0;
return new String(result);
}
}
将byte[]转化成了String,方便拼接字符串。
下面是测试方法。
package aheiziUtil;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestMain {
private static void print(String ip,String printContent) {
Socket socket = null;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(ip, 9100), 4000);
System.out.println("CashierDesk=====小票机连接成功,IP:" + ip);
OutputStream os = socket.getOutputStream();
// 切纸命令
String text = printContent + "\n\n\n\n\n\n";
byte[] CMD_INIT = { 27, 64 };
os.write(CMD_INIT);
os.write(text.getBytes("GB2312"));
final byte[] CMD_CUT = { 0x1D, 0x56, 0x01 };
os.write(CMD_CUT);
System.out.println("CashierDesk=====小票机打印完成,IP:" + ip);
} catch (UnknownHostException e) {
System.out.println("CashierDesk=====小票机连接失败,IP:" + ip);
e.printStackTrace();
} catch (IOException e) {
System.out.println("CashierDesk=====小票机连接失败,IP:" + ip);
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String ip = "192.168.80.209";
String printContent = "";
printContent += "********************倍数放大********************\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "********************加粗加粗********************\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += PrinterCmdUtils.boldOn();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.boldOff();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += PrinterCmdUtils.boldOn();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.boldOff();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += PrinterCmdUtils.boldOn();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.boldOff();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "********************改变字体********************\n";
printContent += "改变字体(仅支持英文,数字)\n";
printContent += PrinterCmdUtils.fontB();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.fontA();
printContent += "\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += PrinterCmdUtils.fontB();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.fontA();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += PrinterCmdUtils.fontB();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.fontA();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += PrinterCmdUtils.fontB();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.fontA();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "********************反显打印********************\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += PrinterCmdUtils.reversePrint();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.reverseCancel();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += PrinterCmdUtils.reversePrint();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.reverseCancel();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += PrinterCmdUtils.reversePrint();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.reverseCancel();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "********************汉字间距1*******************\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += PrinterCmdUtils.margin1();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += PrinterCmdUtils.margin1();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += PrinterCmdUtils.margin1();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "********************汉字间距2*******************\n";
printContent += "纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.longitudinalDouble();
printContent += PrinterCmdUtils.margin2();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.transverseDouble();
printContent += PrinterCmdUtils.margin2();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";
printContent += "横向,纵向放大两倍(汉字,英文,数字都支持)\n";
printContent += PrinterCmdUtils.bothDouble();
printContent += PrinterCmdUtils.margin2();
printContent += "我是中文\nI'm english\n54123456\n";
printContent += PrinterCmdUtils.marginCancle();
printContent += PrinterCmdUtils.ZoomCancel();
printContent += "\n";*/
print(ip, printContent);
}
}
小票打印机指令集封装(支持EPSON指令)的更多相关文章
- C#调用斑马打印机打印条码标签(支持COM、LPT、USB、TCP连接方式和ZPL、EPL、CPCL指令)【转】
原文地址:http://blog.csdn.net/ldljlq/article/details/7338772 在批量打印商品标签时一般都要加上条码或图片,而这类应用大多是使用斑马打印机,所以我也遇 ...
- 让 Odoo POS 支持廉价小票打印机
为了测试 Odoo 在实际业务中的实施,我们开了一家(马上要开第二家分店)猪肉店.由于预算有限,在实施 Odoo PoS 的时候采购了一台价格为 85 元的爱宝热敏打印机,结果连上 Odoo Posb ...
- C#并口热敏小票打印机打印位图包括芯片的写入
下面是打印所需要调用的代码: class LptControl { private string LptStr = "lpt1"; public LptControl(string ...
- ASP.NET页面支持的指令
页面的处理指令 页面指令的处理用于配置执行该页面的运行时环境.在ASP.NET中,指令可以位于页面的任何位置,但良好且常见的习惯是将其置于文件的开始部分.除此,页面指令的名称是不区分大小写的,且指令的 ...
- C# Lpt 并口热敏小票打印机打印位图
class LptControl { private string LptStr = "lpt1"; public LptControl(string l_LPT_Str) { L ...
- 转:C#并口热敏小票打印机打印位图
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class LptControl { private s ...
- C# 热敏打印机 小票打印机 打印图片
最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: 因为我们这里主要是打印条形码和二维码,所以以条形码和二维 ...
- C#并口热敏小票打印机打印位图
原文:C#并口热敏小票打印机打印位图 最近一直在研究并口小票打印机打印图片问题,这也是第一次和硬件打交道,不过还好,最终成功了. 这是DEMO的窗体: 下面是打印所需要调用的代码: class ...
- c#调用刀片小票打印机
public static bool Print(int orderId, string orderTime) { bool b = true; string cut = ((char)29).ToS ...
随机推荐
- 生产环境服务CPU高问题分析
问题描述: 现网个别时候会出现CPU突然飙高的现象,飙高后不能恢复正常. 分析过程: CPU飙高后抓dump,最好本机看,其它机器看dump可能需要下载服务运行机器的sos,clr 0:000 ...
- CSS框架分析与网站的CSS架构
框架(framework)是一个基本概念上的结构,用于去解决或者处理复杂的问题,是一种可复用的构架. 我们对CSS框架这个词比较陌生,但对于JavaScript框架就比较熟悉了,比如jQuery 但为 ...
- 解决g++:command not found(centos7.0)
问题背景,因为装了虚拟机,系统为centos7.0,由于是纯净版,没有gcc,使用命令yum install gcc安装了gcc,但是没安装g++,导致g++:command not found问题. ...
- 机器学习(1)_R与神经网络之Neuralnet包
本篇博客将会介绍R中的一个神经网络算法包:Neuralnet,通过模拟一组数据,展现其在R中是如何使用,以及如何训练和预测.在介绍Neuranet之前,我们先简单介绍一下神经网络算法. 人工神经网络( ...
- 基于Heritrix的特定主题的网络爬虫配置与实现
建议在了解了一定网络爬虫的基本原理和Heritrix的架构知识后进行配置和扩展.相关博文:http://www.cnblogs.com/hustfly/p/3441747.html 摘要 随着网络时代 ...
- WS之cxf与spring整合1
1.在web.xml中加入CXFServlet: <!-- 下面表示所有来自/cxfservice/*的请求,都交给 CXFServlet来处理 .--> <servlet> ...
- extjs笔记
1. ExtJs 结构树.. 2 2. 对ExtJs的态度.. 3 3. Ext.form概述.. 4 4. Ext.TabPanel篇.. 5 5. Functio ...
- mysql 的 GROUP_CONCAT
GROUP_CONCAT 通常跟 group by 一起用,但也可以不用.例:select GROUP_CONCAT(pct_id) as pct_ids from (select max(pct_i ...
- mongodb使用中遇到的问题汇总
1. 每次重新打开mongo,都会显示:forked process:xxxx ,然后用 find -name mongod.lock 进行搜索,发现在 ./var/lib/mongodb/ 目录下又 ...
- 自定义延时查询控件---valen
当查询已经成标配 查询是已成为每个应用常用的功能,也正是这样前端后对查询的设计需求也日益增加,本文针对前端(Android端)查询控件做一个例子: 控件设计与逻辑 产品的设计UI图; 要达到如下 1| ...