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.
| Link | What the application sees | Driver |
|---|---|---|
| USB CDC ACM | A serial port | Built into current Windows, macOS and Linux |
| USB HID | A HID device sending reports | Built in |
| USB vendor-specific | Raw endpoints | WinUSB on Windows; libusb or the OS USB API elsewhere |
| USB to UART bridge | A serial port | Bridge vendor driver, sometimes built in |
| Ethernet or Wi-Fi | An address and port | None |
| bytes/s | |
|---|---|
| 9600 baud | 960 |
| 57600 baud | 5760 |
| 115200 baud | 11520 |
| 460800 baud | 46080 |
| 921600 baud | 92160 |
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.