项目要求实现远程打印,目前设备已经到货,打印机为芯烨XP-80X热敏打印机。
一:去http://www.xprinter.net/里下载使用手册和编程手册,注:其他品牌去百度搜索官网
二:去服务与支持菜单找到驱动,下载到本机,方便测试打印机,可以用本机连接打印机,进而设置相关参数,效果如下图
三:点击打印测试是否连得通打印机,如果可以的话,那么说明这台打印机的ip已经设置好,可以从电脑中拔出插口了。
四:打印机原理:实际打印机内部存在一个socekServer在时刻捕捉通过socket连接进来的线程,具体请参考另一篇socket小结,这里就不多做介绍了。
五:重点是:参考官网下载的编程手册,来发送对应格式的流数据来让打印机解决,之前百度了一下,发现并没有好的方法,所以自己只能详细阅读了一下,编程手册,用java写了一个工具类,希望能给大家帮助,直接上代码:
package com.tms.helper.utils;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
/**
* Created by haha on 2017/4/25.
*/
public class EscPosUtil {
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;
/**
* CodePage table
*/
public static class CodePage {
public static final byte PC437 = 0;
public static final byte KATAKANA = 1;
public static final byte PC850 = 2;
public static final byte PC860 = 3;
public static final byte PC863 = 4;
public static final byte PC865 = 5;
public static final byte WPC1252 = 16;
public static final byte PC866 = 17;
public static final byte PC852 = 18;
public static final byte PC858 = 19;
}
/**
* BarCode table
*/
public static class BarCode {
public static final byte UPC_A = 0;
public static final byte UPC_E = 1;
public static final byte EAN13 = 2;
public static final byte EAN8 = 3;
public static final byte CODE39 = 4;
public static final byte ITF = 5;
public static final byte NW7 = 6;
//public static final byte CODE93 = 72;
public static final byte CODE128 = 73;
}
/**
* Print and line feed
* LF
* @return bytes for this command
*/
public static String printLinefeed()
{
return "$ESC_10";
}
/**
* Turn underline mode on, set at 1-dot width
* ESC - n
* @return bytes for this command
*/
public static String underline1DotOn()
{
return "$ESC_27_45_1";
}
/**
* Turn underline mode on, set at 2-dot width
* ESC - n
* @return bytes for this command
*/
public static String underline2DotOn()
{
return "$ESC_27_45_2";
}
/**
* Turn underline mode off
* ESC - n
* @return bytes for this command
*/
public static String underlineOff()
{
return "$ESC_27_45_0";
}
/**
* Initialize printer
* Clears the data in the print buffer and resets the printer modes to the modes that were
* in effect when the power was turned on.
* ESC @
* @return bytes for this command
*/
public static String initPrinter()
{
return "$ESC_27_64";
}
/**
* Turn emphasized mode on
* ESC E n
* @return bytes for this command
*/
public static String emphasizedOn()
{
byte[] result = new byte[3];
result[0] = ESC;
result[1] = 69;
result[2] = 0xF;
return "$ESC_27_69_1";
}
/**
* Turn emphasized mode off
* ESC E n
* @return bytes for this command
*/
public static String emphasizedOff()
{
return "$ESC_27_69_0";
}
/**
* double_strike_on
* ESC G n
* @return bytes for this command
*/
public static String doubleStrikeOn()
{
return "$ESC_27_71_1";
}
/**
* double_strike_off
* ESC G n
* @return bytes for this command
*/
public static String doubleStrikeOff()
{
return "$ESC_27_71_0";
}
/**
* Select Font A
* ESC M n
* @return bytes for this command
*/
public static String selectFontA()
{
return "$ESC_27_77_0";
}
/**
* Select Font B
* ESC M n
* @return bytes for this command
*/
public static String selectFontB()
{
return "$ESC_27_77_1";
}
/**
* Select Font C ( some printers don't have font C )
* ESC M n
* @return bytes for this command
*/
public static String selectFontC()
{
return "$ESC_27_77_2";
}
/**
* double height width mode on Font A
* ESC ! n
* @return bytes for this command
*/
// public static String doubleHeightWidthOn()
// {
// return "$ESC_27_33_56";
// }
public static List<String> doubleHeightWidthOn()
{
return Arrays.asList("$ESC_27_33_56","$ESC_28_33_12");
}
/**
* double height width mode off Font A
* ESC ! n
* @return bytes for this command
*/
public static List<String> doubleHeightWidthOff()
{
return Arrays.asList("$ESC_27_33_0","$ESC_28_33_0");