2 * This file is part of the libserialport project.
4 * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @mainpage libserialport API
26 * libserialport is a minimal library written in C that is intended to take
27 * care of the OS-specific details when writing software that uses serial ports.
29 * By writing your serial code to use libserialport, you enable it to work
30 * transparently on any platform supported by the library.
32 * The operations that are supported are:
34 * - @ref Enumeration (obtaining a list of serial ports on the system)
36 * - @ref Configuration (baud rate, parity, etc.)
40 * libserialport is an open source project released under the LGPL3+ license.
45 * The API is simple, and designed to be a minimal wrapper around the serial
46 * port support in each OS.
48 * Most functions take a pointer to a struct sp_port, which represents a serial
49 * port. These structures are always allocated and freed by the library, using
50 * the functions in the @ref Enumeration "Enumeration" section.
52 * All functions can return only four possible error values:
54 * - @ref SP_ERR_ARG means that a function was called with invalid
55 * arguments. This implies a bug in the caller. The arguments passed would
56 * be invalid regardless of the underlying OS or serial device involved.
58 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
59 * message provided by the OS can be obtained by calling sp_last_error_code()
60 * or sp_last_error_message().
62 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
63 * operation in the current OS, driver or device. No error message is
64 * available from the OS in this case. There is either no way to request
65 * the operation in the first place, or libserialport does not know how to
66 * do so in the current version.
68 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
70 * All of these error values are negative.
72 * Function calls that succeed return @ref SP_OK, which is equal to zero,
73 * or where otherwise documented a positive value.
76 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
77 #define LIBSERIALPORT_LIBSERIALPORT_H
88 /* Package version macros (e.g. for conditional compilation). */
89 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
90 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
91 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
92 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
94 /* Library/libtool version macros (e.g. for conditional compilation). */
95 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
96 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
97 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
98 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
100 /** Return values. */
102 /** Operation completed successfully. */
104 /** Invalid arguments were passed to the function. */
106 /** A system error occured while executing the operation. */
108 /** A memory allocation failed while executing the operation. */
110 /** The requested operation is not supported by this system or device. */
114 /** Port access modes. */
116 /** Open port for read access. */
118 /** Open port for write access. */
120 /** Open port in non-blocking mode. */
121 SP_MODE_NONBLOCK = 4,
124 /** Buffer selection. */
128 /** Output buffer. */
134 /** Parity settings. */
136 /** Special value to indicate setting should be left alone. */
137 SP_PARITY_INVALID = -1,
146 /** RTS pin behaviour. */
148 /** Special value to indicate setting should be left alone. */
154 /** RTS used for flow control. */
155 SP_RTS_FLOW_CONTROL = 2,
158 /** CTS pin behaviour. */
160 /** Special value to indicate setting should be left alone. */
164 /** CTS used for flow control. */
165 SP_CTS_FLOW_CONTROL = 1,
168 /** DTR pin behaviour. */
170 /** Special value to indicate setting should be left alone. */
176 /** DTR used for flow control. */
177 SP_DTR_FLOW_CONTROL = 2,
180 /** DSR pin behaviour. */
182 /** Special value to indicate setting should be left alone. */
186 /** DSR used for flow control. */
187 SP_DSR_FLOW_CONTROL = 1,
190 /** XON/XOFF flow control behaviour. */
192 /** Special value to indicate setting should be left alone. */
193 SP_XONXOFF_INVALID = -1,
194 /** XON/XOFF disabled. */
195 SP_XONXOFF_DISABLED = 0,
196 /** XON/XOFF enabled for input only. */
198 /** XON/XOFF enabled for output only. */
200 /** XON/XOFF enabled for input and output. */
201 SP_XONXOFF_INOUT = 3,
204 /** Standard flow control combinations. */
205 enum sp_flowcontrol {
206 /** No flow control. */
207 SP_FLOWCONTROL_NONE = 0,
208 /** Software flow control using XON/XOFF characters. */
209 SP_FLOWCONTROL_XONXOFF = 1,
210 /** Hardware flow control using RTS/CTS signals. */
211 SP_FLOWCONTROL_RTSCTS = 2,
212 /** Hardware flow control using DTR/DSR signals. */
213 SP_FLOWCONTROL_DTRDSR = 3,
216 /** Input signals. */
218 /** Clear to send. */
220 /** Data set ready. */
222 /** Data carrier detect. */
224 /** Ring indicator. */
228 /** A serial port. */
230 /** Name used to open the port. */
233 /** OS-specific port handle. */
242 /** Configuration for a serial port. */
243 struct sp_port_config {
244 /** Baud rate in bits per second. */
246 /** Number of data bits to use. Valid values are 5 to 8. */
248 /** Parity setting to use. */
249 enum sp_parity parity;
250 /** Number of stop bits to use (1 or 2). */
260 /** XON/XOFF flow control mode. */
261 enum sp_xonxoff xon_xoff;
265 @defgroup Enumeration Port enumeration
270 * Obtain a pointer to a new sp_port structure representing the named port.
272 * The user should allocate a variable of type "struct sp_port *" and pass a
273 * pointer to this to receive the result.
275 * The result should be freed after use by calling sp_free_port().
277 * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
278 * failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
279 * is returned, the variable pointed to by port_ptr will be set to NULL.
280 * Otherwise, it will be set to point to the newly allocated port.
282 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
285 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
287 void sp_free_port(struct sp_port *port);
290 * List the serial ports available on the system.
292 * The result obtained is an array of pointers to sp_port structures,
293 * terminated by a NULL. The user should allocate a variable of type
294 * "struct sp_port **" and pass a pointer to this to receive the result.
296 * The result should be freed after use by calling sp_free_port_list().
297 * If a port from the list is to be used after freeing the list, it must be
298 * copied first using sp_copy_port().
300 * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
301 * failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
302 * is returned, the variable pointed to by list_ptr will be set to NULL.
303 * Otherwise, it will be set to point to the newly allocated array.
305 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
308 * Make a new copy of a sp_port structure.
310 * The user should allocate a variable of type "struct sp_port *" and pass a
311 * pointer to this to receive the result.
313 * The copy should be freed after use by calling sp_free_port().
315 * @return SP_OK on success, SP_ERR_MEM on allocation failure, or SP_ERR_ARG
316 * if an invalid port or pointer is passed. If any error is returned,
317 * the variable pointed to by copy_ptr will be set to NULL. Otherwise,
318 * it will be set to point to the newly allocated copy.
320 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
323 * Free a port list obtained from sp_list_ports().
325 * This will also free all the sp_port structures referred to from the list;
326 * any that are to be retained must be copied first using sp_copy_port().
328 void sp_free_port_list(struct sp_port **ports);
332 * @defgroup Ports Opening and closing ports
337 * Open the specified serial port.
339 * @param port Pointer to port structure.
340 * @param flags Flags to use when opening the serial port.
342 * @return SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
343 * failure, or SP_ERR_ARG if an invalid port is passed.
345 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
348 * Close the specified serial port.
350 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
351 * if an invalid port is passed.
353 enum sp_return sp_close(struct sp_port *port);
357 * @defgroup Configuration Setting port parameters
362 * Get the current configuration of the specified serial port.
364 * The user should allocate a struct sp_port_config, then pass a pointer to it
365 * as the config parameter. The struct will be populated with the port
368 * Any setting that is in a state not recognised or supported by
369 * libserialport will have its value set to -1 in the struct.
371 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
372 * for invalid arguments.
374 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
377 * Set the configuration for the specified serial port.
379 * The user should populate a struct sp_port_config, then pass a pointer to it
380 * as the config parameter.
382 * To retain the current value of any setting, set that field to -1.
384 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
385 * for invalid arguments.
387 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
390 * Set the baud rate for the specified serial port.
392 * @param port Pointer to port structure.
393 * @param baudrate Baud rate in bits per second.
395 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
396 * for invalid arguments.
398 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
401 * Set the number of data bits for the specified serial port.
403 * @param port Pointer to port structure.
404 * @param bits Number of data bits to use. Valid values are 5 to 8.
406 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
407 * for invalid arguments.
409 enum sp_return sp_set_bits(struct sp_port *port, int bits);
412 * Set the parity for the specified serial port.
414 * @param port Pointer to port structure.
415 * @param parity Parity setting to use.
417 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
418 * for invalid arguments.
420 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
423 * Set the number of stop bits for the specified port.
425 * @param port Pointer to port structure.
426 * @param stopbits Number of stop bits to use (1 or 2).
428 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
429 * for invalid arguments.
431 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
434 * Set the flow control type for the specified serial port.
436 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
437 * XON/XOFF settings as necessary for the specified flow control
438 * type. For more fine-grained control of these settings, use their
439 * individual configuration functions or the sp_set_config() function.
441 * @param port Pointer to port structure.
442 * @param flowcontrol Flow control setting to use.
444 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
445 * for invalid arguments.
447 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
450 * Set the RTS pin behaviour for the specified port.
452 * @param port Pointer to port structure.
453 * @param rts RTS pin mode.
455 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
456 * for invalid arguments.
458 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
461 * Set the CTS pin behaviour for the specified port.
463 * @param port Pointer to port structure.
464 * @param cts CTS pin mode.
466 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
467 * for invalid arguments.
469 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
472 * Set the DTR pin behaviour for the specified port.
474 * @param port Pointer to port structure.
475 * @param dtr DTR pin mode.
477 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
478 * for invalid arguments.
480 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
483 * Set the RTS pin behaviour for the specified port.
485 * @param port Pointer to port structure.
486 * @param dsr DSR pin mode.
488 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
489 * for invalid arguments.
491 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
494 * Configure XON/XOFF flow control for the specified port.
496 * @param port Pointer to port structure.
497 * @param xon_xoff XON/XOFF flow control mode.
499 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
500 * for invalid arguments.
502 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
506 * @defgroup Data Reading, writing, and flushing data
511 * Read bytes from the specified serial port.
513 * Note that this function may return after reading less than the specified
514 * number of bytes; it is the user's responsibility to iterate as necessary
517 * @param port Pointer to port structure.
518 * @param buf Buffer in which to store the bytes read.
519 * @param count Maximum number of bytes to read.
521 * @return The number of bytes read, SP_ERR_FAIL on failure,
522 * or SP_ERR_ARG for invalid arguments.
524 enum sp_return sp_read(struct sp_port *port, void *buf, size_t count);
527 * Write bytes to the specified serial port.
529 * Note that this function may return after writing less than the specified
530 * number of bytes; it is the user's responsibility to iterate as necessary
533 * @param port Pointer to port structure.
534 * @param buf Buffer containing the bytes to write.
535 * @param count Maximum number of bytes to write.
537 * @return The number of bytes written, SP_ERR_FAIL on failure,
538 * or SP_ERR_ARG for invalid arguments.
540 enum sp_return sp_write(struct sp_port *port, const void *buf, size_t count);
543 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
545 * @param port Pointer to port structure.
546 * @param buffers Which buffer(s) to flush.
548 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
549 * if an invalid port is passed.
551 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
554 * Wait for buffered data to be transmitted.
556 * @param port Pointer to port structure.
558 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
559 * if an invalid port is passed.
561 enum sp_return sp_drain(struct sp_port *port);
565 * @defgroup Signal Port signalling operations
570 * Gets the status of the control signals for the specified port.
572 * The user should allocate a variable of type "enum sp_signal" and pass a
573 * pointer to this variable to receive the result. The result is a bitmask
574 * in which individual signals can be checked by bitwise OR with values of
575 * the sp_signal enum.
577 * @param port Pointer to port structure.
578 * @param signals Pointer to variable to receive result.
580 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
581 * if an an invalid port or pointer is passed.
583 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
586 * Put the port transmit line into the break state.
588 * @param port Pointer to port structure.
590 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
591 * if an invalid port is passed.
593 enum sp_return sp_start_break(struct sp_port *port);
596 * Take the port transmit line out of the break state.
598 * @param port Pointer to port structure.
600 * @return SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
601 * if an invalid port is passed.
603 enum sp_return sp_end_break(struct sp_port *port);
607 * @defgroup Errors Obtaining error information
612 * Get the error code for a failed operation.
614 * In order to obtain the correct result, this function should be called
615 * straight after the failure, before executing any other system operations.
617 * @return The system's numeric code for the error that caused the last
620 int sp_last_error_code(void);
623 * Get the error message for a failed operation.
625 * In order to obtain the correct result, this function should be called
626 * straight after the failure, before executing other system operations.
628 * @return The system's message for the error that caused the last
629 * operation to fail. This string may be allocated by the function,
630 * and should be freed after use by calling sp_free_error_message().
632 char *sp_last_error_message(void);
635 * Free an error message returned by sp_last_error_message().
637 void sp_free_error_message(char *message);