| Crates.io | virtualport |
| lib.rs | virtualport |
| version | 0.1.3 |
| created_at | 2025-02-07 06:17:04.12581+00 |
| updated_at | 2025-02-07 06:17:04.12581+00 |
| description | A program to create a virtual serial port (PTY) with extended functionality. |
| homepage | |
| repository | https://github.com/s00d/virtualport |
| max_upload_size | |
| id | 1546552 |
| size | 43,741 |
A Rust-based tool to create virtual serial ports using pseudo-terminals (PTYs) with advanced features like heartbeat messages, logging, and real-time configuration.
/tmp/my_virtual_port)./baud commands./parity commands.git clone https://github.com/s00d/virtualport.git
cd virtualport
cargo build --release
# Run with verbose output
sudo cargo run -- --verbose
# In another terminal, read from the virtual port
sudo cat /tmp/my_virtual_port
# Send data to the virtual port
echo "Hello World" > /tmp/my_virtual_port
USAGE:
virtualport [OPTIONS] --link <LINK>
OPTIONS:
-l, --link <LINK> Symlink path for the virtual port [default: /tmp/my_virtual_port]
-v, --verbose Enable verbose logging
--enable-echo Keep echo enabled on the slave device
--init-msg <INIT_MSG> Initial message to send on startup
--log-file <LOG_FILE> Path to log file (e.g., serial.log)
--heartbeat <HEARTBEAT> Heartbeat interval in seconds (0 = disabled)
--hb-msg <HB_MSG> Custom heartbeat message [default: HEARTBEAT\n]
Heartbeat and Logging:
sudo cargo run -- \
--verbose \
--log-file serial.log \
--heartbeat 5 \
--hb-msg "PING\n"
Initial Message and Custom Baud Rate:
sudo cargo run -- \
--init-msg "INIT" \
--enable-echo
# In the program's console:
/baud 9600
Interact Programmatically:
# Send commands from shell
echo "/parity even" > /tmp/my_virtual_port
openpty to create a PTY pair.[Received] messages.graph LR
A[User Input/Console] -->|stdin| B[Stdin Writer Thread]
B -->|writes to| C[Master PTY]
C -->|read by| D[Master Reader Thread]
D -->|logs/output| E[Console: Received ...]
F[Heartbeat Thread] -->|periodic writes| C
C <-->|PTY Pair| G[Slave PTY]
G -->|symlink| H["/tmp/my_virtual_port"]
H -->|read/write| I[External Tools e.g., `cat`, `echo`]
I -->|writes data| H
G -->|read by| J[Slave Reader Thread]
J -->|writes back to| C
Stdin Writer Thread, which writes it to the Master PTY.Master Reader Thread and displayed as [Received] ....Heartbeat Thread periodically writes messages (e.g., HEARTBEAT)./tmp/my_virtual_port).echo, cat) interact with the slave via the symlink.echo) is forwarded to the master and displayed in the console.commands.txtThe program supports automated command-response handling through a file named commands.txt. This file allows you to predefine command-response pairs that the emulator will use to process incoming commands during runtime.
File Structure: The file is expected to contain alternating lines: the first line of each pair is a command, and the second line is its corresponding response. For example, if the emulator receives the command matching one of these entries, it will automatically send back the associated response.
Location: Place commands.txt in the same directory from which you launch the emulator.
Loading Commands: Upon startup, the emulator reads commands.txt and loads all command-response pairs into memory. If the file is not found, the program will issue a warning and continue running without predefined commands.
Usage in Communication:
\n) is received from the master PTY, it is checked against the loaded commands. If a match is found, the program responds with the predefined response from the file.commands.txt:AT
OK
AT+CSQ
+CSQ: 23,99
AT+CREG?
+CREG: 0,1
Permission Denied:
sudo to create PTY devices and symlinks./tmp has write permissions.Data Not Visible in cat:
--enable-echo to loopback data.Garbage Output:
Heartbeat Not Working:
--heartbeat is greater than 0.--verbose to debug.MIT License. Contributions welcome!