连接 TM-U220 时出错

Mic*_*wan 5 html javascript printing epson

我已经下载了2个支持TM-U220的Epson打印机的javascript SDK,但它们都无法连接到我的打印机并且无法打印。但是当我尝试使用其他 SDK(例如 QZ Tray)时,它可以工作,但是 QZ Tray 必须打开,我希望它在 Android 中工作,所以我使用 Epson 的 javascript SDK。

问题是当我使用 Epson 在 javascript SDK 中提供的打印机示例并输入 ip: 192.168.1.98、端口: 9100 和设备 id: local_printer 时,出现以下错误:

连接到 ePOS 设备服务接口失败。[错误超时]

。
但是打印机打印了一个小字,我看不懂,并且在打印的最后:2http/1.1。在控制台中:

选项https://192.168.1.98/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000 net::ERR_CONNECTION_REFUSED


和

选项https://192.168.1.98/cgi-bin/eposDisp/service.cgi?devid=local_display&timeout=10000 net::ERR_CONNECTION_REFUSED


我还创建了自己的简单代码。这是我使用epos-2.3.0.js的第一个代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Print Test</title>

        <script type="text/javascript" src="epos-2.3.0.js"></script>

        <script type="text/javascript">
            // Retrieving Printer objects (printer selection)
            var printer = null;
            // Retrieving Printer objects (printer selection)

            // Creating ePOSDevice objects (device connection and communication)
            var ePosDev = new epson.ePOSDevice();

            function connect() {
                var ipAddress = '192.168.1.98'; var port = '9100';
                ePosDev.connect(ipAddress, port, callback_connect);
            }
            // Creating ePOSDevice objects (device connection and communication)

            // Retrieving Printer objects (printer selection)
            function callback_connect(resultConnect) {
                var deviceId = 'local_printer';
                var options = {'crypto' : false, 'buffer' : false};

                if ((resultConnect == 'OK') || (resultConnect == 'SSL_CONNECT_OK')) {
                    // Retrieves the printer object
                    alert("Success callback connect");
                    ePostDev.createDevice(deviceId, ePosDev.DEVICE_TYPE_PRINTER, options, callback_createDevice);
                }
                else {
                    // Displays error messages
                    alert("Error callback connect");
                }
            }

            function callback_createDevice(deviceObj, errorCode) {
                if (deviceObj === null) {
                    // Displays an error message if the system fails to retreive the printer object
                    return;
                }
                printer = deviceObj;

                // Registers the print complete event
                printer.onreceive = function(response) {
                    if (response.success) {
                        // Displays the successful print message
                        alert("Callback create device response success");
                    }
                    else {
                        // Displays error messages
                        alert("Callback create device response failed");
                    }
                }
            }
            // Retrieving Printer objects (printer selection)

            // Creating print data (data buffering)
            function createData() {
                printer.addTextAlign(printer.ALIGN_CENTER);
                printer.addText('Hello World\n');
            }
            // Creating print data (data buffering)

            // Sending print data (printing and disconnection)
            function send() {
                if (ePosDev.isConnected) {
                    printer.send();
                }
            }
            // Sending print data (printing and disconnection)
        </script>
    </head>
    <body>
        <input type="button" onclick="connect()" value="Connect" />
        <input type="button" onClick="send()" value="Print Hello World" />
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)


我正在使用epos-print-3.2.0.js:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Print Test 2</title>
        <script type="text/javascript" src="epos-print-3.2.0.js"></script>
        <script type="text/javascript">
            function buildMessage() {
                // Create a print document
                var builder = new epson.ePOSBuilder();
                builder.addTextLang('en');
                builder.addTextSmooth(true);
                builder.addTextFont(builder.FONT_A);
                builder.addTextSize(3, 3);
                builder.addText('Hello,\tWorld!\n');
                builder.addCut(builder.CUT_FEED);
                var request = builder.toString();

                var address = 'http://192.168.1.98/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000';

                // Create an ePOS-Print object
                var epos = new epson.ePOSPrint(address);
                //Send the print document
                epos.send(request);
            }
        </script>
    </head>
    <body>
        <button onclick="buildMessage()">Run</button> 
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

当我运行第二个代码时,我在控制台中收到此错误:

选项http://192.168.1.98/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000


和

XMLHttpRequest 无法加载http://192.168.1.98/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“null”。响应的 HTTP 状态代码为 405。


但是当我将地址从
http://192.168.1.98/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000
更改为
http://192.168.1.98:9100/cgi-bin/epos/service.cgi ?devid=local_printer&timeout=10000
它向我打印了这个:

选项 /cgi-bin/epos/service.cgi?devid=local-printer&timeout=10000 HTTP/1.1
主机:192.168.1.98:9100
连接:保持活动 访问控制请求方法:POST
来源:空
用户代理: Mizilla/5.0(Windows NT 6.3;WOW64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/52.0.2743.116 Safari/537.36
Access-Control-Request-Headers:内容类型、if-modified-since、soapaction
接受:*/ *
接受编码:gzip、deflate、sdch
接受=语言:en-US、en;q=0.8

Jim*_*dra 1

从表面上看,这是一个跨源 HTTP 请求问题 (CORS)。这篇 MDN 文章对此进行了解释:https ://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

我猜测IP 92.168.1.98是打印机IP(位于本地网络上)。请参阅:http ://trendblog.net/ever-wondered-use-192-168-xx-ip-addresses-home/ ,您可以通过端口 9100 访问它,如上面的帖子中所述。

因此,由于您的实际 Web 应用程序驻留在与打印机 IP 和 Web 工作方式不同的 IP 上,因此当您调用不同的 IP/主机时需要 CORS,以防止跨站点脚本攻击。

常识是,如果打印机通过端口 9100 暴露自身,您应该将其作为 URI 的一部分。这就是为什么http://192.168.1.98:9100/cgi-bin/epos/service.cgi?devid=local_printer&timeout=10000 有效而另一个无效的原因。

至于尝试通过 Android 设备访问此内容...问题是该设备是否已加入您的本地网络 (192.168....) 还是在互联网上?如果它连接到互联网,我认为您无法访问可能不会暴露在互联网上的打印机(具有公共IP)。只要它们属于同一网络,它们就应该能够通话。如果没有,您需要将其公开到互联网(坏主意)或确保它们可以互相看到(Android 连接到您公司的 WIFI,并且可以通过 WIFI 访问打印机)。