Skip to content

Commit cde9975

Browse files
author
Roger Brooks
committed
Added a option to use stdin for sent string.
1 parent 950b500 commit cde9975

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

arduino-serial.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* arduino-serial
33
* --------------
4-
*
4+
*
55
* A simple command-line example program showing how a computer can
6-
* communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
6+
* communicate with an Arduino board. Works on any POSIX system (Mac/Unix/PC)
77
*
88
*
99
* Compile with something like:
@@ -17,8 +17,8 @@
1717
* Originally created 5 December 2006
1818
* 2006-2013, Tod E. Kurt, http://todbot.com/blog/
1919
*
20-
*
21-
* Updated 8 December 2006:
20+
*
21+
* Updated 8 December 2006:
2222
* Justin McBride discovered B14400 & B28800 aren't in Linux's termios.h.
2323
* I've included his patch, but commented out for now. One really needs a
2424
* real make system when doing cross-platform C and I wanted to avoid that
@@ -33,7 +33,7 @@
3333
*
3434
* Update 6 April 2012:
3535
* Split into a library and app parts, put on github
36-
*
36+
*
3737
* Update 20 Apr 2013:
3838
* Small updates to deal with flushing and read backs
3939
* Fixed re-opens
@@ -45,9 +45,9 @@
4545
*
4646
*/
4747

48-
#include <stdio.h> // Standard input/output definitions
49-
#include <stdlib.h>
50-
#include <string.h> // String function definitions
48+
#include <stdio.h> // Standard input/output definitions
49+
#include <stdlib.h>
50+
#include <string.h> // String function definitions
5151
#include <unistd.h> // for usleep()
5252
#include <getopt.h>
5353

@@ -61,10 +61,11 @@ void usage(void)
6161
"\n"
6262
"Options:\n"
6363
" -h, --help Print this help message\n"
64-
" -b, --baud=baudrate Baudrate (bps) of Arduino (default 9600)\n"
64+
" -b, --baud=baudrate Baudrate (bps) of Arduino (default 9600)\n"
6565
" -p, --port=serialport Serial port Arduino is connected to\n"
6666
" -s, --send=string Send string to Arduino\n"
6767
" -S, --sendline=string Send string with newline to Arduino\n"
68+
" -i --stdinput Use standard input\n"
6869
" -r, --receive Receive string from Arduino & print it out\n"
6970
" -n --num=num Send a number as a single byte\n"
7071
" -F --flush Flush serial port buffers for fresh reading\n"
@@ -87,7 +88,7 @@ void error(char* msg)
8788
exit(EXIT_FAILURE);
8889
}
8990

90-
int main(int argc, char *argv[])
91+
int main(int argc, char *argv[])
9192
{
9293
const int buf_max = 256;
9394

@@ -112,6 +113,7 @@ int main(int argc, char *argv[])
112113
{"baud", required_argument, 0, 'b'},
113114
{"send", required_argument, 0, 's'},
114115
{"sendline", required_argument, 0, 'S'},
116+
{"stdinput", no_argument, 0, 'i'},
115117
{"receive", no_argument, 0, 'r'},
116118
{"flush", no_argument, 0, 'F'},
117119
{"num", required_argument, 0, 'n'},
@@ -121,9 +123,9 @@ int main(int argc, char *argv[])
121123
{"quiet", no_argument, 0, 'q'},
122124
{NULL, 0, 0, 0}
123125
};
124-
126+
125127
while(1) {
126-
opt = getopt_long (argc, argv, "hp:b:s:S:rFn:d:qe:t:",
128+
opt = getopt_long (argc, argv, "hp:b:s:S:i:rFn:d:qe:t:",
127129
loptions, &option_index);
128130
if (opt==-1) break;
129131
switch (opt) {
@@ -176,9 +178,18 @@ int main(int argc, char *argv[])
176178
rc = serialport_write(fd, buf);
177179
if(rc==-1) error("error writing");
178180
break;
181+
case 'i':
182+
rc=-1;
183+
if( fd == -1) error("serial port not opened");
184+
while(fgets(buf, buf_max, stdin)) {
185+
if( !quiet ) printf("send string:%s\n", buf);
186+
rc = serialport_write(fd, buf);
187+
}
188+
if(rc==-1) error("error writing");
189+
break;
179190
case 'r':
180191
if( fd == -1 ) error("serial port not opened");
181-
memset(buf,0,buf_max); //
192+
memset(buf,0,buf_max); //
182193
serialport_read_until(fd, buf, eolchar, buf_max, timeout);
183194
if( !quiet ) printf("read string:");
184195
printf("%s\n", buf);
@@ -192,6 +203,6 @@ int main(int argc, char *argv[])
192203
}
193204
}
194205

195-
exit(EXIT_SUCCESS);
206+
exit(EXIT_SUCCESS);
196207
} // end main
197-
208+

0 commit comments

Comments
 (0)