1- /*
2- * Arduino-serial
3- * --------------
4- *
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)
7- *
8- *
9- * Compile with something like:
10- * gcc -o arduino-serial arduino-serial.c
11- *
12- * Created 5 December 2006
13- * Copyleft (c) 2006, Tod E. Kurt, [email protected] 14- * http://todbot.com/blog/
15- *
16- *
17- * Updated 8 December 2006:
18- * Justin McBride discoevered B14400 & B28800 aren't in Linux's termios.h.
19- * I've included his patch, but commented out for now. One really needs a
20- * real make system when doing cross-platform C and I wanted to avoid that
21- * for this little program. Those baudrates aren't used much anyway. :)
22- *
23- * Updated 26 December 2007:
24- * Added ability to specify a delay (so you can wait for Arduino Diecimila)
25- * Added ability to send a binary byte number
26- *
27- * Update 31 August 2008:
28- * Added patch to clean up odd baudrates from Andy at hexapodia.org
29- *
30- */
31-
32- #include <stdio.h> /* Standard input/output definitions */
33- #include <stdlib.h>
34- #include <stdint.h> /* Standard types */
35- #include <string.h> /* String function definitions */
36- #include <unistd.h> /* UNIX standard function definitions */
37- #include <fcntl.h> /* File control definitions */
38- #include <errno.h> /* Error number definitions */
39- #include <termios.h> /* POSIX terminal control definitions */
40- #include <sys/ioctl.h>
41- #include <getopt.h>
42-
43- int serialport_init (const char * serialport , int baud );
44- int serialport_writebyte (int fd , uint8_t b );
45- int serialport_write (int fd , const char * str );
46- int serialport_read_until (int fd , char * buf , char until );
47-
48- int serialport_writebyte ( int fd , uint8_t b )
49- {
50- int n = write (fd ,& b ,1 );
51- if ( n != 1 )
52- return -1 ;
53- return 0 ;
54- }
1+ //
2+ // arduino-serial-lib -- simple library for reading/writing serial ports
3+ //
4+ // 2006-2013, Tod E. Kurt, http://todbot.com/blog/
5+ //
556
56- int serialport_write (int fd , const char * str )
57- {
58- int len = strlen (str );
59- int n = write (fd , str , len );
60- if ( n != len )
61- return -1 ;
62- return 0 ;
63- }
7+ #include "arduino-serial-lib.h"
648
65- int serialport_read_until (int fd , char * buf , char until )
66- {
67- char b [1 ];
68- int i = 0 ;
69- do {
70- int n = read (fd , b , 1 ); // read a char at a time
71- if ( n == -1 ) return -1 ; // couldn't read
72- if ( n == 0 ) {
73- usleep ( 10 * 1000 ); // wait 10 msec try again
74- continue ;
75- }
76- buf [i ] = b [0 ]; i ++ ;
77- } while ( b [0 ] != until );
9+ #include <stdio.h> // Standard input/output definitions
10+ #include <unistd.h> // UNIX standard function definitions
11+ #include <fcntl.h> // File control definitions
12+ #include <errno.h> // Error number definitions
13+ #include <termios.h> // POSIX terminal control definitions
14+ #include <string.h> // String function definitions
15+ #include <sys/ioctl.h>
7816
79- buf [i ] = 0 ; // null terminate the string
80- return 0 ;
81- }
17+ // uncomment this to debug reads
18+ //#define SERIALPORTDEBUG
8219
8320// takes the string name of the serial port (e.g. "/dev/tty.usbserial","COM1")
8421// and a baud rate (bps) and connects to that port at that speed and 8N1.
@@ -89,17 +26,20 @@ int serialport_init(const char* serialport, int baud)
8926 struct termios toptions ;
9027 int fd ;
9128
92- //fprintf(stderr,"init_serialport: opening port %s @ %d bps\n",
93- // serialport,baud);
94-
95- fd = open (serialport , O_RDWR | O_NOCTTY | O_NDELAY );
29+ //fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
30+ fd = open (serialport , O_RDWR | O_NONBLOCK );
31+
9632 if (fd == -1 ) {
97- perror ("init_serialport : Unable to open port " );
33+ perror ("serialport_init : Unable to open port " );
9834 return -1 ;
9935 }
10036
37+ //int iflags = TIOCM_DTR;
38+ //ioctl(fd, TIOCMBIS, &iflags); // turn on DTR
39+ //ioctl(fd, TIOCMBIC, &iflags); // turn off DTR
40+
10141 if (tcgetattr (fd , & toptions ) < 0 ) {
102- perror ("init_serialport : Couldn't get term attributes" );
42+ perror ("serialport_init : Couldn't get term attributes" );
10343 return -1 ;
10444 }
10545 speed_t brate = baud ; // let you override switch below if needed
@@ -128,6 +68,8 @@ int serialport_init(const char* serialport, int baud)
12868 // no flow control
12969 toptions .c_cflag &= ~CRTSCTS ;
13070
71+ //toptions.c_cflag &= ~HUPCL; // disable hang-up-on-close to avoid reset
72+
13173 toptions .c_cflag |= CREAD | CLOCAL ; // turn on READ & ignore ctrl lines
13274 toptions .c_iflag &= ~(IXON | IXOFF | IXANY ); // turn off s/w flow ctrl
13375
@@ -136,12 +78,72 @@ int serialport_init(const char* serialport, int baud)
13678
13779 // see: http://unixwiz.net/techtips/termios-vmin-vtime.html
13880 toptions .c_cc [VMIN ] = 0 ;
139- toptions .c_cc [VTIME ] = 20 ;
81+ toptions .c_cc [VTIME ] = 0 ;
82+ //toptions.c_cc[VTIME] = 20;
14083
141- if ( tcsetattr (fd , TCSANOW , & toptions ) < 0 ) {
84+ tcsetattr (fd , TCSANOW , & toptions );
85+ if ( tcsetattr (fd , TCSAFLUSH , & toptions ) < 0 ) {
14286 perror ("init_serialport: Couldn't set term attributes" );
14387 return -1 ;
14488 }
14589
14690 return fd ;
14791}
92+
93+ //
94+ int serialport_close ( int fd )
95+ {
96+ return close ( fd );
97+ }
98+
99+ //
100+ int serialport_writebyte ( int fd , uint8_t b )
101+ {
102+ int n = write (fd ,& b ,1 );
103+ if ( n != 1 )
104+ return -1 ;
105+ return 0 ;
106+ }
107+
108+ //
109+ int serialport_write (int fd , const char * str )
110+ {
111+ int len = strlen (str );
112+ int n = write (fd , str , len );
113+ if ( n != len ) {
114+ perror ("serialport_write: couldn't write whole string\n" );
115+ return -1 ;
116+ }
117+ return 0 ;
118+ }
119+
120+ //
121+ int serialport_read_until (int fd , char * buf , char until , int buf_max , int timeout )
122+ {
123+ char b [1 ]; // read expects an array, so we give it a 1-byte array
124+ int i = 0 ;
125+ do {
126+ int n = read (fd , b , 1 ); // read a char at a time
127+ if ( n == -1 ) return -1 ; // couldn't read
128+ if ( n == 0 ) {
129+ usleep ( 1 * 1000 ); // wait 1 msec try again
130+ timeout -- ;
131+ continue ;
132+ }
133+ #ifdef SERIALPORTDEBUG
134+ printf ("serialport_read_until: i=%d, n=%d b='%c'\n" ,i ,n ,b [0 ]); // debug
135+ #endif
136+ buf [i ] = b [0 ];
137+ i ++ ;
138+ } while ( b [0 ] != until && i < buf_max && timeout > 0 );
139+
140+ buf [i ] = 0 ; // null terminate the string
141+ return 0 ;
142+ }
143+
144+ //
145+ int serialport_flush (int fd )
146+ {
147+ sleep (2 ); //required to make flush work, for some reason
148+ return tcflush (fd , TCIOFLUSH );
149+ }
0 commit comments