Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Console app for serial connection.
* `COM[N]` is mandatory to specify serial port
4. Operate target device via the console
5. Press F1 to leave its serial session and to finish SimpleCom
* Press CTRL+C in batch mode

> [!IMPORTANT]
> You have to use batch mode (`--batch`) if you want to redirect something into SimpleCom. Batch mode would not terminate automatically when stdin reaches EOF. So you have to type CTRL+C if you want terminate SimpleCom.

## Command line options

Expand All @@ -38,6 +42,7 @@ Console app for serial connection.
| `--auto-reconnect-timeout [num]` | 120 | Reconnect timeout |
| `--log-file [logfile]` | <none> | Log serial communication to file |
| `--stdin-logging` | false | Enable stdin logging<br><br>⚠️Possible to be logged each chars duplicately due to echo back from the console when this option is set, and also secrets (e.g. passphrase) typed into the console will be logged even if it is not shown on the console. |
| `--batch` | false | Perform in batch mode<br><br>⚠️You have to set serial port in command line arguments, and you cannot set with `--show-dialog`, `--tty-resizer`, `--auto-reconnect`, `--log-file`. |
| `--help` | - | Show help message |

# How to build
Expand Down Expand Up @@ -69,7 +74,8 @@ Please see [Applications installed from the web](https://docs.microsoft.com/ja-j
# Notes

* SimpleCom sends / receives VT100 escape sequences. So the serial device to connect via SimpleCom needs to support VT100 or compatible shell.
* F1 key is hooked by SimpleCom, so escase sequence of F1 (`ESC O P`) would not be propagated.
* F1 key is hooked by SimpleCom (in interactive mode (default)), so escape sequence of F1 (`ESC O P`) would not be propagated.
* In batch mode, F1 would propergate to peripheral.
* SimpleCom supports ANSI chars only, so it would not work if multibyte chars (e.g. CJK chars) are given.
* Run [resize](https://linux.die.net/man/1/resize) provided by xterm if you want to align VT size of Linux box with your console window.

Expand Down
74 changes: 74 additions & 0 deletions SimpleCom/BatchRedirector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2025, Yasumasa Suenaga
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "stdafx.h"
#include "BatchRedirector.h"

static constexpr int buf_sz = 256;

static void RedirectHandle(HANDLE hSource, HANDLE hDest) {
char buf[buf_sz];
DWORD nBytesRead;
while (ReadFile(hSource, buf, sizeof(buf), &nBytesRead, NULL)) {
DWORD nBytesWritten;
DWORD nBytesRemain = nBytesRead;
while (nBytesRemain > 0) {
if (WriteFile(hDest, &buf[nBytesRead - nBytesRemain], nBytesRemain, &nBytesWritten, nullptr)) {
nBytesRemain -= nBytesWritten;
}
}
}
}

DWORD WINAPI BatchStdInRedirector(_In_ LPVOID lpParameter) {
HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE);
if (hStdIn == INVALID_HANDLE_VALUE) {
std::cerr << "Error: Could not get STDIN handle." << std::endl;
return -1;
}

// Disable line input mode to read raw input from console. (for batch mode without file redirection)
DWORD mode;
GetConsoleMode(hStdIn, &mode);
mode &= ~ENABLE_LINE_INPUT;
SetConsoleMode(hStdIn, mode);

HANDLE hSerial = reinterpret_cast<HANDLE>(lpParameter);
RedirectHandle(hStdIn, hSerial);
return 0;
}

DWORD WINAPI BatchStdOutRedirector(_In_ LPVOID lpParameter) {
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdOut == INVALID_HANDLE_VALUE) {
std::cerr << "Error: Could not get STDOUT handle." << std::endl;
return -1;
}

HANDLE hSerial = reinterpret_cast<HANDLE>(lpParameter);
RedirectHandle(hSerial, hStdOut);
return 0;
}

std::tuple<LPTHREAD_START_ROUTINE, LPVOID> SimpleCom::BatchRedirector::GetStdInRedirector() {
return { &BatchStdInRedirector, _hSerial };
}

std::tuple<LPTHREAD_START_ROUTINE, LPVOID> SimpleCom::BatchRedirector::GetStdOutRedirector() {
return { &BatchStdOutRedirector, _hSerial };
}
38 changes: 38 additions & 0 deletions SimpleCom/BatchRedirector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2025, Yasumasa Suenaga
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#pragma once
#include "stdafx.h"
#include "TerminalRedirectorBase.h"

namespace SimpleCom {

class BatchRedirector :
public TerminalRedirectorBase
{
protected:
virtual std::tuple<LPTHREAD_START_ROUTINE, LPVOID> GetStdInRedirector() override;
virtual std::tuple<LPTHREAD_START_ROUTINE, LPVOID> GetStdOutRedirector() override;

public:
BatchRedirector(HANDLE hSerial) : TerminalRedirectorBase(hSerial) {};
virtual ~BatchRedirector() {};
};

}

Loading