3 * $Id: serial.c,v 1.2 2002/01/30 09:23:58 mdw Exp $
5 * Common serial functionality
7 * (c) 2001 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Jog: Programming for a jogging machine.
14 * Jog is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * Jog is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Jog; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @getbaud@ --- *
41 * Arguments: @const char *p@ = pointer to string
42 * @const char **pp@ = where to store final pointer
44 * Returns: Baud rate, or @-1@ on failure.
46 * Use: Parses a baud rate, either as a raw number or as an
47 * expression like `9k6'.
50 static long getbaud(const char *p
, const char **pp
)
56 /* --- Main loop --- */
59 int ch
= (unsigned char)*p
;
87 x
= x
* s
+ (ch
- '0') * f
;
100 /* --- @getparity@ --- *
102 * Arguments: @const char *p@ = pointer to string
103 * @const char **pp@ = where to store final pointer
105 * Returns: Parity constant or @-1@ on error.
107 * Use: Parses a parity setting.
110 static int getparity(const char *p
, const char **pp
)
114 for (q
= p
; isalpha((unsigned char)*q
); q
++)
120 if (strncmp(p
, "none", q
- p
) == 0)
121 return (PARITY_NONE
);
122 else if (strncmp(p
, "odd", q
- p
) == 0)
124 else if (strncmp(p
, "even", q
- p
) == 0)
125 return (PARITY_EVEN
);
130 /* --- @getflow@ --- *
132 * Arguments: @const char *p@ = pointer to string
133 * @const char **pp@ = where to store final pointer
135 * Returns: Flow control constant or @-1@ on error.
137 * Use: Parses a flow control setting.
140 static int getflow(const char *p
, const char **pp
)
144 for (q
= p
; isalpha((unsigned char)*q
); q
++)
150 if (strncmp(p
, "none", q
- p
) == 0)
152 else if (strncmp(p
, "soft", q
- p
) == 0 ||
153 strncmp(p
, "xon-xoff", q
- p
) == 0 ||
154 strncmp(p
, "xon/xoff", q
- p
) == 0 ||
155 strncmp(p
, "xonxoff", q
- p
) == 0)
156 return (FLOW_XONXOFF
);
157 else if (strncmp(p
, "hard", q
- p
) == 0 ||
158 strncmp(p
, "cts-rts", q
- p
) == 0 ||
159 strncmp(p
, "cts/rts", q
- p
) == 0 ||
160 strncmp(p
, "ctsrts", q
- p
) == 0 ||
161 strncmp(p
, "rts/cts", q
- p
) == 0 ||
162 strncmp(p
, "rts-cts", q
- p
) == 0 ||
163 strncmp(p
, "rtscts", q
- p
) == 0)
164 return (FLOW_RTSCTS
);
169 /* --- @serial_parse@ --- *
171 * Arguments: @serial_config *sc@ = pointer to serial config structure
172 * @const char *k, *v@ = keyword and value pair
174 * Returns: Zero if OK, or @-1@ on error.
176 * Use: Parses a serial config string into something more
177 * reasonable. The config structure should have been
178 * initialized to something sensible (e.g., @SERIAL_INIT@)
182 int serial_parse(serial_config
*sc
, const char *k
, const char *v
)
188 if (strcmp(k
, "baud") == 0 || strcmp(k
, "bps") == 0) {
189 if (!v
|| (x
= getbaud(v
, &q
)) < 0 || *q
)
192 } else if (strcmp(k
, "data") == 0 || strcmp(k
, "data-bits") == 0 ||
193 strcmp(k
, "databits") == 0 || strcmp(k
, "data-len") == 0 ||
194 strcmp(k
, "datalen") == 0 || strcmp(k
, "word") == 0 ||
195 strcmp(k
, "word-len") == 0 || strcmp(k
, "wordlen") == 0) {
196 if (!v
|| (x
= strtol(v
, &qq
, 10)) == 0 || *qq
)
199 } else if (strcmp(k
, "parity") == 0) {
200 if (!v
|| (x
= getparity(v
, &q
)) < 0 || *q
)
203 } else if (strcmp(k
, "stop") == 0 || strcmp(k
, "stop-bits") == 0 ||
204 strcmp(k
, "stopbits") == 0) {
205 if (!v
|| (x
= strtol(v
, &qq
, 10)) == 0 || *qq
)
208 } else if (strcmp(k
, "flow") == 0 || strcmp(k
, "flow-control") == 0 ||
209 strcmp(k
, "flowcontrol") == 0) {
210 if (!v
|| (x
= getflow(v
, &q
)) < 0 || *q
)
213 } else if (strcmp(k
, "format") == 0) {
216 if ((x
= strtol(v
, &qq
, 10)) == 0)
222 if ((x
= getparity(v
, &q
)) < 0)
228 if ((x
= strtol(v
, &qq
, 10)) == 0)
240 /*----- Test rig ----------------------------------------------------------*/
247 int main(int argc
, char *argv
[])
250 static const char *ptab
[] = { "none", "odd", "even" };
251 static const char *ftab
[] = { "none", "xon/xoff", "rts/cts" };
253 for (i
= 1; i
< argc
; i
++) {
254 serial_config sc
= SERIAL_INIT
;
257 for (k
= strtok(argv
[i
], ";"); k
; k
= strtok(0, ";")) {
258 if ((v
= strchr(k
, '=')) != 0)
260 if (serial_parse(&sc
, k
, v
)) {
261 printf("invalid serial configuration `%s=%s'\n", k
, v
);
265 printf("%lu %u-%s-%u %s\n", sc
.baud
, sc
.wordlen
,
266 ptab
[sc
.parity
], sc
.stopbits
, ftab
[sc
.flow
]);
274 /*----- That's all, folks -------------------------------------------------*/