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:
- Serial Port Initialization: Configures and opens serial ports for data communication and debug output
- File Descriptor Management: Demonstrates proper handling of file descriptors for serial communication
- Formatted Output: Uses
fdprintf()
to send formatted data to serial ports
- Network Integration: Initializes HTTP server and waits for network connectivity
- 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
- Global Variables:
fddebug
: File descriptor for debug output
fdserial
: File descriptor for serial data I/O
- InitializeSerialPorts():
- Configures serial port settings
- Opens communication channels
- Redirects standard I/O streams
- UserMain():
- Main application entry point
- Initializes system components
- Runs continuous operation loop
Usage
The application automatically:
- Initializes the NetBurner system
- Starts HTTP server
- Waits for network connectivity
- Configures serial ports
- Begins continuous timestamp output
Output Format
The application outputs timestamps in the following format:
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