1
1
/*
2
2
* arduino-serial
3
3
* --------------
4
- *
4
+ *
5
5
* 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)
7
7
*
8
8
*
9
9
* Compile with something like:
17
17
* Originally created 5 December 2006
18
18
* 2006-2013, Tod E. Kurt, http://todbot.com/blog/
19
19
*
20
- *
21
- * Updated 8 December 2006:
20
+ *
21
+ * Updated 8 December 2006:
22
22
* Justin McBride discovered B14400 & B28800 aren't in Linux's termios.h.
23
23
* I've included his patch, but commented out for now. One really needs a
24
24
* real make system when doing cross-platform C and I wanted to avoid that
33
33
*
34
34
* Update 6 April 2012:
35
35
* Split into a library and app parts, put on github
36
- *
36
+ *
37
37
* Update 20 Apr 2013:
38
38
* Small updates to deal with flushing and read backs
39
39
* Fixed re-opens
45
45
*
46
46
*/
47
47
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
51
51
#include <unistd.h> // for usleep()
52
52
#include <getopt.h>
53
53
@@ -61,10 +61,11 @@ void usage(void)
61
61
"\n"
62
62
"Options:\n"
63
63
" -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"
65
65
" -p, --port=serialport Serial port Arduino is connected to\n"
66
66
" -s, --send=string Send string to Arduino\n"
67
67
" -S, --sendline=string Send string with newline to Arduino\n"
68
+ " -i --stdinput Use standard input\n"
68
69
" -r, --receive Receive string from Arduino & print it out\n"
69
70
" -n --num=num Send a number as a single byte\n"
70
71
" -F --flush Flush serial port buffers for fresh reading\n"
@@ -87,7 +88,7 @@ void error(char* msg)
87
88
exit (EXIT_FAILURE );
88
89
}
89
90
90
- int main (int argc , char * argv [])
91
+ int main (int argc , char * argv [])
91
92
{
92
93
const int buf_max = 256 ;
93
94
@@ -112,6 +113,7 @@ int main(int argc, char *argv[])
112
113
{"baud" , required_argument , 0 , 'b' },
113
114
{"send" , required_argument , 0 , 's' },
114
115
{"sendline" , required_argument , 0 , 'S' },
116
+ {"stdinput" , no_argument , 0 , 'i' },
115
117
{"receive" , no_argument , 0 , 'r' },
116
118
{"flush" , no_argument , 0 , 'F' },
117
119
{"num" , required_argument , 0 , 'n' },
@@ -121,9 +123,9 @@ int main(int argc, char *argv[])
121
123
{"quiet" , no_argument , 0 , 'q' },
122
124
{NULL , 0 , 0 , 0 }
123
125
};
124
-
126
+
125
127
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:" ,
127
129
loptions , & option_index );
128
130
if (opt == -1 ) break ;
129
131
switch (opt ) {
@@ -176,9 +178,18 @@ int main(int argc, char *argv[])
176
178
rc = serialport_write (fd , buf );
177
179
if (rc == -1 ) error ("error writing" );
178
180
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 ;
179
190
case 'r' :
180
191
if ( fd == -1 ) error ("serial port not opened" );
181
- memset (buf ,0 ,buf_max ); //
192
+ memset (buf ,0 ,buf_max ); //
182
193
serialport_read_until (fd , buf , eolchar , buf_max , timeout );
183
194
if ( !quiet ) printf ("read string:" );
184
195
printf ("%s\n" , buf );
@@ -192,6 +203,6 @@ int main(int argc, char *argv[])
192
203
}
193
204
}
194
205
195
- exit (EXIT_SUCCESS );
206
+ exit (EXIT_SUCCESS );
196
207
} // end main
197
-
208
+
0 commit comments