打印开发中关于打印协议【澳门唯一金莎娱乐】,Android下的POS打印机调用的简单实现

打印开发中关于打印协议【澳门唯一金莎娱乐】,Android下的POS打印机调用的简单实现

打字与印刷机打字与印刷合同有三种,EPOS和ESC/POS.

正文基于GP58类别,它能够包容ESC/POS指令集,对EPSON的打字与印刷机通用.

对此EPOS的打字与印刷开采,能够选择微软的库POS.NET实行付出(连接打字与印刷机供给动用逻辑名字,
可以在打字与印刷机官方网站下载相关的驱动),对于ESC/POS,能够动用串口开采(连接打字与印刷机须求利用com口名字,对于除com口之外的打印机供给在官网络下载相关的驱动)

Android下的设备调节和测验,就算设备提供了驱动,依据商家的驱动调节和测量试验就能够;设备未提供驱动,只好依据通用的情势进行调解。这里运用的是调用USB接口来调整打字与印刷机输出。

1.首先得到USB微处理器

public UsbAdmin(Context context) { 
    mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); 
    mPermissionIntent = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_USB_PERMISSION), 0); 
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION); 
    context.registerReceiver(mUsbReceiver, filter); 
  }

选择三个推迟意图来接受usb接入时的广播,当播放选用届期,表达有新的配备连接。

增加一个boardcast action

复制代码 代码如下:

private static final String ACTION_USB_PERMISSION =
“com.android.example.USB_PERMISSION”;

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      if (ACTION_USB_PERMISSION.equals(action)) { 
        synchronized (this) { 
          UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); 
          if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { 
            if (device != null) { 
              setDevice(device); 
            } else { 
              Closeusb(); 
             // mDevice = device; 
            } 
          } else { 
            Log.d(TAG, "permission denied for device " + device); 
          } 

        } 

      } 
    } 
  }; 

取到usb设备的援引,android系统会理解你是不是同意设备访谈,默认为false;当允许了访问之后,会咬定USB的援用是或不是为null,如果不为空则会调用setDevice来成立叁个Connection,不然会倒闭这次连接。

在setDevice中,大家得以博得器材的效果与利益集(UsbInterface),也足以赢得通讯通道(UsbEndpoint),同不时间也创造了host与device的总是用来传输数据。

private void setDevice(UsbDevice device) { 
    if (device != null) { 
      UsbInterface intf = null; 
      UsbEndpoint ep = null; 

      int InterfaceCount = device.getInterfaceCount(); 
      int j; 

      mDevice = device; 
      for (j = 0; j < InterfaceCount; j++) { 
        int i; 

        intf = device.getInterface(j); 
        Log.i(TAG, "接口是:" + j + "类是:" + intf.getInterfaceClass()); 
        if (intf.getInterfaceClass() == 7) { 
          int UsbEndpointCount = intf.getEndpointCount(); 
          for (i = 0; i < UsbEndpointCount; i++) { 
            ep = intf.getEndpoint(i); 
            Log.i(TAG, "端点是:" + i + "方向是:" + ep.getDirection() + "类型是:" + ep.getType()); 
            if (ep.getDirection() == 0 && ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { 
              Log.i(TAG, "接口是:" + j + "端点是:" + i); 
              break; 
            } 
          } 
          if (i != UsbEndpointCount) { 
            break; 
          } 
        } 
      } 
      if (j == InterfaceCount) { 
        Log.i(TAG, "没有打印机接口"); 
        return; 
      } 

      mEndpointIntr = ep; 

        UsbDeviceConnection connection = mUsbManager.openDevice(device); 

        if (connection != null && connection.claimInterface(intf, true)) { 
          Log.i(TAG, "打开成功! "); 
          mConnection = connection; 

        } else { 
          Log.i(TAG, "打开失败! "); 
          mConnection = null; 
        } 
      } 

  } 

2.在连锁的类中新建多少个UsbAdmin,调用openUsb,这里首先是走了上边包车型客车setDevice(卡塔尔(英语:State of Qatar)方法,获取到了配备的引用,当连接通道建构即列出富有USB设备,当设备的引用不设不时同样列出装有的USB设备,並且都诉求获取USB权限。

public void openUsb() { 
    if (mDevice != null) { 
      setDevice(mDevice); 
      if (mConnection == null) { 
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); 
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 

        while (deviceIterator.hasNext()) { 
          UsbDevice device = deviceIterator.next(); 
          mUsbManager.requestPermission(device, mPermissionIntent); 
        } 
      } 
    } else { 
      HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList(); 
      Iterator<UsbDevice> deviceIterator = deviceList.values().iterator(); 

      while (deviceIterator.hasNext()) { 
        UsbDevice device = deviceIterator.next(); 
        mUsbManager.requestPermission(device, mPermissionIntent); 
      } 
    } 
  } 

3.当下面两部都走完了现在,大家就足以发送指令来支配已经创设连接的打字与印刷机了,这里大家采纳的是正经的ESC/POS指令集,为硬件暗中认可,贴出代码,这里的一声令下集应用的是十进制表示方式,也得以替换到十一进制。

public class printerCmdUtils { 

  /** 
   * 这些数据源自爱普生指令集,为POS机硬件默认 
   */ 

  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;//作废(页模式下取消打印数据 ) 



//------------------------打印机初始化----------------------------- 


  /** 
   * 打印机初始化 
   * @return 
   */ 
  public static byte[] init_printer() 
  { 
    byte[] result = new byte[2]; 
    result[0] = ESC; 
    result[1] = 64; 
    return result; 
  } 


//------------------------换行----------------------------- 


  /** 
   * 换行 
   * @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; 
  } 


//------------------------下划线-----------------------------   


  /** 
   * 绘制下划线(1点宽) 
   * @return 
   */ 
  public static byte[] underlineWithOneDotWidthOn() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 45; 
    result[2] = 1; 
    return result; 
  } 


  /** 
   * 绘制下划线(2点宽) 
   * @return 
   */ 
  public static byte[] underlineWithTwoDotWidthOn() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 45; 
    result[2] = 2; 
    return result; 
  } 
  /** 
   * 取消绘制下划线 
   * @return 
   */ 
  public static byte[] underlineOff() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 45; 
    result[2] = 0; 
    return result; 
  } 


