---
title: "USB: Two serial ports"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/cdc_acm_dual/
last_updated: 2026-11-26
sdk_version: 4.44
---
# USB: Two serial ports

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/usb/cdc_acm_dual/

This sample has two serial ports over USB: the console on the first, given to it by the sample’s `roles.yml`, and an echo of what the host sends on the second, for two minutes. Every 10 s the console tells whether the second port is open.

Connect the board’s USB device port to a computer, which then has two new serial ports, as `/dev/cu.usbmodem*` on macOS or `/dev/ttyACM*` on Linux. The console keeps what it prints until a terminal opens the first port, the boot banner included, and then sends it.

Run:

`pbl samples samples/subsys/usb/cdc_acm_dual`

Boards:

every board

## Sources

roles.yml

```
console\_uart:symbol: USB\_CDC\_ACM\_UART(0)include: pbl/usb/cdc\_acm.h
```

src/main.c

```
#include \<stdint.h\>#include \<stdio.h\>#include \<pbl/kernel/msgq.h\>#include \<pbl/kernel/time.h\>#include \<pbl/usb/cdc\_acm.h\>#define RUN\_S 120#define TICK\_S 10#define QUEUE\_LEN 2048/\* The receive callback runs in the controller's interrupt: it queues, the main thread echoes. \*/static PBL\_MSGQ\_DEFINE(s\_rx, sizeof(uint8\_t), QUEUE\_LEN);static void prv\_rx(const struct pbl\_uart \*uart, uint8\_t c, void \*arg) {(void)pbl\_msgq\_put(&s\_rx, &c, PBL\_NO\_WAIT);}int main(void) {uint64\_t start = pbl\_uptime\_ms();uint64\_t tick = start;unsigned int count = 0U;uint8\_t c;int ret;ret = pbl\_uart\_rx\_enable(USB\_CDC\_ACM\_UART(1), prv\_rx, nullptr);if (ret != 0) {printf("usb: serial port 1 failed (%d)\n", ret);return ret;}printf("usb: console on port 0, echo on port 1 for %d s\n", RUN\_S);while (pbl\_uptime\_ms() - start \< RUN\_S \* 1000U) {if (pbl\_uptime\_ms() \>= tick) {printf("usb: %u s, port 1 %s, %u bytes echoed\n", (unsigned int)((tick - start) / 1000U),pbl\_usb\_cdc\_acm\_dtr(USB\_CDC\_ACM\_UART(1)) ? "open" : "closed", count);tick += TICK\_S \* 1000U;}if (pbl\_msgq\_get(&s\_rx, &c, PBL\_MSEC(100)) != 0) {continue;}pbl\_uart\_poll\_out(USB\_CDC\_ACM\_UART(1), c);count++;}printf("usb: echoed %u bytes\n", count);printf("Sample done\n");return 0;}
```

## Output

```
usb: console on port 0, echo on port 1
```

Generated from PebbleOS [9fa910d13](https://github.com/coredevices/pebbleos/commit/9fa910d13ea42053dff926754a25c68090e963dc). This page is maintained in the pebbleos repository: [docs/samples/subsys/usb/cdc\_acm\_dual.md](https://github.com/coredevices/pebbleos/blob/9fa910d13ea42053dff926754a25c68090e963dc/docs/samples/subsys/usb/cdc_acm_dual.md).
