无法使用WebUSB API连接usb打印机

问题描述 投票:1回答:1

我有DATECS DPP-250 POS打印机,我正在尝试使用WebUSB API连接它并打印一些数据。我遇到的问题是打印不起作用。我可以连接到设备和chrome读取它很好,但当我提交打印(transferOut)打印机只是挂起。我试图在控制台中调试错误,但我没有任何错误。我尝试使用MAC OS和Windows 10上的WinUSB驱动程序(使用Zadig来更改Windows上的驱动程序),所有这些都有同样的问题。

有谁知道问题出在哪里?

编辑:

我试过其他POS打印机(非蓝牙一体机),我的代码完美无缺。唯一的问题是这台打印机。这种类型的打印机使用micro usb,这可能是一个问题?

这是我正在使用的代码

<html>
<body>
    <textarea id="printContent"></textarea>
    <input type="submit" onclick="connectAndPrint()" value="Print"/>
    <P>Type text into box and click on submit button.
    <script>
    var device;

    function setup(device) {
        return device.open()
        .then(() => device.selectConfiguration(1))
        .then(() => device.claimInterface(device.configuration.interfaces[0].interfaceNumber))
    }

    function print() {
        var string = document.getElementById("printContent").value + "\n";
        var encoder = new TextEncoder();
        var data = encoder.encode(string);
        console.log(data.length);
        device.transferOut(device.configuration.interfaces[0].alternate.endpoints[0].endpointNumber, data)
        .catch(error => { console.warn(error); })
    }

    function connectAndPrint() {
        console.log(device);
        if (device == null) {
            navigator.usb.requestDevice({ filters: [{ vendorId: 5380 }, { vendorId: 65520 }] })
            .then(selectedDevice => {
                device = selectedDevice;
                console.log(device.configuration);
                return setup(device);
            })
            .then(() => print())
            .catch(error => { console.log(error); })
        }
        else
            print();
    }

    navigator.usb.getDevices()
    .then(devices => {
        if (devices.length > 0) {
            device = devices[0];
            return setup(device);
        }
    })
    .catch(error => { console.log(error); });

    </script>
</body>
</html>
usb webusb
1个回答
0
投票

您需要在服务器上运行它,例如:xampp或node js。我以前用单个html尝试它不起作用,但我放在服务器上它会工作

© www.soinside.com 2019 - 2024. All rights reserved.