//------------------------加粗----------------------------- 


  /** 
   * 选择加粗模式 
   * @return 
   */ 
  public static byte[] boldOn() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 69; 
    result[2] = 0xF; 
    return result; 
  } 


  /** 
   * 取消加粗模式 
   * @return 
   */ 
  public static byte[] boldOff() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 69; 
    result[2] = 0; 
    return result; 
  } 


//------------------------对齐----------------------------- 


  /** 
   * 左对齐 
   * @return 
   */ 
  public static byte[] alignLeft() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 97; 
    result[2] = 0; 
    return result; 
  } 


  /** 
   * 居中对齐 
   * @return 
   */ 
  public static byte[] alignCenter() 
  { 
      byte[] result = new byte[3]; 
    result[0] = ESC; 
    result[1] = 97; 
    result[2] = 1; 
    return 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; 
  } 
//------------------------字体变大----------------------------- 


  /** 
   * 字体变大为标准的n倍 
   * @param num 
   * @return 
   */ 
  public static byte[] fontSizeSetBig(int num) 
  { 
      byte realSize = 0; 
      switch (num) 
      { 
      case 1: 
        realSize = 0;break; 
      case 2: 
        realSize = 17;break; 
      case 3: 
        realSize = 34;break; 
      case 4: 
        realSize = 51;break; 
      case 5: 
        realSize = 68;break; 
      case 6: 
        realSize = 85;break; 
      case 7: 
        realSize = 102;break; 
      case 8: 
        realSize = 119;break; 
      } 
      byte[] result = new byte[3]; 
      result[0] = 29; 
      result[1] = 33; 
      result[2] = realSize; 
      return result; 
  } 


//------------------------字体变小----------------------------- 


  /** 
   * 字体取消倍宽倍高 
   * @param num 
   * @return 
   */ 
  public static byte[] fontSizeSetSmall(int num) 
  { 
      byte[] result = new byte[3]; 
      result[0] = ESC; 
      result[1] = 33; 

    return result; 
  } 


//------------------------切纸-----------------------------   


  /** 
   * 进纸并全部切割 
   * @return 
   */ 
  public static byte[] feedPaperCutAll() 
  { 
      byte[] result = new byte[4]; 
     result[0] = GS; 
     result[1] = 86; 
     result[2] = 65; 
     result[3] = 0; 
     return result; 
  } 


  /** 
   * 进纸并切割(左边留一点不切) 
   * @return 
   */ 
  public static byte[] feedPaperCutPartial() 
  { 
      byte[] result = new byte[4]; 
     result[0] = GS; 
     result[1] = 86; 
     result[2] = 66; 
     result[3] = 0; 
     return result; 
  } 

//------------------------切纸----------------------------- 
  public static byte[] byteMerger(byte[] byte_1, byte[] byte_2){  
    byte[] byte_3 = new byte[byte_1.length+byte_2.length];  
    System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);  
    System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);  
    return byte_3;  
  }  


  public static byte[] byteMerger(byte[][] byteList){  

      int length = 0; 
    for(int i=0;i<byteList.length;i++) 
    { 
        length += byteList[i].length; 
    } 
    byte[] result = new byte[length]; 

    int index = 0; 
    for(int i=0;i<byteList.length;i++) 
    { 
        byte[] nowByte = byteList[i]; 
        for(int k=0;k<byteList[i].length;k++) 
        { 
          result[index] = nowByte[k]; 
          index++; 
        } 
    } 
    return result;  
  }  



} 

4.在上述都做到今后,就能够把你要求的字符串转变到byte数组并调用sendCommand方法来举办打字与印刷了

@SuppressLint("NewApi") 
  public boolean sendCommand(byte[] Content) { 
    boolean Result; 
    synchronized (this) { 
      int len = -1; 
      if (mConnection != null) { 
        len = mConnection.bulkTransfer(mEndpointIntr, Content, Content.length, 10000); 
      } 

      if (len < 0) { 
        Result = false; 
        Log.i(TAG, "发送失败! " + len); 
      } else { 
        Result = true; 
        Log.i(TAG, "发送" + len + "字节数据"); 
      } 
    } 
    return Result; 

复制代码 代码如下:

len = mConnection.bulkTransfer(mEndpointIntr, Content, Content.length,
10000); 

这一步仅仅加了合伙锁,并未开启多少个新的线程去管理,在本机上未曾难题,但上边的USB通讯机制的篇章有提到要放松权利异步线程,这里必要专一。

以上就是本文的全体内容,希望对大家的学习抱有助于,也意在大家多多指教脚本之家。

你也许感兴趣的随笔:

  • Android Bluetooth连接 ESC/POS
    热敏打字与印刷机打字与印刷实例(ESC/POS指令篇)
  • Android Bluetooth连接 ESC/POS
    热敏打字与印刷机打字与印刷实例(蓝牙( Bluetooth® 卡塔尔(قطر‎连接篇)
  • Android进级——安卓调用ESC/POS打字与印刷机打字与印刷实例
admin

网站地图xml地图