Skip to content

Commit abfe161

Browse files
committed
StdioSerial.h: add SERIAL_OUTPUT_FILENO macro to allow clobbering on the command line (see #92)
1 parent 37e8bd6 commit abfe161

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Unreleased
44
* Support sending output of `Serial` port to `STDERR`
55
(replaces [PR#92](https://github.com/bxparks/EpoxyDuino/pull/92).
6+
* Add `SERIAL_OUTPUT_FILENO` macro which can be overridden on the command
7+
line (e.g. `make EXTRA_CPPFLAGS='SERIAL_OUTPUT_FILENO=2'` to override the
8+
default output file descriptor of `Serial` at compile time.
69
* 1.6.0 (2024-07-25)
710
* Add `strncat_P()` to `pgmspace.h`.
811
* Add `ESP.restart()` and `ESP.getChipId()`. See

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ The disadvantages are:
109109
* [Enable Terminal Echo](#EnableTerminalEcho)
110110
* [Output to STDERR](#OutputToStderr)
111111
* [Multiple Serial Ports](#MultipleSerialPorts)
112+
* [Shell Redirection](#ShellRedirection)
112113
* [Libraries and Mocks](#LibrariesAndMocks)
113114
* [Inherently Compatible Libraries](#InherentlyCompatibleLibraries)
114115
* [Emulation Libraries](#EmulationLibraries)
@@ -1110,8 +1111,8 @@ void setup() {
11101111
#### Output to STDERR
11111112

11121113
By default, the `Serial` instance sends its output to the STDOUT (file
1113-
descriptor `STDOUT_FILENO`, i.e. 1). We can override that to send the output to
1114-
STDERR (file descriptor `STDERR_FILENO`) using the
1114+
descriptor `STDOUT_FILENO` which is always 1). We can override that to send the
1115+
output to STDERR (file descriptor `STDERR_FILENO`) using the
11151116
`StdioSerial::setOutputFileDescriptor(int fd)` method:
11161117

11171118
```C++
@@ -1120,6 +1121,20 @@ Serial.setOutputFileDescriptor(STDERR_FILENO);
11201121
Serial.println("This goes to STDERR");
11211122
```
11221123

1124+
Another way to override the output file descriptor of the `Serial` instance is
1125+
to override the `SERIAL_OUTPUT_FILENO` macro on the command line during
1126+
compiling. The compiler `c++` or `g++` allows the `-D` flag like this:
1127+
1128+
```
1129+
$ c++ -D SERIAL_OUTPUT_FILENO=2 file.cpp ...
1130+
```
1131+
1132+
When using `make`, the flag can be passed into the compiler like this:
1133+
1134+
```
1135+
$ make EXTRA_CPPFLAGS='-D SERIAL_OUTPUT_FILENO=2'
1136+
```
1137+
11231138
<a name="MultipleSerialPorts"></a>
11241139
#### Multiple Serial Ports
11251140

@@ -1145,6 +1160,39 @@ void someFunction() {
11451160
}
11461161
```
11471162

1163+
See [examples/StdioSerialMultiple](examples/StdioSerialMultiple) for details.
1164+
1165+
<a name="ShellRedirection"></a>
1166+
#### Shell Redirection
1167+
1168+
This probably a good place to remind Unix users that shell redirection is
1169+
available on specific file descriptors using the `N>` syntax. Suppose we change
1170+
the above example to use 3 and 4 instead, like this:
1171+
1172+
```C++
1173+
#ifdef EPOXY_DUINO
1174+
StdioSerial Serial1(3);
1175+
StdioSerial Serial2(4);
1176+
#endif
1177+
...
1178+
void someFunction() {
1179+
Serial1.println("Print to 3");
1180+
Serial2.println("Print to 4");
1181+
...
1182+
}
1183+
```
1184+
1185+
We can run the executable in the `bash(1)` shell like this:
1186+
1187+
```
1188+
$ ./StdioSerialMultiple.out 3> stream3.txt 4> stream4.txt
1189+
```
1190+
1191+
We then get 2 files:
1192+
1193+
- `stream3.txt` contains the string "Print to 3", and
1194+
- `stream4.txt` contains the string "Print to 4".
1195+
11481196
<a name="LibrariesAndMocks"></a>
11491197
## Libraries and Mocks
11501198

cores/epoxy/StdioSerial.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
#include "Print.h"
1111
#include "Stream.h"
1212

13+
#ifndef SERIAL_OUTPUT_FILENO
14+
// Default output file number used by `StdioSerial` class, and therefore, the
15+
// `Serial` instance. Normally this is `STDOUT_FILENO` but can be overridden on
16+
// the command line. For example, recompiling with `-D SERIAL_OUTPUT_FILENO=2`
17+
// will configure `Serial` to print to `STDERR` instead.
18+
#define SERIAL_OUTPUT_FILENO STDOUT_FILENO
19+
#endif
20+
1321
/**
1422
* A version of Serial that reads from STDIN and sends output to STDOUT on
1523
* Linux or MacOS.
@@ -20,9 +28,13 @@ class StdioSerial: public Stream {
2028
* Construct an instance. The default output file descriptor is
2129
* STDOUT_FILENO, but STDERR_FILENO is another option. POSIX.1-2017 defines
2230
* these values in <unistd.h> (see
23-
* https://unix.stackexchange.com/questions/437602).
31+
* https://unix.stackexchange.com/questions/437602), so works under Linux
32+
* and MacOS (and assumed to work under FreeBSD, but not explicitly tested).
33+
*
34+
* The default file descriptor can be overridden by defining the
35+
* `SERIAL_OUTPUT_FILENO={n}` macro on the command line when compiling.
2436
*/
25-
StdioSerial(int fd = STDOUT_FILENO) : outputFd(fd) { }
37+
StdioSerial(int fd = SERIAL_OUTPUT_FILENO) : outputFd(fd) { }
2638

2739
/**
2840
* Override the output file descriptor. Two common values are expected to be

0 commit comments

Comments
 (0)