VisualHMI USB HID 扫码枪:on_key 扫描码转 ASCII

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

USB Host + 定制固件,直接接 HID 条码扫描枪,扫码内容经 on_key(key) 回调进 Lua。工业仓储、产线追溯用得着。HMI&M&DX 系列 + USB Interface + 定制固件。

1. API:on_key(key)

key 为 16 ビット符号なし整数:低 8 位 = USB HID Usage ID(扫描码),高 8 位 = Shift status(非 0 表示 Shift 按下,区分大小写)。低 8 位 == 0x28(Enter)表示条码结束,此时送信ストリング·ストリング并クリア缓冲。

2. Lua:HID 扫描码转 ASCII

用查表把 Usage ID マッピング成文字(含 Shift 大写态),遇到 0x28 结束符写 LW2000:

_barCode = ""
usbScanGun = {
  [0x04] = {"a","A"}, [0x05] = {"b","B"}, ... [0x1D] = {"z","Z"},
  [0x1E] = {"1","!"}, [0x1F] = {"2","@"}, ... [0x27] = {"0",")"},
  [0x2C] = {" "," "}, [0x28] = {"n","n"}  -- Enter
}
function on_key(key)
  local usage = key & 0xFF
  local shift = (key >> 8) & 0xFF
  if usage == 0x28 then        -- 结束符
    set_string(VT_LW, 0x2000, _barCode)
    _barCode = ""
    return
  end
  local ch = usbScanGun[usage]
  if ch then
    _barCode = _barCode .. (shift ~= 0 and ch[2] or ch[1])
  end
end

VisualHMI USB HID 扫码枪:on_key 扫描码转 ASCIIFigure

扫码枪本质是键盘模拟:HID 上报的是扫描码ではない ASCII,得自己查表转。Shift ステータス在 key 高 8 位,大小写靠它区分。0x28 是回车结束符——收到就送信,别等タイムアウト。定制固件专属。

整理自 广州大彩科技 VisualHMI 开发ドキュメント(hmi-doc.gz-dc.com)其他技术チュートリアル「USB HID扫码枪」,著作権は大彩科技すべての。

Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.