NetBurner 3.5.6
PDF Version
fdprintf - printf to file descriptor

fdprintf Example Application

Overview

This application demonstrates the use of file descriptors for serial port communication using the NetBurner RTOS platform. It showcases how to close, open, and print to file descriptors, specifically focusing on serial port operations with formatted output.

Note
While this example uses a serial file descriptor, fdprintf() can be used with any file descriptor, such as dynamic web page output. A serial port is used here for simplicity, but the web examples make extensive use of fdprintf().

Functionality

The application performs the following operations:

  1. Serial Port Initialization: Configures and opens serial ports for data communication and debug output
  2. File Descriptor Management: Demonstrates proper handling of file descriptors for serial communication
  3. Formatted Output: Uses fdprintf() to send formatted data to serial ports
  4. Network Integration: Initializes HTTP server and waits for network connectivity
  5. Continuous Operation: Runs in a loop, outputting timestamp data every second

Configuration

Serial Port Settings

  • Data Serial Port: Port 0
  • Debug Serial Port: Port 0 (shared with data port in this example)
  • Baud Rate: 115200 bps
  • Data Bits: 8
  • Stop Bits: 1
  • Parity: None

Network Settings

  • Network Timeout: 5 seconds wait for active network connection
  • HTTP Server: Enabled

Key Features

File Descriptor Operations

  • Opens serial ports using OpenSerial()
  • Closes existing serial connections with SerialClose()
  • Manages separate file descriptors for data and debug output
  • Replaces standard I/O streams (stdin, stdout, stderr) with custom file descriptors

Output Functionality

  • Sends formatted timestamp data every second
  • Uses fdprintf() for formatted output to specific file descriptors
  • Outputs current system time in seconds with carriage return and line feed

System Integration

  • Initializes NetBurner system components
  • Enables system diagnostics (recommended to disable for production)
  • Integrates with HTTP server functionality
  • Waits for network connectivity before main operation

Code Structure

Main Components

  1. Global Variables:
    • fddebug: File descriptor for debug output
    • fdserial: File descriptor for serial data I/O
  2. InitializeSerialPorts():
    • Configures serial port settings
    • Opens communication channels
    • Redirects standard I/O streams
  3. UserMain():
    • Main application entry point
    • Initializes system components
    • Runs continuous operation loop

Usage

The application automatically:

  1. Initializes the NetBurner system
  2. Starts HTTP server
  3. Waits for network connectivity
  4. Configures serial ports
  5. Begins continuous timestamp output

Output Format

The application outputs timestamps in the following format:

Secs: [timestamp]

Where [timestamp] is the current system time in seconds since startup.

Development Notes

  • System diagnostics are enabled by default (consider disabling for production)
  • Both data and debug ports use the same serial port (Port 0) in this configuration
  • The application demonstrates proper file descriptor management for embedded systems
  • Standard I/O streams are redirected to custom file descriptors for better control