DualTCP2Serial Example
A NetBurner embedded application that bridges TCP network connections to serial ports, enabling remote access to serial devices over Ethernet.
Overview
This application creates TCP-to-serial bridges on two serial ports simultaneously, allowing network clients to communicate with serial devices as if they were directly connected. Each serial port listens on a different TCP port and handles one client connection at a time.
Features
- Dual Serial Port Support: Bridges both serial port 0 and serial port 1
- Bidirectional Data Transfer: Data flows seamlessly between TCP clients and serial devices
- Connection Override: New clients can override existing connections after a timeout period
- Configurable Parameters: Easily adjust baud rates, TCP ports, and timeout values
- Error Handling: Robust handling of network and serial port errors
Configuration
Default Settings
| Parameter | Port 0 | Port 1 |
| TCP Port | 2000 | 2001 |
| Baud Rate | 115200 | 115200 |
| Data Bits | 8 | 8 |
| Stop Bits | 1 | 1 |
| Parity | None | None |
| Override Timeout | 10 seconds | 10 seconds |
Customization
Modify these constants in main.cpp to change the configuration:
#define BAUDRATE_TO_USE (115200)
#define STOP_BITS (1)
#define DATA_BITS (8)
#define TCP_PORT0_TO_USE (2000)
#define TCP_PORT1_TO_USE (2001)
#define OVERRIDE_TIMEOUT (TICKS_PER_SECOND * 10)
How It Works
Connection Process
- Startup: Application initializes both serial ports and begins listening on configured TCP ports
- Client Connection: When a TCP client connects, a dedicated bridge is established
- Data Transfer: All data received from the network is sent to the serial port, and vice versa
- Connection Management: Only one client per serial port at a time
Override Feature
The override timeout prevents stale connections from blocking new clients:
- When a client connects, the listen socket is closed to prevent additional connections
- After the timeout period expires, a new listen socket opens
- If a new client attempts to connect, the old connection is automatically closed
- This handles cases where clients crash or disconnect improperly
Usage
Network Client Connection
Connect to the NetBurner device using any TCP client:
# Connect to serial port 0
telnet <netburner_ip> 2000
# Connect to serial port 1
telnet <netburner_ip> 2001
Example Use Cases
- Remote Serial Console: Access device debug output over network
- Serial Device Control: Control industrial equipment remotely
- Data Logging: Collect serial data from remote sensors
- Protocol Bridging: Convert between network and serial protocols
Architecture
Task Structure
- UserMain Task: Handles serial port 0 and TCP port 2000
- SerialTask2: Handles serial port 1 and TCP port 2001 (lower priority)
Data Flow
TCP Client <-> Network Stack <-> SerialToEthernetFunction <-> Serial Port <-> Connected Device
Technical Details
Buffer Management
- Uses 80-byte buffers for data transfer
- Non-blocking I/O with select() for efficient multiplexing
- Immediate data forwarding (no buffering delays)
Error Handling
- Automatic socket cleanup on network errors
- Graceful handling of serial port errors
- Connection state monitoring and recovery
Performance Considerations
- SerialTask2 runs at lower priority than UserMain
- 2-second select timeout for responsive operation
- Efficient file descriptor monitoring
Building and Deployment
- Requirements: NetBurner development environment and hardware
- Compilation: Use standard NetBurner build tools
- Deployment: Flash to NetBurner device
- Network Setup: Ensure device has network connectivity
Troubleshooting
Common Issues
No Connection**: Verify network connectivity and TCP port accessibility Data Loss**: Check baud rate and serial settings match connected device Stale Connections**: Override timeout will eventually clear blocked connections Performance Issues**: Consider adjusting buffer sizes for high-throughput applications
Debug Output
The application provides console output showing:
- Listen socket creation
- Client connections and disconnections
- Override timeout events
- Socket file descriptor information