Android sockot连接打印机EPSON ESC/POS指令打印
生活随笔
收集整理的這篇文章主要介紹了
Android sockot连接打印机EPSON ESC/POS指令打印
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
sockot連接打印機EPSON ESC/POS指令打印
接了一個需求,需要用Android pad連接打印機進行打印,以前倒是沒接觸過,這次在網上找了下資料,簡單實現了下需求。在這記錄下相關代碼以及踩的坑- 首先第一步就是通過socket連接打印機
- 再通過socket發送打印指令就行
代碼步驟都比較簡單,先看下代碼
public class EscPos {public static final byte FS = 0x1C;public static final byte HT = 0x9;public static final byte LF = 0x0A;public static final byte CR = 0x0D;public static final byte ESC = 0x1B;public static final byte DLE = 0x10;public static final byte GS = 0x1D;public static final byte STX = 0x02;public static final byte US = 0x1F;public static final byte CAN = 0x18;public static final byte CLR = 0x0C;public static final byte EOT = 0x04;/* 初始化打印機 */public static final byte[] ESC_INIT = new byte[]{ESC, '@'};/* 設置標準模式 */public static final byte[] ESC_STANDARD = new byte[]{ESC, 'S'};/* 設置漢字打印模式 */public static final byte[] ESC_CN_FONT = new byte[]{FS, '&'};/* 選擇字符集 */public static final byte[] ESC_SELECT_CHARACTER = new byte[]{ESC, 'R', 9};/* 設置用戶自定義漢字字體 焗7118 */public static final byte[] ESC_FS_2 = new byte[]{FS, 0x32, 0x71, 0x18};/* 取消用戶自定義字體 */public static final byte[] ESC_CANCEL_DEFINE_FONT = new byte[]{ESC, '%', 0};/* 打開錢箱指令 */public static final byte[] ESC_OPEN_DRAWER = new byte[]{ESC, 'p', 0x00, 0x10, (byte) 0xff};/* 切紙指令GS V m* m 0,48 Executes a full cut(cuts the paper completely)* 1,49 Excutes a partilal cut(one point left uncut)*/public static final byte[] POS_CUT_MODE_FULL = new byte[]{GS, 'V', 0x00};public static final byte[] POS_CUT_MODE_PARTIAL = new byte[]{GS, 'V', 0x01};/* 西文字符 (半寬)字體A (6 ×12),漢字字符 (全寬)字體A (12×12) */public static final byte[] ESC_FONT_A = new byte[]{ESC, '!', 0};/* 西文字符 (半寬)字體B (8×16),漢字字符 (全寬)字體B (16×16) */public static final byte[] ESC_FONT_B = new byte[]{ESC, '!', 1};/* 12*24 0/48*/public static final byte[] ESC_FONTA = new byte[]{ESC, 'M', 48};/* 9*17 1/49*/public static final byte[] ESC_FONTB = new byte[]{ESC, 'M', 1};/* 默認顏色字體指令 */public static final byte[] ESC_FONT_COLOR_DEFAULT = new byte[]{ESC, 'r', 0x00};/* 紅色字體指令 */public static final byte[] ESC_FONT_COLOR_RED = new byte[]{ESC, 'r', 0x01};/* 標準大小 */public static final byte[] FS_FONT_ALIGN = new byte[]{FS, 0x21, 1, ESC, 0x21, 1};/* 橫向放大一倍 */public static final byte[] FS_FONT_ALIGN_DOUBLE = new byte[]{FS, 0x21, 4, ESC, 0x21, 4};/* 縱向放大一倍 */public static final byte[] FS_FONT_VERTICAL_DOUBLE = new byte[]{FS, 0x21, 8, ESC, 0x21, 8, GS, '!', 0x01};/* 橫向縱向都放大一倍 */public static final byte[] FS_FONT_DOUBLE = new byte[]{FS, 0x21, 12, ESC, 0x21, 48};/* 靠左打印命令 */public static final byte[] ESC_ALIGN_LEFT = new byte[]{0x1b, 'a', 0x00};/* 居中打印命令 */public static final byte[] ESC_ALIGN_CENTER = new byte[]{0x1b, 'a', 0x01};/* 靠右打印命令 */public static final byte[] ESC_ALIGN_RIGHT = new byte[]{0x1b, 'a', 0x02};/* 字體加粗 */public static final byte[] ESC_SETTING_BOLD = new byte[]{ESC, 0x45, 1};/* 取消字體加粗 */public static final byte[] ESC_CANCEL_BOLD = new byte[]{ESC, 0x45, 0};public static final int STATE_1 = 1;// 創建Socket連接public static final int STATE_2 = 2;// 獲取輸出流public static final int STATE_3 = 3;// 關閉Socketpublic static final int STATE_0 = 0;// 成功public static final int ERROR_2 = -2;// 創建Socket失敗public static final int ERROR_3 = -3;// 獲取輸出流失敗public static final int ERROR_4 = -4;// 關閉Socket出錯public static final int ERROR_100 = -100;// 失敗// 通過socket流進行讀寫private OutputStream socketOut = null;// 以ip作為key,EscPos實例作為value的Mapprivate static Map<String, EscPos> posMap = new HashMap<String, EscPos>();private static EscPos escPos = null;// 默認端口public static int DEFAULT_PORT = 9100;private Socket socket;private OnPrinterListener listener;private String ip;private int port;/*** 根據ip、端口、字符編碼構造工具類實例** @param ip 打印機ip* @param port 打印機端口,默認9100* @throws IOException*/public EscPos(String ip, int port) {this.ip = ip;this.port = port;}public synchronized static EscPos getInstance(String ip, Integer port) {escPos = posMap.get(ip);if (escPos == null) {escPos = new EscPos(ip, port);}return escPos;}public static synchronized EscPos getInstance(String ip) {return getInstance(ip, DEFAULT_PORT);}public static synchronized EscPos getInstance() {//這兒寫死或者傳入打印機地址和端口return getInstance("","";}public static void myPrinter(final List<String> list, final OnPrinterListener printerListener) {Runnable runnable = new Runnable() {@Overridepublic void run() {EscPos escPos = EscPos.getInstance();escPos.setListener(printerListener);escPos.connectSocket();try {escPos.init();for (int i = 0; i < list.size(); i++) {escPos.line(1);escPos.boldOff(true);escPos.printStr(list.get(i));//escPos.printStr("測試字體6");}escPos.line(10);escPos.feedAndCut();escPos.closeIOAndSocket();printerListener.onListener(STATE_0);} catch (IOException e) {printerListener.onError(ERROR_100);LOGGER.error("Socket printer write ===> ", e);e.printStackTrace();}}};new Thread(runnable).start();}public void connectSocket() {try {socket = new Socket();SocketAddress socAddress = new InetSocketAddress(ip, port);socket.connect(socAddress, 5000);if (listener != null) {listener.onListener(STATE_1);}try {socketOut = socket.getOutputStream();if (listener != null) {listener.onListener(STATE_2);}} catch (IOException e) {if (listener != null) {listener.onError(ERROR_3);}LOGGER.error("Socket printer getOutputStream ===> ", e);e.printStackTrace();}} catch (IOException e) {if (listener != null) {listener.onError(ERROR_2);}LOGGER.error("Socket printer connect ===> ", e);e.printStackTrace();}}/*** 換行** @param lineNum 換行數,0為不換行* @return* @throws IOException*/public EscPos line(int lineNum) throws IOException {for (int i = 0; i < lineNum; i++) {socketOut.write("\n".getBytes());socketOut.flush();}return this;}/*** 加粗** @param flag false為不加粗* @return* @throws IOException*/public EscPos bold(boolean flag) throws IOException {if (flag) {socketOut.write(ESC_SETTING_BOLD);}return this;}/*** 取消粗體** @param flag true為取消粗體模式* @return* @throws IOException*/public EscPos boldOff(boolean flag) throws IOException {if (flag) {socketOut.write(ESC_CANCEL_BOLD);//socketOut.write(FS_FONT_DOUBLE);}return this;}/*** 初始化打印機** @return* @throws IOException*/EscPos init() throws IOException {socketOut.write(ESC_INIT);return this;}/*** 打印空白** @param length 需要打印空白的長度* @throws IOException*/private void printSpace(int length) throws IOException {for (int i = 0; i < length; i++) {socketOut.write(" ".getBytes());}socketOut.flush();}/*** 進紙并全部切割** @return* @throws IOException*/public EscPos feedAndCut() throws IOException {socketOut.write(POS_CUT_MODE_FULL);socketOut.flush();return this;}public void closeIOAndSocket() {try {socketOut.close();socket.close();if (listener != null) {listener.onListener(STATE_3);}} catch (IOException e) {if (listener != null) {listener.onError(ERROR_4);}LOGGER.error("Socket printer close ===> ", e);e.printStackTrace();}}/*** 打印字符串** @param str 所需打印字符串* @return* @throws IOException*/public EscPos printStr(String str) throws IOException {socketOut.write(ESC_CN_FONT);socketOut.write(str.getBytes(Charset.forName("GBK")));socketOut.write(LF);socketOut.flush();return this;}public void setListener(OnPrinterListener listener) {this.listener = listener;}public interface OnPrinterListener {void onListener(int status);void onError(int errorCode);} }打印調用示例代碼
//printerData 是list<string>類型的數據 循環打印文案 EscPos.myPrinter(printerData, new EscPos.OnPrinterListener() {@Overridepublic void onListener(int status) {}@Overridepublic void onError(final int errorCode) {((Activity) context).runOnUiThread(new Runnable() {@Overridepublic void run() {//此時已在主線程中,可以更新UI了if (errorCode == EscPos.ERROR_2) {Toast.makeText(context, "連接打印機失敗", Toast.LENGTH_SHORT).show();} else {Toast.makeText(context, "打印異常", Toast.LENGTH_SHORT).show();}}});}});過程遇到的坑
-
socket連不上打印機
1.確保打印機的網和手機在同一個網段內
2.打印機端口和地址正確(可百度查詢如何確定打印機IP和端口) -
socket發送打印指令不起作用
這個當時坑死我了,我用一個串測試“hello word” ,結果就因為中間多加了空格,就是打印不出來,后來才知道,這種空格沒法打印,一定要寫個簡單的串先測試。
總結
以上是生活随笔為你收集整理的Android sockot连接打印机EPSON ESC/POS指令打印的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA生成springboot项目的两
- 下一篇: 一位大牛对学IT的忠告