-----------------------------------------------------------------
+-------------------------------------------------------------------------------
libserialport: cross-platform library for accessing serial ports
-----------------------------------------------------------------
+-------------------------------------------------------------------------------
libserialport is a minimal library written in C that is intended to take care
of the OS-specific details when writing software that uses serial ports.
The API is simple, and designed to be a minimal wrapper around the serial port
support in each OS.
-Most functions take a pointer to a struct sp_port, which represents an serial
-port. This structure is obtained from the array returned by sp_list_ports().
+Most functions take a pointer to a struct sp_port, which represents a serial
+port. These structures are always allocated and freed by the library, using the
+functions in the 'Enumeration' section below.
All functions can return only three possible error values. SP_ERR_ARG indicates
the function was called with invalid arguments. SP_ERR_FAIL indicates that the
error code or message is that provided by the OS; libserialport does not define
any error codes or messages of its own.
-Functions calls that succeed return SP_OK, which is equal to zero, or where
+Function calls that succeed return SP_OK, which is equal to zero, or where
otherwise documented a positive value.
The available functions are as follows:
Obtains a pointer to a new sp_port structure representing the named port. The
user should allocate a variable of type "struct sp_port *" and pass a pointer
to this to receive the result.
+
+ The result should be freed after use by calling sp_free_port().
- Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation failure,
- or SP_ERR_ARG if an invalid pointer is passed. If any error is returned,
- the value pointed to by port_ptr will be set to NULL.
+ Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
+ failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
+ is returned, the variable pointed to by port_ptr will be set to NULL.
+ Otherwise, it will be set to point to the newly allocated port.
void sp_free_port(struct sp_port *port);
allocate a variable of type "struct sp_port **" and pass a pointer to this to
receive the result.
- The result should be freed after use by calling sp_free_port_list().
+ The result should be freed after use by calling sp_free_port_list(). If a port
+ from the list is to be used after freeing the list, it must be copied first
+ using sp_copy_port().
+
+ Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
+ failure, or SP_ERR_ARG if an invalid pointer is passed. If any error
+ is returned, the variable pointed to by list_ptr will be set to NULL.
+ Otherwise, it will be set to point to the newly allocated array.
+
+int sp_copy_port(const struct sp_port *port, struct sp_port **copy_ptr);
+
+ Makes a new copy of a sp_port structure. The user should allocate a variable
+ of type "struct sp_port *" and pass a pointer to this to receive the result.
- Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation failure,
- or SP_ERR_ARG if an invalid pointer is passed. If any error is returned,
- the value pointed to by list_ptr will be set to NULL.
+ The copy should be freed after use by calling sp_free_port().
+
+ Returns: SP_OK on success, SP_ERR_MEM on allocation failure, or SP_ERR_ARG
+ if an invalid port or pointer is passed. If any error is returned,
+ the variable pointed to by copy_ptr will be set to NULL. Otherwise,
+ it will be set to point to the newly allocated copy.
void sp_free_port_list(struct sp_port **list);
- Frees a port list obtained from sp_list_ports().
+ Frees a port list obtained from sp_list_ports(). This will also free all
+ the sp_port structures referred to from the list; any that are to be retained
+ must be copied first using sp_copy_port().
Opening and closing ports
-------------------------
flags: Flags to use when opening the serial port. Possible
flags are: SP_MODE_RDWR, SP_MODE_RDONLY, and SP_MODE_NONBLOCK.
- Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG
- if an invalid port is passed.
+ Returns: SP_OK on success, SP_ERR_FAIL on failure, SP_ERR_MEM on allocation
+ failure, or SP_ERR_ARG if an invalid port is passed.
int sp_close(struct sp_port *port);
int sp_read(struct sp_port *port, const void *buf, size_t count)
- Reads a number of bytes from the specified serial port.
+ Reads bytes from the specified serial port. Note that this function may
+ return after reading less than the specified number of bytes; it is the
+ user's responsibility to iterate as necessary in this case.
Parameters:
port: Pointer to port structure.
buf: Buffer in which to store the bytes read.
- count: Number of bytes to read.
+ count: Maximum number of bytes to read.
Returns: The number of bytes read, SP_ERR_FAIL on failure,
or SP_ERR_ARG for invalid arguments.
int sp_write(struct sp_port *port, const void *buf, size_t count)
- Writes a number of bytes to the specified serial port.
+ Write bytes to the specified serial port. Note that this function may
+ return after writing less than the specified number of bytes; it is the
+ user's responsibility to iterate as necessary in this case.
Parameters:
port: Pointer to port structure.
buf: Buffer containing the bytes to write.
- count: Number of bytes to write.
+ count: Maximum number of bytes to write.
Returns: The number of bytes written, SP_ERR_FAIL on failure,
or SP_ERR_ARG for invalid arguments.