Talking to Hardware From Applications: Serial, USB and Network Links

An application that configures, monitors or updates a device spends much of its complexity on the link. The operating system delivers bytes; it does not deliver messages, detect a half-written frame or notice that someone has unplugged the cable. Whatever the link, the application needs a protocol layer of its own, and that layer decides whether the connection is dependable.

At a glance

CDC ACM
The USB class that makes a device appear as a serial port without a custom driver.
HID
USB human interface devices use drivers built into every major desktop operating system.
8N1
The common UART format: eight data bits, no parity and one stop bit, so ten bits per byte.
CRC
A checksum on every frame catches corrupted messages before they are acted on.

The problem

Serial ports and TCP sockets are byte streams. A read can return half a message or two messages joined together, and code that assumes one read equals one message works on the bench and fails under load or on a slower machine.

Disconnection is the other common failure. USB devices are unplugged, reset during firmware updates and re-enumerate under a different port name, and an application that cannot detect and recover from that leaves users restarting it.

The operating system gives you bytes. Framing, checking and recovering are the application's job, and they belong in one well-tested layer.

Choosing the link

USB CDC ACM is the simplest route for a microcontroller device: it appears as a COM port on Windows, a /dev/ttyACM device on Linux and a /dev/cu device on macOS. HID suits small, periodic reports and needs no driver installation. A vendor-specific interface used through WinUSB or libusb gives the most control and throughput.

Network links suit devices on Ethernet or Wi-Fi. TCP gives ordered, reliable delivery but is still a stream; UDP preserves message boundaries but can lose or reorder packets.

What good looks like

Define the message format first: a length field or delimiter, a message type, the payload and a CRC. COBS or SLIP encoding gives clean frame boundaries on a byte stream.

Put the link behind one interface in the application, with explicit timeouts, retries and a disconnected state, so the rest of the code never touches the port directly.

Test against real faults: unplug mid-transfer, reset the device, flood the link and run on the slowest supported machine.

How a device appears to the application
LinkWhat the application seesDriver
USB CDC ACMA serial portBuilt into current Windows, macOS and Linux
USB HIDA HID device sending reportsBuilt in
USB vendor-specificRaw endpointsWinUSB on Windows; libusb or the OS USB API elsewhere
USB to UART bridgeA serial portBridge vendor driver, sometimes built in
Ethernet or Wi-FiAn address and portNone
Common links between an application and a device it controls.
UART throughput at common baud rates
bytes/s
9600 baud960
57600 baud5760
115200 baud11520
460800 baud46080
921600 baud92160
With 8N1 framing each byte takes ten bits, so bytes per second is the baud rate divided by ten. Logarithmic scale.

What it changes

Robustness

Framed, checked messages survive partial reads, noise and buffering differences between machines.

Recovery

Hot-plug detection and reconnection turn a cable pull into a short pause, not a crash.

Portability

One link abstraction lets the same protocol run over USB, serial or the network.

Testability

A simulated device behind the same interface lets the application be tested without hardware.

Where to go next

Include a protocol version in the first exchange. Applications and firmware are updated on different schedules, and each side needs to know what the other supports.

Where throughput matters, measure it on the target machines rather than relying on the nominal speed of the link.

Problems this prevents

  • Messages split or merged by a read call
  • An application that hangs when the cable is pulled
  • Firmware and app protocols drifting apart
  • Code that can only be tested with hardware attached

Discuss your project

Email support@aavico.com or use Get in Touch when the website finishes loading.