chiark / gitweb /
Debian build: ./newVersion updates debian/changelog too.
[wiringPi.git] / wiringPi / wiringSerial.c
1 /*
2  * wiringSerial.c:
3  *      Handle a serial port
4  ***********************************************************************
5  * This file is part of wiringPi:
6  *      https://projects.drogon.net/raspberry-pi/wiringpi/
7  *
8  *    wiringPi is free software: you can redistribute it and/or modify
9  *    it under the terms of the GNU Lesser General Public License as published by
10  *    the Free Software Foundation, either version 3 of the License, or
11  *    (at your option) any later version.
12  *
13  *    wiringPi is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU Lesser General Public License for more details.
17  *
18  *    You should have received a copy of the GNU Lesser General Public License
19  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
20  ***********************************************************************
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <termios.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34
35 #include "wiringSerial.h"
36
37 /*
38  * serialOpen:
39  *      Open and initialise the serial port, setting all the right
40  *      port parameters - or as many as are required - hopefully!
41  *********************************************************************************
42  */
43
44 int serialOpen (const char *device, const int baud)
45 {
46   struct termios options ;
47   speed_t myBaud ;
48   int     status, fd ;
49
50   switch (baud)
51   {
52     case     50:        myBaud =     B50 ; break ;
53     case     75:        myBaud =     B75 ; break ;
54     case    110:        myBaud =    B110 ; break ;
55     case    134:        myBaud =    B134 ; break ;
56     case    150:        myBaud =    B150 ; break ;
57     case    200:        myBaud =    B200 ; break ;
58     case    300:        myBaud =    B300 ; break ;
59     case    600:        myBaud =    B600 ; break ;
60     case   1200:        myBaud =   B1200 ; break ;
61     case   1800:        myBaud =   B1800 ; break ;
62     case   2400:        myBaud =   B2400 ; break ;
63     case   4800:        myBaud =   B4800 ; break ;
64     case   9600:        myBaud =   B9600 ; break ;
65     case  19200:        myBaud =  B19200 ; break ;
66     case  38400:        myBaud =  B38400 ; break ;
67     case  57600:        myBaud =  B57600 ; break ;
68     case 115200:        myBaud = B115200 ; break ;
69     case 230400:        myBaud = B230400 ; break ;
70
71     default:
72       return -2 ;
73   }
74
75   if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
76     return -1 ;
77
78   fcntl (fd, F_SETFL, O_RDWR) ;
79
80 // Get and modify current options:
81
82   tcgetattr (fd, &options) ;
83
84     cfmakeraw   (&options) ;
85     cfsetispeed (&options, myBaud) ;
86     cfsetospeed (&options, myBaud) ;
87
88     options.c_cflag |= (CLOCAL | CREAD) ;
89     options.c_cflag &= ~PARENB ;
90     options.c_cflag &= ~CSTOPB ;
91     options.c_cflag &= ~CSIZE ;
92     options.c_cflag |= CS8 ;
93     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
94     options.c_oflag &= ~OPOST ;
95
96     options.c_cc [VMIN]  =   0 ;
97     options.c_cc [VTIME] = 100 ;        // Ten seconds (100 deciseconds)
98
99   tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;
100
101   ioctl (fd, TIOCMGET, &status);
102
103   status |= TIOCM_DTR ;
104   status |= TIOCM_RTS ;
105
106   ioctl (fd, TIOCMSET, &status);
107
108   usleep (10000) ;      // 10mS
109
110   return fd ;
111 }
112
113
114 /*
115  * serialFlush:
116  *      Flush the serial buffers (both tx & rx)
117  *********************************************************************************
118  */
119
120 void serialFlush (const int fd)
121 {
122   tcflush (fd, TCIOFLUSH) ;
123 }
124
125
126 /*
127  * serialClose:
128  *      Release the serial port
129  *********************************************************************************
130  */
131
132 void serialClose (const int fd)
133 {
134   close (fd) ;
135 }
136
137
138 /*
139  * serialPutchar:
140  *      Send a single character to the serial port
141  *********************************************************************************
142  */
143
144 void serialPutchar (const int fd, const unsigned char c)
145 {
146   write (fd, &c, 1) ;
147 }
148
149
150 /*
151  * serialPuts:
152  *      Send a string to the serial port
153  *********************************************************************************
154  */
155
156 void serialPuts (const int fd, const char *s)
157 {
158   write (fd, s, strlen (s)) ;
159 }
160
161 /*
162  * serialPrintf:
163  *      Printf over Serial
164  *********************************************************************************
165  */
166
167 void serialPrintf (const int fd, const char *message, ...)
168 {
169   va_list argp ;
170   char buffer [1024] ;
171
172   va_start (argp, message) ;
173     vsnprintf (buffer, 1023, message, argp) ;
174   va_end (argp) ;
175
176   serialPuts (fd, buffer) ;
177 }
178
179
180 /*
181  * serialDataAvail:
182  *      Return the number of bytes of data avalable to be read in the serial port
183  *********************************************************************************
184  */
185
186 int serialDataAvail (const int fd)
187 {
188   int result ;
189
190   if (ioctl (fd, FIONREAD, &result) == -1)
191     return -1 ;
192
193   return result ;
194 }
195
196
197 /*
198  * serialGetchar:
199  *      Get a single character from the serial device.
200  *      Note: Zero is a valid character and this function will time-out after
201  *      10 seconds.
202  *********************************************************************************
203  */
204
205 int serialGetchar (const int fd)
206 {
207   uint8_t x ;
208
209   if (read (fd, &x, 1) != 1)
210     return -1 ;
211
212   return ((int)x) & 0xFF ;
213 }