【问题标题】:Epson js SDK unable to use multiple printersEpson js SDK 无法使用多台打印机
【发布时间】:2019-10-22 01:03:36
【问题描述】:

简介
我们正在开发这个基于 javascript 的 Web 应用程序,它应该使用 epson javascript sdk 打印收据。

现在我们已经有了这个 poc,可以将多台打印机添加到应用程序中,并且可以为每台打印机打印收据。

问题是收据只能是最后添加的打印机的打印机。

进一步调查告诉我们,sdk 只使用最后添加(连接)的打印机。这可以在以下图片中看到。

  1. 在第一个图像中有 2 台打印机设置。注意不同的 IP 地址。
  2. 在第二张图片中,我们记录了打印时使用的 EpsonPrinter 实例。请注意,IP 地址显然是第一台打印机。
  3. 在第三张图片中,我们跟踪网络。注意实际使用的ip地址(忽略错误)。

我们创建了自己的 EpsonPrinter 类,可以在 here 或下方找到。

EpsonPrinter

export default class EpsonPrinter {
  name = null
  ipAddress = null
  port = null
  deviceId = null
  crypto = false
  buffer = false
  eposdev = null
  printer = null
  intervalID = null
  restry = 0

  constructor (props) {
    const {
      name = 'Epson printer',
      ipAddress,
      port = 8008,
      deviceId = 'local_printer',
      crypto = false,
      buffer = false
    } = props
    this.name = name
    this.ipAddress = ipAddress
    this.port = port
    this.deviceId = deviceId
    this.crypto = crypto
    this.buffer = buffer

    this.eposdev = new window.epson.ePOSDevice()
    this.eposdev.onreconnecting = this.onReconnecting
    this.eposdev.onreconnect = this.onReconnect
    this.eposdev.ondisconnect = this.onDisconnect

    this.connect()
  }

  onReconnecting = () => {
    this.consoleLog('reconnecting')
  }
  onReconnect = () => {
    this.consoleLog('reconnect')
  }

  onDisconnect = () => {
    this.consoleLog('disconnect')

    if (this.intervalID === null ){
      this.intervalID = setInterval(() => this.reconnect(), 5000)
    }
  }

  connect = () => {
    this.consoleLog('connect')

    this.eposdev.ondisconnect = null
    this.eposdev.disconnect()

    this.eposdev.connect(this.ipAddress, this.port, this.connectCallback)
  }

  reconnect = () => {
    this.consoleLog('(Re)connect')

    this.eposdev.connect(this.ipAddress, this.port, this.connectCallback)
  }

  connectCallback = (data) => {
    clearInterval(this.intervalID)
    this.intervalID = null
    this.eposdev.ondisconnect = this.onDisconnect

    if (data === 'OK' || data === 'SSL_CONNECT_OK') {
      this.createDevice()
    } else {
      setTimeout(() => this.reconnect(), 5000)
    }
  }

  createDevice = () => {
    console.log('create device, try: ' + this.restry)

    const options = {
      crypto: this.crypto,
      buffer: this.buffer
    }

    this.eposdev.createDevice(this.deviceId, this.eposdev.DEVICE_TYPE_PRINTER, options, this.createDeviceCallback)
  }

  createDeviceCallback = (deviceObj, code) => {
    this.restry++

    if (code === 'OK') {
      this.printer = deviceObj
      this.printer.onreceive = this.onReceive
    } else if (code === 'DEVICE_IN_USE') {
      if (this.restry < 5) {
        setTimeout(() => this.createDevice(), 3000)
      }
    }
  }

