Star TSP热敏打印机transferOut跨PC报错:指定端点不在已声明备选接口
解决Star TSP打印机WebUSB transferOut端点错误问题
问题根源
报错提示指定端点不属于已声明并选中的备用接口,核心问题有两点:
- 硬编码配置编号、接口索引和端点号,不同PC上设备枚举的接口配置可能存在差异
- 未显式激活接口的备用设置,部分USB设备必须选中对应备用设置才能启用端点
修复方案
- 动态获取设备配置:避免硬编码配置编号,优先使用设备默认配置或验证后选择
- 显式激活备用接口:声明接口后必须选中对应备用设置,确保端点处于可用状态
- 自动识别输出端点:遍历接口端点,匹配打印机所需的批量输出类型,摒弃硬编码端点号
修改后的代码
$(document) .on("click", ".request-device", async function () { try { const device = await navigator.usb.requestDevice({ filters: [{ vendorId: 0x0519 }] }); await device.open(); // 优先使用设备默认配置,而非固定配置1 const configuration = device.configuration || await device.selectConfiguration(1); // 获取目标接口(可根据设备文档调整索引) const targetInterface = configuration.interfaces[0]; const interfaceNumber = targetInterface.interfaceNumber; await device.claimInterface(interfaceNumber); // 激活接口的备用设置(Star TSP通常使用备用设置0,可参考设备文档调整) const alternateSetting = targetInterface.alternates[0].alternateSetting; await device.selectAlternateInterface(interfaceNumber, alternateSetting); // 查找批量输出端点 const outEndpoint = targetInterface.alternates[alternateSetting].endpoints.find( endpoint => endpoint.direction === 'out' && endpoint.type === 'bulk' ); if (!outEndpoint) { throw new Error("未找到打印机的批量输出端点"); } var string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."; string += "\x1B\x64\x02";//切纸指令 var encoder = new TextEncoder(); var data = encoder.encode(string); await device.transferOut(outEndpoint.endpointNumber, data) .catch(error => { console.log(error); }); } catch (error) { alert('错误: ' + error.message); } });
额外排查建议
- 可添加日志打印设备配置、接口和端点信息,对比两台PC的差异:
console.log("设备配置详情:", configuration); console.log("目标接口信息:", targetInterface); console.log("匹配到的输出端点:", outEndpoint); - 查阅Star TSP打印机官方文档,确认接口的备用设置编号和端点类型,确保代码与设备规格匹配
内容的提问来源于stack exchange,提问作者Spitfire5793
相关产品推荐
相关产品推荐

