A cross-platform serial port data collection tool that supports multiple ports simultaneously.
- Multi-port concurrent data collection
- Configurable port parameters via JSON
- Real-time status display with color indicators
- Automatic data file management
- Cross-platform support (Windows/Linux)
- TCP forwarding
SerialPortCollector/
├── main.cpp # Main program entry
├── SerialPort.h # Serial port class declaration
├── SerialPort.cpp # Serial port implementation
├── Config.h # Configuration class declaration
├── Config.cpp # Configuration implementation
├── TcpClient.h # TCP client class declaration
├── TcpClient.cpp # TCP client implementation
├── Common.h # Common definitions
├── Logger.h # Logger class
├── CMakeLists.txt # CMake build configuration
├── changelog.txt # Version changelog
├── README.md # English documentation
├── README_CN.md # Chinese documentation
├── data/ # Data storage directory (auto-created)
├── error/ # Error log directory (auto-created)
└── config.json # Port configuration file
- Program entry point
- Multi-threaded data collection
- Status monitoring and display
- Data storage management
- Serial port communication implementation
- Cross-platform serial operations
- Windows and Linux support
- Configuration file management
- JSON configuration parsing
- Default configuration generation
- CMake project configuration
- Cross-platform build support
- Dependency management
struct PortConfig {
std::string name; // Port name
int baudRate; // Baud rate
int dataBits; // Data bits
int stopBits; // Stop bits
std::string parity; // Parity mode
bool addTimestamp; // Enable timestamp
int timeout; // Timeout in seconds
bool enabled; // Enable TCP forwarding
std::string server; // TCP server address
int port; // TCP server port
int reconnectInterval; // Reconnection interval in seconds
};
- Handles port open, close, and read operations
- Cross-platform implementation
- Error handling and status management
- Configuration file reading and parsing
- Default configuration generation
- Configuration validation and error handling
- Visual Studio 2019/2022
- CMake 3.10+
- vcpkg package manager
- Windows SDK 10.0+
- GCC 8.0+
- CMake 3.10+
- Serial port development library
- nlohmann-json: JSON parsing library
- Install required tools:
# Install vcpkg
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat
.\vcpkg integrate install
- Install dependencies
.\vcpkg install nlohmann-json:x64-windows
- Build project:
cd SerialPortCollector/CPP
mkdir build
cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=[vcpkg root]/scripts/buildsystems/vcpkg.cmake
cmake --build . --config Release
- Install dependencies:
sudo apt-get install build-essential cmake
sudo apt-get install nlohmann-json3-dev
- Build project:
cd SerialPortCollector/CPP
mkdir build
cd build
cmake ..
make
{
"ports": [
{
"name": "COM1",
"baudRate": 9600,
"dataBits": 8,
"stopBits": 1,
"parity": "none",
"addTimestamp": true,
"timeout": 60,
"enabled": true,
"server": "127.0.0.1",
"port": 12345,
"reconnectInterval": 30
}
]
}
- name: Port name (Windows: COM1, Linux: /dev/ttyUSB0)
- baudRate: Baud rate (common values: 9600, 115200)
- dataBits: Data bits (typically 8)
- stopBits: Stop bits (1 or 2)
- parity: Parity check ("none", "odd", "even")
- addTimestamp: Enable timestamp in data
- timeout: Data timeout threshold in seconds
- enabled: Enable TCP forwarding (true/false)
- server: TCP server address
- port: TCP server port
- reconnectInterval: Reconnection interval in seconds
- Green: Actively receiving data
- Yellow: Waiting for data
- Red: No data received (timeout exceeded)
- Port number
- Port name
- Baud rate
- Current status
data/
├── COM1/
│ ├── 20240118.data
│ └── 20240119.data
└── COM2/
├── 20240118.data
└── 20240119.data
- Filename: YYYYMMDD.data
- Data format: [timestamp] data content (if timestamp enabled)
-
Port Open Failure
- Verify port name
- Check if port is in use
- Verify user permissions
-
Data Reception Issues
- Check baud rate settings
- Verify port parameters
- Check hardware connection
-
Build Errors
- Verify all dependencies are installed
- Check CMake configuration
- Verify compiler version
This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0).
You are free to use, modify, and distribute this work, provided that any modifications or derivatives are also distributed under the AGPL-3.0 license, ensuring they remain open-source.
For more details, see the LICENSE file or visit GNU AGPL-3.0.
Luo Qi
- GitHub: @keirosang
-
Create Shortcut
- Locate the compiled program (SerialPortCollector.exe)
- Right-click to create shortcut
- Right-click the shortcut, select "Properties"
- Fill in the "Start in" field with the full path to the program directory
- Or modify "Target" by adding
cd /d
command, for example:cmd /c "cd /d C:\Program Files\SerialPortCollector && SerialPortCollector.exe"
-
Add to Startup Folder (ensure correct working directory)
- Press Win + R, type
shell:startup
to open startup folder - Copy the configured shortcut to the startup folder
- Press Win + R, type
-
Use Task Scheduler (Recommended)
- Open Task Scheduler (search for "Task Scheduler")
- Create Basic Task
- Set name and description (e.g., "SerialPortCollector")
- Choose trigger "When computer starts"
- Action select "Start a program"
- Browse to select program path
- Important: Fill in "Start in (optional)" with the full path to program directory
- Optional: Set "Run with highest privileges"
- Use Systemd Service (Recommended)
- Create service file:
sudo nano /etc/systemd/system/serialportcollector.service
- Add following content:
[Unit]
Description=Serial Port Collector Service
After=network.target
[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/program/directory
ExecStart=/path/to/program/SerialPortCollector
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
- Enable service:
sudo systemctl daemon-reload
sudo systemctl enable serialportcollector
sudo systemctl start serialportcollector
- Use rc.local (for older systems)
- Edit rc.local file:
sudo nano /etc/rc.local
- Add before
exit 0
:
/path/to/program/SerialPortCollector &
- Ensure rc.local is executable:
sudo chmod +x /etc/rc.local
- Use Desktop Environment Autostart (GUI environment)
- Create .desktop file:
nano ~/.config/autostart/serialportcollector.desktop
- Add following content:
[Desktop Entry]
Type=Application
Name=SerialPortCollector
Exec=/path/to/program/SerialPortCollector
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
-
Windows Notes:
- Ensure program path contains no Chinese characters
- Recommended to run with administrator privileges
- Can set restart on failure in Task Scheduler
- Working Directory Setup:
- Program needs correct working directory to find config file
- Use one of these methods to ensure correct working directory:
- Set "Start in" in shortcut properties
- Use batch file to start program:
@echo off cd /d "%~dp0" start SerialPortCollector.exe
- Modify program code to use program directory as config path
- Recommended to keep config file in same directory as program
-
Linux Notes:
- Ensure program has execution permissions
- Check user permissions for serial devices
- View systemd service logs:
sudo systemctl status serialportcollector
sudo journalctl -u serialportcollector
- General Notes:
- Use absolute paths when possible
- Ensure correct config file location
- Consider adding error logging
- Ensure data directory has write permissions
- Added TCP data forwarding feature
- Added error logging system
- Improved status display mechanism
- Optimized performance and stability
- Enhanced error handling
- Initial release
- Multi-port data collection
- Real-time status display
- Data file management