  onReceive = (response) => {
    this.consoleLog('on receive: ', response)
    let message = `Print ${this.name} ${response.success ? 'success' : 'failute'}\n`
    message += `Code: ${response.code}\n`
    message += `Status: \n`

    if (response.status === this.printer.ASB_NO_RESPONSE) { message += ' No printer response\n' }
    if (response.status === this.printer.ASB_PRINT_SUCCESS) { message += ' Print complete\n' }
    if (response.status === this.printer.ASB_DRAWER_KICK) { message += ' Status of the drawer kick number 3 connector pin = "H"\n' }
    if (response.status === this.printer.ASB_OFF_LINE)  {   message += ' Offline status\n' }
    if (response.status === this.printer.ASB_COVER_OPEN)    {   message += ' Cover is open\n' }
    if (response.status === this.printer.ASB_PAPER_FEED) {  message += ' Paper feed switch is feeding paper\n' }
    if (response.status === this.printer.ASB_WAIT_ON_LINE) {    message += '  Waiting for online recovery\n' }
    if (response.status === this.printer.ASB_PANEL_SWITCH) {    message += ' Panel switch is ON\n' }
    if (response.status === this.printer.ASB_MECHANICAL_ERR) {  message += ' Mechanical error generated\n' }
    if (response.status === this.printer.ASB_AUTOCUTTER_ERR) {  message += ' Auto cutter error generated\n' }
    if (response.status === this.printer.ASB_UNRECOVER_ERR) {   message += ' Unrecoverable error generated\n' }
    if (response.status === this.printer.ASB_AUTORECOVER_ERR) { message += ' Auto recovery error generated\n' }
    if (response.status === this.printer.ASB_RECEIPT_NEAR_END) {    message += ' No paper in the roll paper near end detector\n' }
    if (response.status === this.printer.ASB_RECEIPT_END) { message += ' No paper in the roll paper end detector\n' }
    if (response.status === this.printer.ASB_SPOOLER_IS_STOPPED) {  message += ' Stop the spooler\n' }

    if (!response.success) {
      alert(message)
      // TODO: error message?
    } else {
      // TODO: success -> remove from queue
    }
  }

  printReceipt = () => {
    this.consoleLog(`Print receipt, `, this)
    try {
      if (!this.printer) {
        throw `No printer created for ${this.name}`
      }

      this.printer.addPulse(this.printer.DRAWER_1, this.printer.PULSE_100)

      this.printer.addText(`Printed from: ${this.name}\n`)

      this.printer.send()
    } catch (err) {
      let message = `Print ${this.name} failure\n`
      message += `Error: ${err}`

      alert(message)
    }
  }

  consoleLog = (...rest) => {
    console.log(`${this.name}: `, ...rest)
  }
}

Poc
可以在 here 找到完整的工作 poc。

爱普生javascript sdk
2.9.0

有没有人有使用 epson sdk 的经验?它应该能够同时支持多个连接吗?请告知使用。

