簡體   English   中英

無法將USB打印機與WebUSB API連接

[英]Can't connect usb printer with WebUSB API

我有DATECS DPP-250 POS打印機,並且嘗試使用WebUSB API連接它並打印一些數據。 我的問題是打印無法正常工作。 我可以連接到設備,並且chrome可以正常讀取,但是當我提交打印( transferOut )時,打印機掛起了。 我嘗試在控制台中調試錯誤,但沒有任何錯誤。 我嘗試在Mac OS和Windows 10上使用WinUSB驅動程序(使用Zadig更改Windows上的驅動程序),所有這些都有相同的問題。

有人知道問題出在哪里嗎?

編輯:

我嘗試使用其他POS打印機(非藍牙打印機),並且代碼運行正常。 唯一的問題是這台打印機。 這種類型的打印機使用微型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>

您需要在服務器上運行它,例如使用:xampp或node js。 我以前嘗試過單個html它不起作用,但是我放在服務器上它將起作用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM