@@ -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
11121113By 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);
11201121Serial.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
0 commit comments