Barcode Scanner Emulator
USB HID vs Serial Barcode Scanners
A USB HID keyboard-wedge scanner types the decoded barcode into the focused application. A USB serial scanner exposes a serial interface, often as a virtual COM port, and the application has to open that port and read the bytes. USB is the cable and the bus. HID and serial are different ways of presenting the data.
Pick the interface the application can actually read. A checkout field that already accepts typing is a keyboard-wedge problem. A service that must receive scans while no text field is focused, or that must keep scanner bytes separate from the keyboard, is a serial problem. Many scanners offer only one of those modes. Some can switch. The manual for that model is the list of modes you can test.
The HID keyboard wedge guide explains the keystroke path in detail. How barcode scanners work covers capture and decode. This page compares the two host interfaces and the tests that belong to each.
What Is USB HID Keyboard Wedge Mode?
In USB HID keyboard mode the scanner enumerates as a keyboard. The operating system loads its normal keyboard driver. After a decode, the scanner sends the characters as keystrokes to whichever control has keyboard focus. A web form, a POS item field, or a desktop text box can accept that input without scanner-specific code.
Focus decides the destination. If the intended field is not focused, the characters land in another control or they are lost. The scanner does not click, and it does not move the pointer.
A configured suffix may follow the payload. Enter submits many single-field forms. Tab moves focus to the next control. Neither key is guaranteed. Suffix choices are covered in ENTER vs TAB suffixes.
Ordinary keyboard events do not include the scanner's vendor, serial number, or a flag that says the keys came from a scanner. Browsers that implement WebHID still refuse keyboard interfaces, so a page cannot open the wedge device and read raw HID reports.
What Is USB Serial Barcode Scanner Mode?
In USB serial mode the scanner does not type. It exposes a serial interface over USB. The operating system usually presents that interface as a virtual COM port: a COM name on Windows, a cu or tty device on macOS, or a ttyACM or ttyUSB device on Linux. The name is assigned by the driver and can change after the scanner is unplugged.
A virtual COM port is not a physical RS-232 port. RS-232 is an electrical interface, with its own connector and voltage levels, that a computer reads through a UART. USB serial uses a USB cable. The application still opens a serial port and reads bytes, but the signaling on the wire is USB. Some products are a real RS-232 scanner plus a USB-to-serial adapter. Others implement USB CDC and never speak RS-232 at all. Treat the port the operating system shows you as the integration point, and do not assume the electrical details match.
The application opens the port, reads the byte stream, and decides where a message starts and ends. A common frame is the barcode text followed by CR (0x0D), LF (0x0A), or CR then LF. That framing is a device setting, not a rule of USB. A prefix byte such as STX is also possible. The prefix and suffix guide separates those bytes from the Enter key a wedge scanner sends.
Baud rate, data bits, parity, and stop bits matter on a real UART and on many USB-to-serial adapters. A wrong rate produces garbage rather than a clean error. Some USB CDC virtual COM devices ignore the baud rate the host requests and still deliver bytes. There is no single setting that fits every virtual serial scanner. Use the rate and framing in that model's manual, then confirm them with a known barcode.
The process that holds the port open receives the data. Keyboard focus does not choose the destination. Another program that already has the port open will block a second open. Unplugging the scanner removes the port. Plugging it back in can create a new name, and the application has to open that port again.
HID vs Serial
| Feature | HID keyboard wedge | USB serial |
|---|---|---|
| Data delivery | Keyboard-style input | Serial byte stream |
| Input focus required | Yes. Keystrokes follow the focused control | No. Bytes go to the process that opened the port |
| Browser integration | Ordinary input and keyboard events | Web Serial where the browser supports it, or software outside the page |
| Application control | Limited to keyboard behavior, focus, and suffixes | The application reads and frames the stream |
| Device identification | Not available from ordinary keyboard events | The operating system can show a port name. Stable identity still depends on the driver |
| Configuration | Scanner settings, including suffix and keyboard layout | Scanner settings plus the host port settings the device actually uses |
| Typical use | Forms and POS item entry | Dedicated integrations that own the device |
These are the usual cases. A given model, operating system, and driver can differ. A Bluetooth scanner in HID keyboard mode follows the wedge column at the application layer. Bluetooth SPP follows the serial column.
How HID Scanners Work in Web Applications
A wedge scan updates the focused element. Printable characters fire keydown and then input. Enter and Tab fire keydown and do not insert a character into a normal text input. event.key is "Enter" or "Tab" for those keys, and a one-character string for a printable character. event.code names the physical key and can stay the same when a keyboard-layout mismatch changes event.key.
The listener below commits the field only when Enter arrives. A scanner with no Enter suffix leaves the text in the field.
const field = document.querySelector('#barcode')
if (!(field instanceof HTMLInputElement)) {
throw new Error('Barcode field was not found.')
}
field.addEventListener('keydown', (event) => {
if (event.key !== 'Enter') {
return
}
event.preventDefault()
const value = field.value.trim()
if (!value) {
return
}
console.log(value)
field.value = ''
})
Focus #barcode before the scan. preventDefault() keeps a parent form from submitting a second time. If the scanner sends Tab instead, read the value in that keydown and let the browser move focus. If it sends neither, submit the field from a button or another explicit action. A pause in typing is only a heuristic for the end of a scan.
Nothing in those events identifies the USB device. A person, a macro, or the emulator can produce the same sequence. Validation belongs on the payload. The browser workflow, including Tab and empty input, is in How to Test Barcode Scanners in Web Applications.
How Serial Scanners Work in Applications
A serial integration opens a port, reads bytes, splits them into messages, and handles the port disappearing. On the desktop, that is the operating system's serial API or a vendor library. In a browser, the only built-in option is the Web Serial API, and only in browsers that implement it.
Web Serial is available in Chromium-based browsers. Firefox and Safari do not provide it. The page must be a secure context, which means HTTPS or localhost. requestPort() has to run from a user gesture, and the user must choose the port in the browser prompt. A keyboard-wedge scanner does not appear in that list, because it is not a serial device. Web Serial also cannot see a serial port the operating system has not exposed.
Baud rate in the example is a starting point you replace with the manual's value. The loop treats CR or LF as the end of a message and consumes a CRLF pair as one terminator. If your scanner uses a different frame, this split is wrong even when the port is open.
<button id="open-serial" type="button">Open scanner port</button>
<p id="status" role="status"></p>
<script>
const button = document.querySelector('#open-serial')
const status = document.querySelector('#status')
function takeLine(buffer) {
const cr = buffer.indexOf('\r')
const lf = buffer.indexOf('\n')
const end = cr < 0 ? lf : lf < 0 ? cr : Math.min(cr, lf)
if (end < 0) {
return null
}
const line = buffer.slice(0, end).trim()
let rest = buffer.slice(end + 1)
if (buffer[end] === '\r' && rest.startsWith('\n')) {
rest = rest.slice(1)
}
return { line, rest }
}
button.addEventListener('click', async () => {
if (!('serial' in navigator)) {
status.textContent = 'This browser does not provide Web Serial.'
return
}
let port
try {
port = await navigator.serial.requestPort()
await port.open({ baudRate: 9600 })
} catch (error) {
status.textContent = 'The port was not opened.'
return
}
const reader = port.readable.getReader()
const decoder = new TextDecoder()
let buffer = ''
try {
while (true) {
const { value, done } = await reader.read()
if (done) {
break
}
buffer += decoder.decode(value, { stream: true })
let next = takeLine(buffer)
while (next) {
if (next.line) {
status.textContent = next.line
}
buffer = next.rest
next = takeLine(buffer)
}
}
} catch (error) {
status.textContent = 'The serial read stopped.'
} finally {
reader.releaseLock()
await port.close()
}
})
</script>
The read loop runs until the stream ends or the read fails. Unplugging the scanner typically rejects the read. releaseLock() and close() run in finally. Previously approved ports for this origin can be listed with navigator.serial.getPorts() without showing the prompt again. The first approval still requires the user.
A desktop application follows the same idea with the platform serial API: open, read, frame, and reopen after the device returns. This page does not invent vendor commands. If the scanner expects a proprietary protocol, that protocol is in the manufacturer's documentation.
Which Is Better: HID or Serial?
Neither mode is better for every product. Use the mode the scanner actually offers and the mode the application can read.
Choose HID when
- The application already accepts keyboard input in a focused field.
- You want a form or POS screen to work without device code.
- The workflow can guarantee focus before each scan.
- Enter or Tab matches how that form commits or advances.
Consider serial when
- Scans must arrive while focus is in a dialog, on a button, or nowhere.
- Scanner bytes must stay separate from typing.
- The application can open a port, frame messages, and recover when the device is unplugged.
- The host can grant permission to that port and no other process needs it at the same time.
A scanner that only enumerates as a keyboard cannot be switched to serial in software on the host. Confirm the modes in the manual before you design the integration.
Switching Between HID and Serial
Some scanners change mode when you scan a configuration barcode from that manufacturer's manual, or when you write a profile with the vendor's utility. Others are built as one interface and have nothing to switch.
There is no configuration barcode that changes every brand. A sequence printed for a different model can do nothing, or it can leave the scanner in an unexpected mode. After any change, check what the operating system shows: a keyboard, a COM port, or both. Then test the path your application uses. The configuration guide describes the usual setting groups. It does not replace the manual.
Testing a HID Scanner
- Connect the scanner and confirm it is in keyboard-wedge mode. A text editor should receive characters when a field in that editor is focused.
- Open the Barcode Input Tester.
- Click Start Test, or click the scan area. It turns blue when it is ready. The page does not take focus by itself.
- Scan a barcode whose payload you already know.
- Compare the captured value and the character count with that payload.
- Read the suffix: ENTER, TAB, or None. Enter and Tab are not part of the barcode string.
The tester records keyboard events the browser can see. It cannot read a serial port, and it cannot identify the USB device. When the average gap between characters is under 100 ms, it reports that timing. The timing is not a hardware identification. For each keydown, including key and code, use the Keyboard Event Tester. A broader QA pass is in How to Test Barcode Scanner Input.
Testing a Serial Scanner
The Barcode Input Tester does not open a COM port. A serial test needs a program that can open the port the scanner exposes.
- Confirm the scanner is in a serial mode and that the operating system lists a port when it is connected. Unplug it and check that the port disappears.
- Note the port name. It is operating-system specific and may change on the next plug-in.
- Open the port from a compatible application: a serial terminal, your desktop app, or the Web Serial example above in a Chromium browser on HTTPS or localhost.
- Set baud rate, data bits, parity, and stop bits when the manual specifies them. Skip invented values.
- Scan a known barcode.
- Compare the bytes or the decoded text with that payload.
- Check the framing. Look for a prefix, and for CR, LF, or CRLF after the data, only if that is what the manual configured.
If the port open fails because it is busy, close the other program that holds it. If the text is garbage, the line settings do not match the device. If the port never appears, the scanner may still be in keyboard mode, the driver may be missing, or the OS user may not be allowed to open serial devices.
Common Problems
The scanner works in a text editor and not in the target application. The device is typing. The target does not have focus, or a keydown listener is discarding the keys. Start with Scanner Works in Notepad but Not POS.
The scanner does not type into any text field. It may be in serial mode, powered off, or not reading the symbol. A serial-mode scanner will not type. Check for a COM port before you debug the field. Barcode Scanner Not Working separates a dead scanner from the wrong interface.
The serial port is missing. Keyboard mode does not create a COM port. A missing driver, a charge-only cable, or a scanner that has no serial mode produces the same symptom. Watch the OS device list while you plug the scanner in.
The serial port is already in use. Only one process can read it. Close the terminal or the other copy of the application, then open it again.
The communication settings are wrong. Mismatched baud, parity, or stop bits look like random characters. Set the values from the manual for that model, then scan a known code again.
ENTER or TAB never arrives. That is a wedge suffix setting. The input tester reports None. See Barcode Scanner Missing ENTER and the suffix guide linked above. A serial scanner does not send the Enter key. It may send CR or LF bytes instead.
Characters are wrong on a wedge scanner. The scanner's keyboard layout and the operating-system layout can disagree. Serial bytes are not remapped by that layout. See Barcode Scanner Wrong Keyboard Layout.
The browser cannot access the scanner. A wedge scanner will not appear in the Web Serial prompt. Firefox and Safari have no Web Serial API. A page that is not HTTPS or localhost cannot use it. The user can also dismiss the port prompt.
The application loses the scanner after it is unplugged. A wedge scanner returns as a keyboard, and the next scan still requires focus. A serial port is gone until the application opens the new port. The name may not match the previous one.
Integration Scenarios
Web applications
Keyboard wedge fits a focused input and ordinary DOM events. Serial access inside the page means Web Serial in a supporting browser, a secure context, and a user-selected port. A page that must support Firefox, Safari, and unattended kiosks cannot depend on Web Serial alone. A local helper can own the COM port and forward complete barcodes to the page. That helper is separate software. The page still should not expect a wedge test to exercise it.
POS systems
A POS that puts focus in the item field and commits on Enter is a wedge integration. A POS that must scan while a modal is open, or that must ignore the keyboard, is a serial integration. Mixing them produces scans in the wrong field or scans that never arrive. Match the scanner mode to the screen that is actually on duty.
Desktop applications
A desktop app can accept wedge input in whichever control is focused, or it can open the virtual COM port and keep a session for the life of the process. Port ownership, reconnect, and framing are part of that session. Focus is not.
QA automation
The Barcode Scanner Emulator types a value you choose into the focused application and can send ENTER, TAB, or no suffix. That covers keyboard-wedge regressions: focus, validation, and suffix handling. It does not create a virtual COM port, enumerate USB devices, or reproduce baud and framing. Serial tests still need a serial port and a reader, or a test double that writes the same byte framing your application parses.
Kiosk applications
A kiosk using wedge input has to keep the scan field focused across dialogs, screen savers, and error messages. One lost focus and the next scan hits a shortcut or nothing. A kiosk using serial can read the port without that focus, and it must hold permission to the device and reopen the port after power loss or a cable pull.
Where the Barcode Scanner Emulator Fits
The Barcode Scanner Emulator is a desktop app for Windows, macOS, and Linux. It reproduces the keyboard path a wedge scanner uses. You enter the text, choose a typing delay, and choose a suffix of none, ENTER, or TAB. A global shortcut types that text into the focused application.
Use it to develop and regress HID-style forms when the scanner is not on the desk. Pair it with the Barcode Input Tester to see the value, prefix keys, and suffix the browser received.
It does not emulate a USB serial device, a virtual COM port, USB enumeration, or an optical decode. Those behaviors still belong to hardware, or to a serial test double you control.