Last active
December 23, 2018 02:23
-
-
Save karronoli/1f698e47eee88e8d72fabd11dca4a287 to your computer and use it in GitHub Desktop.
npm install @types/w3c-web-usb && tsc -t ES2015 webusb.ts # source: https://qiita.com/karronoli/items/49eb179b9be6d5268950
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | |
return new (P || (P = Promise))(function (resolve, reject) { | |
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | |
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | |
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | |
step((generator = generator.apply(thisArg, _arguments || [])).next()); | |
}); | |
}; | |
// webusb.ts | |
var StandardCode; | |
(function (StandardCode) { | |
StandardCode[StandardCode["ESC"] = 27] = "ESC"; | |
StandardCode[StandardCode["STX"] = 2] = "STX"; | |
StandardCode[StandardCode["ETX"] = 3] = "ETX"; | |
})(StandardCode || (StandardCode = {})); | |
// MB400のベンダIDとプロダクトID https://www.satoamerica.com/products/mobile-printers/mbi-series.aspx | |
// このプリンタは 203dpi なのでメートルに直すと 8dot/mm | |
const VID = 0x0828; | |
const PID = 0x0025; | |
// 各文字のアスキーコード | |
const A = 'A'.charCodeAt(0); | |
const B = 'B'.charCodeAt(0); | |
const G = 'G'.charCodeAt(0); | |
const H = 'H'.charCodeAt(0); | |
const I = 'I'.charCodeAt(0); | |
const Q = 'Q'.charCodeAt(0); | |
const V = 'V'.charCodeAt(0); | |
const Z = 'Z'.charCodeAt(0); | |
const ZERO = '0'.charCodeAt(0); | |
const ONE = '1'.charCodeAt(0); | |
const TWO = '2'.charCodeAt(0); | |
const FOUR = '4'.charCodeAt(0); | |
const SIX = '6'.charCodeAt(0); | |
const EIGHT = '8'.charCodeAt(0); | |
let main = function () { | |
return __awaiter(this, void 0, void 0, function* () { | |
// プリンタを探してUSBの書き込み用のエンドポイントにアクセス | |
const device = yield navigator.usb.requestDevice({ filters: [{ vendorId: VID, productId: PID }] }); | |
yield device.open(); | |
yield device.claimInterface(0); | |
return yield device.transferOut(1, new Uint8Array([ | |
// 印刷命令開始 | |
StandardCode.STX, StandardCode.ESC, A, | |
// 用紙のサイズは 縦400(dot)/8(dot/mm) = 50(mm)、横680(dot)/8(dot/mm) = 85(mm) | |
StandardCode.ESC, A, ONE, ZERO, FOUR, ZERO, ZERO, ZERO, SIX, EIGHT, ZERO, | |
// ラベルの境界はリフレクションセンサで判定する | |
StandardCode.ESC, I, G, ZERO, | |
// 横80(dot)/8(dot/mm) = 10(mm) に印字位置を移動 | |
StandardCode.ESC, H, ZERO, ZERO, EIGHT, ZERO, | |
// 縦80(dot)/8(dot/mm) = 10(mm) に印字位置を移動 | |
StandardCode.ESC, V, ZERO, ZERO, EIGHT, ZERO, | |
// CODE128で細い線を2(dot) * 1/8(mm/dot) = 0.25mm、高さ80(dot) * 1/8(mm/dot) = 10mm で 018 を印字 | |
StandardCode.ESC, B, G, ZERO, TWO, ZERO, EIGHT, ZERO, ZERO, ONE, EIGHT, | |
// 印刷枚数を1枚 | |
StandardCode.ESC, Q, ZERO, ZERO, ZERO, ONE, | |
// 印刷命令終了 = プリンタからラベルが出てくる | |
StandardCode.ESC, Z, StandardCode.ETX | |
])); | |
}); | |
}; | |
console.log(main()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// webusb.ts | |
enum StandardCode { | |
ESC = 0x1b, | |
STX = 0x02, | |
ETX = 0x03, | |
} | |
// MB400のベンダIDとプロダクトID https://www.satoamerica.com/products/mobile-printers/mbi-series.aspx | |
// このプリンタは 203dpi なのでメートルに直すと 8dot/mm | |
const VID = 0x0828; | |
const PID = 0x0025; | |
// 各文字のアスキーコード | |
const A = 'A'.charCodeAt(0); | |
const B = 'B'.charCodeAt(0); | |
const G = 'G'.charCodeAt(0); | |
const H = 'H'.charCodeAt(0); | |
const I = 'I'.charCodeAt(0); | |
const Q = 'Q'.charCodeAt(0); | |
const V = 'V'.charCodeAt(0); | |
const Z = 'Z'.charCodeAt(0); | |
const ZERO = '0'.charCodeAt(0); | |
const ONE = '1'.charCodeAt(0); | |
const TWO = '2'.charCodeAt(0); | |
const FOUR = '4'.charCodeAt(0); | |
const SIX = '6'.charCodeAt(0); | |
const EIGHT = '8'.charCodeAt(0); | |
let main = async function () { | |
// プリンタを探してUSBの書き込み用のエンドポイントにアクセス | |
const device = await navigator.usb.requestDevice( | |
{ filters: [{ vendorId: VID, productId: PID }] }); | |
await device.open(); | |
await device.claimInterface(0); | |
return await device.transferOut(1, new Uint8Array([ | |
// 印刷命令開始 | |
StandardCode.STX, StandardCode.ESC, A, | |
// 用紙のサイズは 縦400(dot)/8(dot/mm) = 50(mm)、横680(dot)/8(dot/mm) = 85(mm) | |
StandardCode.ESC, A, ONE, ZERO, FOUR, ZERO, ZERO, ZERO, SIX, EIGHT, ZERO, | |
// ラベルの境界はリフレクションセンサで判定する | |
StandardCode.ESC, I, G, ZERO, | |
// 横80(dot)/8(dot/mm) = 10(mm) に印字位置を移動 | |
StandardCode.ESC, H, ZERO, ZERO, EIGHT, ZERO, | |
// 縦80(dot)/8(dot/mm) = 10(mm) に印字位置を移動 | |
StandardCode.ESC, V, ZERO, ZERO, EIGHT, ZERO, | |
// CODE128で細い線を2(dot) * 1/8(mm/dot) = 0.25mm、高さ80(dot) * 1/8(mm/dot) = 10mm で 018 を印字 | |
StandardCode.ESC, B, G, ZERO, TWO, ZERO, EIGHT, ZERO, ZERO, ONE, EIGHT, | |
// 印刷枚数を1枚 | |
StandardCode.ESC, Q, ZERO, ZERO, ZERO, ONE, | |
// 印刷命令終了 = プリンタからラベルが出てくる | |
StandardCode.ESC, Z, StandardCode.ETX])); | |
}; | |
console.log(main()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment