---
title: "Recipe: a serial port, a HID device or a USB device"
description: "@exact/serial, @exact/hid and @exact/usb are Web Serial, WebHID and WebUSB (LLP 1133.037)."
modificationDate: "2026-11-09"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: a serial port, a HID device or a USB device

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/usb-hid-serial: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

`@exact/serial`, `@exact/hid` and `@exact/usb` are Web Serial, WebHID and WebUSB (LLP 1133.037). Contract lists and
opens devices; `app.ts` sends and reads the bytes, as base64 strings. Grant `device.serial`, `device.hid` or
`device.usb`, with no purpose.

Where to find the device:

```prompt Connect a serial, HID or USB device
Make the Exact app in this folder list and open a serial port, a HID device or a USB device. Use https://github.com/expo/exact/blob/main/docs/recipes/usb-hid-serial.md as the source for each step. The Exact checkout has the same page at docs/recipes/usb-hid-serial.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me. Find the Exact checkout (the folder with scripts/exact.mjs); if you cannot find it, ask me where it is.
2. If I did not say which kind of device (serial, HID or USB) and how to know it (its vendor and product, and for serial the baud rate), ask me before you write code.
3. Install the package: `bun <exact2>/scripts/exact.mjs add serial .` (or `hid`, or `usb`). Grant `device.serial`, `device.hid` or `device.usb`, with no purpose.
4. In app.ts, choose the list from the status, as the recipe shows: `attachedPorts()` or `attachedDevices(filters=…)` when `attachedList` is true, else `getPorts()` or `getDevices()`. A list that fails at launch stops the app, so never call the wrong one. Send and read the bytes in app.ts, as base64 strings.
5. In app.contract, show a menu of the attached devices when `attachedList` is true, else a button that opens the browser's chooser. Disable Connect until a device is chosen. Where `available` is false (iOS and tvOS), hide the feature.
6. In app.test.contract, add a test that Connect is disabled until a device is chosen.
7. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 8) and report it; do not keep trying.
8. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
9. Tell me the device kind, what the tests and verify reported, and how to drive it by hand with a fake device: `bun exact.mjs agent web 'prefer extension serial.ports=[{"id":"s3","input":[]}]' 'tap @chooser s3' state` (`hid.devices` or `usb.devices` for the others); the writes are in `state.extensions.serial.writes`. On Linux, a device with `granted` false needs a udev rule.
```

- **macOS, Windows, Linux, Android:** `attachedPorts()` or `attachedDevices(filters=…)` lists what is plugged in now
  (`status.attachedList` is true). Show it as a menu, or pick the device by vendor and product.
- **The web (Chromium):** a press sends `requestPort()` or `requestDevice(filters=…)`, the browser's chooser; the
  devices chosen before are `getPorts()` or `getDevices()`.
- **iOS and tvOS:** `status.available` is false. Hide the feature.

Each list fails `NotSupportedError` where it does not exist, and a resource that fails at launch stops the app, so
choose between them in `app.ts`, where the status says which this target has:

```ts
import { attachedPorts, getPorts } from '@exact/serial';

export const portList = (attachedList: boolean) => attachedList ? attachedPorts({}) : getPorts({});
```

```text
use serialStatus, requestPort, open as openPort, SerialStatus, SerialPortRecord, SerialOpened from "@exact/serial"

component Monitor
  state port = ""
  resource serial = serialStatus() as shape SerialStatus else empty()
  resource ports = portList(serial.attachedList) as shape SerialPortRecord[] else empty()
  mutation chosen as shape SerialPortRecord
  mutation opened as shape SerialOpened
  action pick(id: string)
    port = id
  action choose
    send chosen = requestPort()
  action connect
    send opened = openPort(port=port, baudRate=115200)
  view
    column
      if serial.attachedList
        select value=port change=pick aria-label="Port" testId="port"
          option "Choose a Port" value=""
          for p in ports key=p.id
            option p.name value=p.id
      else
        button press=choose testId="choose-port"
          text "Choose Port…"
      button press=connect disabled=(port == "") testId="connect"
        text "Connect"
```

`serialInput({connection})` answers every byte since its last answer, errors at their offsets and `done: true`
at the end; decode with `TextDecoder`'s `stream: true` (`apps/bench/serial.ts`). HID's `inputReports` is a batch;
`apps/bench/dfu.ts` is a DFU updater over USB control transfers.

On Linux a record's `granted` false means the node's mode refuses this user: add a udev rule for the device.

Under the agent a device is data: `prefer extension serial.ports=[{"id":"s3","input":[{"at":0,"data":"<base64>"}]}]`
(`hid.devices`, `usb.devices`), a chooser is `tap @chooser s3`, and writes are in `state.extensions.serial.writes`.
