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.)
37 * - @ref Signals (modem control lines, breaks, etc.)
41 * libserialport is an open source project released under the LGPL3+ license.
46 * The API is simple, and designed to be a minimal wrapper around the serial
47 * port support in each OS.
49 * Most functions take a pointer to a struct sp_port, which represents a serial
50 * port. These structures are always allocated and freed by the library, using
51 * the functions in the @ref Enumeration "Enumeration" section.
53 * Most functions have return type @ref sp_return and can return only four
54 * possible error values:
56 * - @ref SP_ERR_ARG means that a function was called with invalid
57 * arguments. This implies a bug in the caller. The arguments passed would
58 * be invalid regardless of the underlying OS or serial device involved.
60 * - @ref SP_ERR_FAIL means that the OS reported a failure. The error code or
61 * message provided by the OS can be obtained by calling sp_last_error_code()
62 * or sp_last_error_message().
64 * - @ref SP_ERR_SUPP indicates that there is no support for the requested
65 * operation in the current OS, driver or device. No error message is
66 * available from the OS in this case. There is either no way to request
67 * the operation in the first place, or libserialport does not know how to
68 * do so in the current version.
70 * - @ref SP_ERR_MEM indicates that a memory allocation failed.
72 * All of these error values are negative.
74 * Calls that succeed return @ref SP_OK, which is equal to zero. Some functions
75 * declared @ref sp_return can also return a positive value for a successful
76 * numeric result, e.g. sp_read() and sp_write().
79 #ifndef LIBSERIALPORT_LIBSERIALPORT_H
80 #define LIBSERIALPORT_LIBSERIALPORT_H
91 /* Package version macros (e.g. for conditional compilation). */
92 #define SP_PACKAGE_VERSION_MAJOR @SP_PACKAGE_VERSION_MAJOR@
93 #define SP_PACKAGE_VERSION_MINOR @SP_PACKAGE_VERSION_MINOR@
94 #define SP_PACKAGE_VERSION_MICRO @SP_PACKAGE_VERSION_MICRO@
95 #define SP_PACKAGE_VERSION_STRING "@SP_PACKAGE_VERSION@"
97 /* Library/libtool version macros (e.g. for conditional compilation). */
98 #define SP_LIB_VERSION_CURRENT @SP_LIB_VERSION_CURRENT@
99 #define SP_LIB_VERSION_REVISION @SP_LIB_VERSION_REVISION@
100 #define SP_LIB_VERSION_AGE @SP_LIB_VERSION_AGE@
101 #define SP_LIB_VERSION_STRING "@SP_LIB_VERSION@"
103 /** Return values. */
105 /** Operation completed successfully. */
107 /** Invalid arguments were passed to the function. */
109 /** A system error occured while executing the operation. */
111 /** A memory allocation failed while executing the operation. */
113 /** The requested operation is not supported by this system or device. */
117 /** Port access modes. */
119 /** Open port for read access. */
121 /** Open port for write access. */
125 /** Buffer selection. */
129 /** Output buffer. */
135 /** Parity settings. */
137 /** Special value to indicate setting should be left alone. */
138 SP_PARITY_INVALID = -1,
151 /** RTS pin behaviour. */
153 /** Special value to indicate setting should be left alone. */
159 /** RTS used for flow control. */
160 SP_RTS_FLOW_CONTROL = 2,
163 /** CTS pin behaviour. */
165 /** Special value to indicate setting should be left alone. */
169 /** CTS used for flow control. */
170 SP_CTS_FLOW_CONTROL = 1,
173 /** DTR pin behaviour. */
175 /** Special value to indicate setting should be left alone. */
181 /** DTR used for flow control. */
182 SP_DTR_FLOW_CONTROL = 2,
185 /** DSR pin behaviour. */
187 /** Special value to indicate setting should be left alone. */
191 /** DSR used for flow control. */
192 SP_DSR_FLOW_CONTROL = 1,
195 /** XON/XOFF flow control behaviour. */
197 /** Special value to indicate setting should be left alone. */
198 SP_XONXOFF_INVALID = -1,
199 /** XON/XOFF disabled. */
200 SP_XONXOFF_DISABLED = 0,
201 /** XON/XOFF enabled for input only. */
203 /** XON/XOFF enabled for output only. */
205 /** XON/XOFF enabled for input and output. */
206 SP_XONXOFF_INOUT = 3,
209 /** Standard flow control combinations. */
210 enum sp_flowcontrol {
211 /** No flow control. */
212 SP_FLOWCONTROL_NONE = 0,
213 /** Software flow control using XON/XOFF characters. */
214 SP_FLOWCONTROL_XONXOFF = 1,
215 /** Hardware flow control using RTS/CTS signals. */
216 SP_FLOWCONTROL_RTSCTS = 2,
217 /** Hardware flow control using DTR/DSR signals. */
218 SP_FLOWCONTROL_DTRDSR = 3,
221 /** Input signals. */
223 /** Clear to send. */
225 /** Data set ready. */
227 /** Data carrier detect. */
229 /** Ring indicator. */
233 /** A serial port. */
236 /** Configuration for a serial port. */
237 struct sp_port_config;
240 @defgroup Enumeration Port enumeration
245 * Obtain a pointer to a new sp_port structure representing the named port.
247 * The user should allocate a variable of type "struct sp_port *" and pass a
248 * pointer to this to receive the result.
250 * The result should be freed after use by calling sp_free_port().
252 * If any error is returned, the variable pointed to by port_ptr will be set
253 * to NULL. Otherwise, it will be set to point to the newly allocated port.
255 * @return SP_OK upon success, a negative error code otherwise.
257 enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr);
260 * Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
262 void sp_free_port(struct sp_port *port);
265 * List the serial ports available on the system.
267 * The result obtained is an array of pointers to sp_port structures,
268 * terminated by a NULL. The user should allocate a variable of type
269 * "struct sp_port **" and pass a pointer to this to receive the result.
271 * The result should be freed after use by calling sp_free_port_list().
272 * If a port from the list is to be used after freeing the list, it must be
273 * copied first using sp_copy_port().
275 * If any error is returned, the variable pointed to by list_ptr will be set
276 * to NULL. Otherwise, it will be set to point to the newly allocated array.
278 * @return SP_OK upon success, a negative error code otherwise.
280 enum sp_return sp_list_ports(struct sp_port ***list_ptr);
283 * Make a new copy of a sp_port structure.
285 * The user should allocate a variable of type "struct sp_port *" and pass a
286 * pointer to this to receive the result.
288 * The copy should be freed after use by calling sp_free_port().
290 * If any error is returned, the variable pointed to by copy_ptr will be set
291 * to NULL. Otherwise, it will be set to point to the newly allocated copy.
293 * @return SP_OK upon success, a negative error code otherwise.
295 enum sp_return sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
298 * Free a port list obtained from sp_list_ports().
300 * This will also free all the sp_port structures referred to from the list;
301 * any that are to be retained must be copied first using sp_copy_port().
303 void sp_free_port_list(struct sp_port **ports);
307 * @defgroup Ports Opening, closing and querying ports
312 * Open the specified serial port.
314 * @param port Pointer to port structure.
315 * @param flags Flags to use when opening the serial port.
317 * @return SP_OK upon success, a negative error code otherwise.
319 enum sp_return sp_open(struct sp_port *port, enum sp_mode flags);
322 * Close the specified serial port.
324 * @return SP_OK upon success, a negative error code otherwise.
326 enum sp_return sp_close(struct sp_port *port);
329 * Get the name of a port.
331 * The name returned is whatever is normally used to refer to a port on the
332 * current operating system; e.g. for Windows it will usually be a "COMn"
333 * device name, and for Unix it will be a device path beginning with "/dev/".
335 * @param port Pointer to port structure.
337 * @return The port name, or NULL if an invalid port is passed. The name
338 * string is part of the port structure and may not be used after the
339 * port structure has been freed.
341 char *sp_get_port_name(const struct sp_port *port);
344 * Get the operating system handle for a port.
346 * The type of the handle depends on the operating system. On Unix based
347 * systems, the handle is a file descriptor of type "int". On Windows, the
348 * handle is of type "HANDLE". The user should allocate a variable of the
349 * appropriate type and pass a pointer to this to receive the result.
351 * To obtain a valid handle, the port must first be opened by calling
352 * sp_open() using the same port structure.
354 * After the port is closed or the port structure freed, the handle may
355 * no longer be valid.
357 * @warning This feature is provided so that programs may make use of
358 * OS-specific functionality where desired. Doing so obviously
359 * comes at a cost in portability. It also cannot be guaranteed
360 * that direct usage of the OS handle will not conflict with the
361 * library's own usage of the port. Be careful.
363 * @return SP_OK upon success, a negative error code otherwise.
365 enum sp_return sp_get_port_handle(const struct sp_port *port, void *result_ptr);
369 * @defgroup Configuration Setting port parameters
374 * Allocate a port configuration structure.
376 * The user should allocate a variable of type "struct sp_config *" and pass a
377 * pointer to this to receive the result. The variable will be updated to
378 * point to the new configuration structure. The structure is opaque and must
379 * be accessed via the functions provided.
381 * All parameters in the structure will be initialised to special values which
382 * are ignored by sp_set_config().
384 * The structure should be freed after use by calling sp_free_config().
386 * @param config_ptr Pointer to variable to receive result.
388 * @return SP_OK upon success, a negative error code otherwise.
390 enum sp_return sp_new_config(struct sp_port_config **config_ptr);
393 * Free a port configuration structure.
395 * @param config Pointer to configuration structure.
397 void sp_free_config(struct sp_port_config *config);
400 * Get the current configuration of the specified serial port.
402 * The user should allocate a configuration structure using sp_new_config()
403 * and pass this as the config parameter. The configuration structure will
404 * be updated with the port configuration.
406 * Any parameters that are configured with settings not recognised or
407 * supported by libserialport, will be set to special values that are
408 * ignored by sp_set_config().
410 * @return SP_OK upon success, a negative error code otherwise.
412 enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config);
415 * Set the configuration for the specified serial port.
417 * For each parameter in the configuration, there is a special value (usually
418 * -1, but see the documentation for each field). These values will be ignored
419 * and the corresponding setting left unchanged on the port.
421 * @return SP_OK upon success, a negative error code otherwise.
423 enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config);
426 * Set the baud rate for the specified serial port.
428 * @param port Pointer to port structure.
429 * @param baudrate Baud rate in bits per second.
431 * @return SP_OK upon success, a negative error code otherwise.
433 enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate);
436 * Get the baud rate from a port configuration.
438 * The user should allocate a variable of type int and pass a pointer to this
439 * to receive the result.
441 * @param config Pointer to configuration structure.
442 * @param baudrate_ptr Pointer to variable to store result.
444 * @return SP_OK upon success, a negative error code otherwise.
446 enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr);
449 * Set the baud rate in a port configuration.
451 * @param config Pointer to configuration structure.
452 * @param baudrate Baud rate in bits per second, or -1 to retain current setting.
454 * @return SP_OK upon success, a negative error code otherwise.
456 enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate);
459 * Set the data bits for the specified serial port.
461 * @param port Pointer to port structure.
462 * @param bits Number of data bits.
464 * @return SP_OK upon success, a negative error code otherwise.
466 enum sp_return sp_set_bits(struct sp_port *port, int bits);
469 * Get the data bits from a port configuration.
471 * The user should allocate a variable of type int and pass a pointer to this
472 * to receive the result.
474 * @param config Pointer to configuration structure.
475 * @param bits_ptr Pointer to variable to store result.
477 * @return SP_OK upon success, a negative error code otherwise.
479 enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr);
482 * Set the data bits in a port configuration.
484 * @param config Pointer to configuration structure.
485 * @param bits Number of data bits, or -1 to retain current setting.
487 * @return SP_OK upon success, a negative error code otherwise.
489 enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits);
492 * Set the parity setting for the specified serial port.
494 * @param port Pointer to port structure.
495 * @param parity Parity setting.
497 * @return SP_OK upon success, a negative error code otherwise.
499 enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity);
502 * Get the parity setting from a port configuration.
504 * The user should allocate a variable of type enum sp_parity and pass a pointer to this
505 * to receive the result.
507 * @param config Pointer to configuration structure.
508 * @param parity_ptr Pointer to variable to store result.
510 * @return SP_OK upon success, a negative error code otherwise.
512 enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr);
515 * Set the parity setting in a port configuration.
517 * @param config Pointer to configuration structure.
518 * @param parity Parity setting, or -1 to retain current setting.
520 * @return SP_OK upon success, a negative error code otherwise.
522 enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity);
525 * Set the stop bits for the specified serial port.
527 * @param port Pointer to port structure.
528 * @param stopbits Number of stop bits.
530 * @return SP_OK upon success, a negative error code otherwise.
532 enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits);
535 * Get the stop bits from a port configuration.
537 * The user should allocate a variable of type int and pass a pointer to this
538 * to receive the result.
540 * @param config Pointer to configuration structure.
541 * @param stopbits_ptr Pointer to variable to store result.
543 * @return SP_OK upon success, a negative error code otherwise.
545 enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr);
548 * Set the stop bits in a port configuration.
550 * @param config Pointer to configuration structure.
551 * @param stopbits Number of stop bits, or -1 to retain current setting.
553 * @return SP_OK upon success, a negative error code otherwise.
555 enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits);
558 * Set the RTS pin behaviour for the specified serial port.
560 * @param port Pointer to port structure.
561 * @param rts RTS pin mode.
563 * @return SP_OK upon success, a negative error code otherwise.
565 enum sp_return sp_set_rts(struct sp_port *port, enum sp_rts rts);
568 * Get the RTS pin behaviour from a port configuration.
570 * The user should allocate a variable of type enum sp_rts and pass a pointer to this
571 * to receive the result.
573 * @param config Pointer to configuration structure.
574 * @param rts_ptr Pointer to variable to store result.
576 * @return SP_OK upon success, a negative error code otherwise.
578 enum sp_return sp_get_config_rts(const struct sp_port_config *config, enum sp_rts *rts_ptr);
581 * Set the RTS pin behaviour in a port configuration.
583 * @param config Pointer to configuration structure.
584 * @param rts RTS pin mode, or -1 to retain current setting.
586 * @return SP_OK upon success, a negative error code otherwise.
588 enum sp_return sp_set_config_rts(struct sp_port_config *config, enum sp_rts rts);
591 * Set the CTS pin behaviour for the specified serial port.
593 * @param port Pointer to port structure.
594 * @param cts CTS pin mode.
596 * @return SP_OK upon success, a negative error code otherwise.
598 enum sp_return sp_set_cts(struct sp_port *port, enum sp_cts cts);
601 * Get the CTS pin behaviour from a port configuration.
603 * The user should allocate a variable of type enum sp_cts and pass a pointer to this
604 * to receive the result.
606 * @param config Pointer to configuration structure.
607 * @param cts_ptr Pointer to variable to store result.
609 * @return SP_OK upon success, a negative error code otherwise.
611 enum sp_return sp_get_config_cts(const struct sp_port_config *config, enum sp_cts *cts_ptr);
614 * Set the CTS pin behaviour in a port configuration.
616 * @param config Pointer to configuration structure.
617 * @param cts CTS pin mode, or -1 to retain current setting.
619 * @return SP_OK upon success, a negative error code otherwise.
621 enum sp_return sp_set_config_cts(struct sp_port_config *config, enum sp_cts cts);
624 * Set the DTR pin behaviour for the specified serial port.
626 * @param port Pointer to port structure.
627 * @param dtr DTR pin mode.
629 * @return SP_OK upon success, a negative error code otherwise.
631 enum sp_return sp_set_dtr(struct sp_port *port, enum sp_dtr dtr);
634 * Get the DTR pin behaviour from a port configuration.
636 * The user should allocate a variable of type enum sp_dtr and pass a pointer to this
637 * to receive the result.
639 * @param config Pointer to configuration structure.
640 * @param dtr_ptr Pointer to variable to store result.
642 * @return SP_OK upon success, a negative error code otherwise.
644 enum sp_return sp_get_config_dtr(const struct sp_port_config *config, enum sp_dtr *dtr_ptr);
647 * Set the DTR pin behaviour in a port configuration.
649 * @param config Pointer to configuration structure.
650 * @param dtr DTR pin mode, or -1 to retain current setting.
652 * @return SP_OK upon success, a negative error code otherwise.
654 enum sp_return sp_set_config_dtr(struct sp_port_config *config, enum sp_dtr dtr);
657 * Set the DSR pin behaviour for the specified serial port.
659 * @param port Pointer to port structure.
660 * @param dsr DSR pin mode.
662 * @return SP_OK upon success, a negative error code otherwise.
664 enum sp_return sp_set_dsr(struct sp_port *port, enum sp_dsr dsr);
667 * Get the DSR pin behaviour from a port configuration.
669 * The user should allocate a variable of type enum sp_dsr and pass a pointer to this
670 * to receive the result.
672 * @param config Pointer to configuration structure.
673 * @param dsr_ptr Pointer to variable to store result.
675 * @return SP_OK upon success, a negative error code otherwise.
677 enum sp_return sp_get_config_dsr(const struct sp_port_config *config, enum sp_dsr *dsr_ptr);
680 * Set the DSR pin behaviour in a port configuration.
682 * @param config Pointer to configuration structure.
683 * @param dsr DSR pin mode, or -1 to retain current setting.
685 * @return SP_OK upon success, a negative error code otherwise.
687 enum sp_return sp_set_config_dsr(struct sp_port_config *config, enum sp_dsr dsr);
690 * Set the XON/XOFF configuration for the specified serial port.
692 * @param port Pointer to port structure.
693 * @param xon_xoff XON/XOFF mode.
695 * @return SP_OK upon success, a negative error code otherwise.
697 enum sp_return sp_set_xon_xoff(struct sp_port *port, enum sp_xonxoff xon_xoff);
700 * Get the XON/XOFF configuration from a port configuration.
702 * The user should allocate a variable of type enum sp_xonxoff and pass a pointer to this
703 * to receive the result.
705 * @param config Pointer to configuration structure.
706 * @param xon_xoff_ptr Pointer to variable to store result.
708 * @return SP_OK upon success, a negative error code otherwise.
710 enum sp_return sp_get_config_xon_xoff(const struct sp_port_config *config, enum sp_xonxoff *xon_xoff_ptr);
713 * Set the XON/XOFF configuration in a port configuration.
715 * @param config Pointer to configuration structure.
716 * @param xon_xoff XON/XOFF mode, or -1 to retain current setting.
718 * @return SP_OK upon success, a negative error code otherwise.
720 enum sp_return sp_set_config_xon_xoff(struct sp_port_config *config, enum sp_xonxoff xon_xoff);
723 * Set the flow control type in a port configuration.
725 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
726 * XON/XOFF settings as necessary for the specified flow control
727 * type. For more fine-grained control of these settings, use their
728 * individual configuration functions.
730 * @param config Pointer to configuration structure.
731 * @param flowcontrol Flow control setting to use.
733 * @return SP_OK upon success, a negative error code otherwise.
735 enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol);
738 * Set the flow control type for the specified serial port.
740 * This function is a wrapper that sets the RTS, CTS, DTR, DSR and
741 * XON/XOFF settings as necessary for the specified flow control
742 * type. For more fine-grained control of these settings, use their
743 * individual configuration functions.
745 * @param port Pointer to port structure.
746 * @param flowcontrol Flow control setting to use.
748 * @return SP_OK upon success, a negative error code otherwise.
750 enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol);
754 * @defgroup Data Reading, writing, and flushing data
759 * Read bytes from the specified serial port, blocking until complete.
761 * @warning If your program runs on Unix, defines its own signal handlers, and
762 * needs to abort blocking reads when these are called, then you
763 * should not use this function. It repeats system calls that return
764 * with EINTR. To be able to abort a read from a signal handler, you
765 * should implement your own blocking read using sp_nonblocking_read()
766 * together with a blocking method that makes sense for your program.
767 * E.g. you can obtain the file descriptor for an open port using
768 * sp_get_port_handle() and use this to call select() or pselect(),
769 * with appropriate arrangements to return if a signal is received.
771 * @param port Pointer to port structure.
772 * @param buf Buffer in which to store the bytes read.
773 * @param count Requested number of bytes to read.
774 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
776 * @return The number of bytes read on success, or a negative error code. If
777 * the number of bytes returned is less than that requested, the
778 * timeout was reached before the requested number of bytes was
779 * available. If timeout is zero, the function will always return
780 * either the requested number of bytes or a negative error code.
782 enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout);
785 * Read bytes from the specified serial port, without blocking.
787 * @param port Pointer to port structure.
788 * @param buf Buffer in which to store the bytes read.
789 * @param count Maximum number of bytes to read.
791 * @return The number of bytes read on success, or a negative error code. The
792 * number of bytes returned may be any number from zero to the maximum
793 * that was requested.
795 enum sp_return sp_nonblocking_read(struct sp_port *port, void *buf, size_t count);
798 * Write bytes to the specified serial port, blocking until complete.
800 * Note that this function only ensures that the accepted bytes have been
801 * written to the OS; they may be held in driver or hardware buffers and not
802 * yet physically transmitted. To check whether all written bytes have actually
803 * been transmitted, use the sp_output_waiting() function. To wait until all
804 * written bytes have actually been transmitted, use the sp_drain() function.
806 * @warning If your program runs on Unix, defines its own signal handlers, and
807 * needs to abort blocking writes when these are called, then you
808 * should not use this function. It repeats system calls that return
809 * with EINTR. To be able to abort a write from a signal handler, you
810 * should implement your own blocking write using sp_nonblocking_write()
811 * together with a blocking method that makes sense for your program.
812 * E.g. you can obtain the file descriptor for an open port using
813 * sp_get_port_handle() and use this to call select() or pselect(),
814 * with appropriate arrangements to return if a signal is received.
816 * @param port Pointer to port structure.
817 * @param buf Buffer containing the bytes to write.
818 * @param count Requested number of bytes to write.
819 * @param timeout Timeout in milliseconds, or zero to wait indefinitely.
821 * @return The number of bytes written on success, or a negative error code.
822 * If the number of bytes returned is less than that requested, the
823 * timeout was reached before the requested number of bytes was
824 * written. If timeout is zero, the function will always return
825 * either the requested number of bytes or a negative error code. In
826 * the event of an error there is no way to determine how many bytes
827 * were sent before the error occured.
829 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout);
832 * Write bytes to the specified serial port, without blocking.
834 * Note that this function only ensures that the accepted bytes have been
835 * written to the OS; they may be held in driver or hardware buffers and not
836 * yet physically transmitted. To check whether all written bytes have actually
837 * been transmitted, use the sp_output_waiting() function. To wait until all
838 * written bytes have actually been transmitted, use the sp_drain() function.
840 * @param port Pointer to port structure.
841 * @param buf Buffer containing the bytes to write.
842 * @param count Maximum number of bytes to write.
844 * @return The number of bytes written on success, or a negative error code.
845 * The number of bytes returned may be any number from zero to the
846 * maximum that was requested.
848 enum sp_return sp_nonblocking_write(struct sp_port *port, const void *buf, size_t count);
851 * Gets the number of bytes waiting in the input buffer.
853 * @param port Pointer to port structure.
855 * @return Number of bytes waiting on success, a negative error code otherwise.
857 enum sp_return sp_input_waiting(struct sp_port *port);
860 * Gets the number of bytes waiting in the output buffer.
862 * @param port Pointer to port structure.
864 * @return Number of bytes waiting on success, a negative error code otherwise.
866 enum sp_return sp_output_waiting(struct sp_port *port);
869 * Flush serial port buffers. Data in the selected buffer(s) is discarded.
871 * @param port Pointer to port structure.
872 * @param buffers Which buffer(s) to flush.
874 * @return SP_OK upon success, a negative error code otherwise.
876 enum sp_return sp_flush(struct sp_port *port, enum sp_buffer buffers);
879 * Wait for buffered data to be transmitted.
881 * @warning If your program runs on Unix, defines its own signal handlers, and
882 * needs to abort draining the output buffer when when these are
883 * called, then you should not use this function. It repeats system
884 * calls that return with EINTR. To be able to abort a drain from a
885 * signal handler, you would need to implement your own blocking
886 * drain by polling the result of sp_output_waiting().
888 * @param port Pointer to port structure.
890 * @return SP_OK upon success, a negative error code otherwise.
892 enum sp_return sp_drain(struct sp_port *port);
896 * @defgroup Signals Port signalling operations
901 * Gets the status of the control signals for the specified port.
903 * The user should allocate a variable of type "enum sp_signal" and pass a
904 * pointer to this variable to receive the result. The result is a bitmask
905 * in which individual signals can be checked by bitwise OR with values of
906 * the sp_signal enum.
908 * @param port Pointer to port structure.
909 * @param signals Pointer to variable to receive result.
911 * @return SP_OK upon success, a negative error code otherwise.
913 enum sp_return sp_get_signals(struct sp_port *port, enum sp_signal *signals);
916 * Put the port transmit line into the break state.
918 * @param port Pointer to port structure.
920 * @return SP_OK upon success, a negative error code otherwise.
922 enum sp_return sp_start_break(struct sp_port *port);
925 * Take the port transmit line out of the break state.
927 * @param port Pointer to port structure.
929 * @return SP_OK upon success, a negative error code otherwise.
931 enum sp_return sp_end_break(struct sp_port *port);
935 * @defgroup Errors Obtaining error information
940 * Get the error code for a failed operation.
942 * In order to obtain the correct result, this function should be called
943 * straight after the failure, before executing any other system operations.
945 * @return The system's numeric code for the error that caused the last
948 int sp_last_error_code(void);
951 * Get the error message for a failed operation.
953 * In order to obtain the correct result, this function should be called
954 * straight after the failure, before executing other system operations.
956 * @return The system's message for the error that caused the last
957 * operation to fail. This string may be allocated by the function,
958 * and should be freed after use by calling sp_free_error_message().
960 char *sp_last_error_message(void);
963 * Free an error message returned by sp_last_error_message().
965 void sp_free_error_message(char *message);
968 * Set the handler function for library debugging messages.
970 * Debugging messages are generated by the library during each operation,
971 * to help in diagnosing problems. The handler will be called for each
972 * message. The handler can be set to NULL to ignore all debug messages.
974 * The handler function should accept a format string and variable length
975 * argument list, in the same manner as e.g. printf().
977 * The default handler is sp_default_debug_handler().
979 void sp_set_debug_handler(void (*handler)(const char *format, ...));
982 * Default handler function for library debugging messages.
984 * This function prints debug messages to the standard error stream if the
985 * environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
988 void sp_default_debug_handler(const char *format, ...);