【问题讨论】:

    标签: epson


    【解决方案1】:

    适用于寻找使用此 SDK 处理多台打印机的方法的用户。我们想出了以下解决方法:

    我们创建了一个单独的“打印机应用程序”,负责处理一个打印机连接并将其在线托管。然后,我们将此打印机应用程序“加载”到需要使用 iframe 进行多个连接的应用程序中。应用程序和打印机应用程序之间的通信是通过 window.PostMessage API 完成的,例如,使用正确的打印机连接初始化打印机并提供必须打印的数据。

    这需要一些努力,但它是我们能想到的处理多个连接的最稳定的解决方案。

    如果有人想出更好的方法,请告诉我!

    您可以查看我们的打印机应用程序here 以获取灵感(检查该应用程序,因为它并没有显示太多像那样访问它)。

    【讨论】:

      【解决方案2】:

      为了使用您的 EpsonPrinter 类,我还在您的类之后添加了 myPrinters 类:

      class myPrinters {
          printers = null;
          cantidad = 0;
      
          constructor() {
              console.log("Creo la coleccion de printers");
              this.printers = [];
          }
      
          inicializarConeccionImpresora(idImpresora, ip, puerto, _deviceId) {
              let ipAddress = ip;
              let port = puerto;
              let deviceId = _deviceId;
              console.log("Agrego una impresora");
              let myPrinter = new EpsonPrinter(ipAddress);
              myPrinter.port = port;
              myPrinter.deviceId = deviceId;
              myPrinter.id = idImpresora;
              console.log('Id impresora antes de connect es: ' + idImpresora);
              myPrinter.connect();
              this.printers[this.cantidad] = myPrinter;
              this.cantidad ++;   
          }
      
          imprimirPruebaJS(idImpresora) {
              let printer = null;
              let printerTemp = null
              for(var i = 0; i < this.printers.length; i++) {
                  printerTemp = this.printers[i];
                  if (printerTemp.id == idImpresora) {
                      printer = printerTemp.printer;
                  }
              }
      
              if (printer == null) {
                  console.log("La impresora no esta iniciada en clase myPrinters");
                  return;
              }
      
              printer.addText('Hola mundo texto normal\n');
              printer.addFeed();
              printer.addCut(printer.CUT_FEED);
          }
      }
      

      以这种方式调用 myPrinters 类:

      myEpsonPrinters = new myPrinters();
      
      myEpsonPrinters.inicializarConeccionImpresora(1, '192.168.0.51', 8008, 'local_printer');
      myEpsonPrinters.inicializarConeccionImpresora(2, '192.168.0.52', 8008, 'local_printer');
      
      myEpsonPrinters.imprimirPruebaJS(1)
      or
      myEpsonPrinters.imprimirPruebaJS(2)
      

      测试它并告诉我。 娟

      【讨论】:

      • 别忘了:进入webconfig,默认ipaddress用户epson,通过epson。转到配置 - 网络 -> 配置中的 ePos-Device,版本 2.70 并启用它。告诉我这是否有效。
      【解决方案3】:

      只需创建多个打印对象就可以了

      this.eposdev = [];
      
      let printersCnt = 3;
      
      let self = this;
      
      for(let i=1 ; i <= printersCnt ; i++){
      
          this.eposdev[i] = new window.epson.ePOSDevice()
      
          this.eposdev[i].onreconnecting = function (){
              this.consoleLog('reConnecting')
          }
      
          this.eposdev[i].onreconnect =  function (){
             this.consoleLog('onReconnect')
          }
      
          this.eposdev[i].ondisconnect = function (){
              this.consoleLog('onDisconnect')
          }    
      } 
      
      
      function connect(printerKey) => {
      
          this.consoleLog('connect')
      
          this.eposdev.ondisconnect = null
          this.eposdev.disconnect()
      
          this.eposdev.connect(self.ipAddress[printerKey], self.port[printerKey], function(){
      
              clearInterval(self.intervalID)
      
              self.intervalID = null
              self.eposdev[i].ondisconnect = self.ondisconnect
      
              if (data === 'OK' || data === 'SSL_CONNECT_OK') {
      
                  console.log('create device, try: ' + self.restry)
      
                  const options = {
                    crypto: self.crypto,
                    buffer: self.buffer
                  }
      
                  self.eposdev[printerKey].createDevice(self.deviceId, self.eposdev[printerKey].DEVICE_TYPE_PRINTER, options, function(deviceObj, code){
                      this.restry++
      
                      if (code === 'OK') {
                        self.printer[printerKey] = deviceObj
                        self.printer.onreceive = function(){
                            console.log("onreceive");
                        }
                      } else if (code === 'DEVICE_IN_USE') {
                        if (self.restry < 5) {
                          setTimeout(() => self.createDevice(printerKey), 3000)
                        }
                      })
                  }
      
      
              } else {
                setTimeout(() => self.reconnect(printerKey), 5000)
              }
      
          })
      } 
      

      【讨论】:

        【解决方案4】:

        Epson 表示,在 2.12.0 版本中,您可以添加多台打印机。

        【讨论】:

        • 爱普生在哪里说的?您能否提供链接或其他内容,并解释在 2.12.0 中成功添加多台打印机的过程是什么,如果不同的话?详细说明您的答案将使其对未来的读者更有用。
        • 如果您在此处下载 sdk (download.epson-biz.com/modules/pos/…),您可以在版本历史记录中的文件 readme.en.txt 中看到:版本 2.12.0 - 添加了 Web 浏览器版本支持 - Microsoft Edge 39 - 44 - Mozilla Firefox 50 - 66 - Google Chrome 54 - 74 - Safari 11 - 12 - iOS 11 - 12 中的 Safari - 添加了 TM 打印机支持 - TM-T20III - TM-T82III - 条码扫描器现在可以连接到 TM-T88VI。 - Connect API 可用于一个应用程序中的多个设备。
        • 我正在寻找解决方案,如果我找到了,我会写在这里。问候,胡安
        • 我搞定了。进入webconfig,ipaddress默认用户epson,通过epson。转到配置中的 ePos-Device 版本 2.70 并启用它。告诉我这是否有效。
        • 转到配置 - 网络 -> ePos-Device,我的来源与您的非常相似。告诉它是否有效。
        猜你喜欢
        • 2018-11-18
        • 2019-09-05
        • 2015-08-09
        • 2016-12-01
        • 2015-09-18
        • 1970-01-01
        • 2011-10-16
        • 2014-01-27
        • 2012-01-21
        相关资源
        最近更新 更多