UART Examples (Blocking API)
Overview
The blocking UART examples are available for both C and C++:
C:
examples/blocking_api/uart/c/C++:
examples/blocking_api/uart/cpp/
These examples are intended for both Supernova and Pulsar USB host adapters.
Hardware Setup
Required hardware:
One Binho Supernova or Pulsar
One Binho Breakout Board
Required wiring:
Interconnect TX and RX on the breakout board to close the hardware loopback path.
Otherwise, connect another UART device to the breakout board for a real-world test. In this case, the user is in charge of configuring the external device to match the UART settings used in the example.
What The UART Examples Do
Create the SDK instance/handle.
Register a UART receive notification callback at the beginning of the program.
Connect to the adapter and retrieve device information.
Initialize UART with
115200-8N1and sendHello, world!.Receive loopback data through the registered callback.
Reconfigure UART to
9600-8N1and send the same message again.Receive loopback data again through the callback.
Notification Callback Requirement
UART receive data is delivered through asynchronous notifications. For this reason, the client application must register a callback before UART traffic begins:
C API:
CosmicSDK_onNotification, then parse UART notifications withCosmicSDK_notificationAsUartRx.C++ API:
CosmicSDK::onNotificationand parse usingnotification.uartRx().
Synchronization Mechanism
The examples use callback-driven synchronization instead of fixed sleeps. After each UART transmission, the main flow waits until the callback signals that loopback data has been received (with a timeout to avoid blocking forever).
C example: platform synchronization primitives (Windows condition variable or POSIX
pthreadcondition variable) plus a notification counter.C++ example:
std::mutex+std::condition_variableplus a notification counter.
Build and Run (UART C/C++)
UART example directories build binaries with the following names:
- C (
examples/blocking_api/uart/c/): example_c_dynamicexample_c_static
- C (
- C++ (
examples/blocking_api/uart/cpp/): example_cpp_dynamicexample_cpp_static
- C++ (
See also: