X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=README;h=49e7a7e78ffc960fc893bb9b71ae5ee473524b05;hb=c6754b4517d7c9764a887ee3078a3693bfa5c571;hp=5958d8925d32e7f56868d1c46df1a1def9690684;hpb=0a16d4def93aa348fdd480222168d8e66afd1784;p=libserialport.git diff --git a/README b/README index 5958d89..49e7a7e 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ ----------------------------------------------------------------- +------------------------------------------------------------------------------- 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. @@ -61,20 +61,21 @@ API 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 open -port. This structure should be allocated by the user and is populated by -sp_open(). It can be freed safely after sp_close(). +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 two possible error values. SP_ERR_ARG indicates +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 -OS reported a failure. Both these error values are negative. +OS reported a failure. SP_ERR_MEM indicates that a memory allocation failed. +All of these error values are negative. When SP_ERR_FAIL is returned, an error code or string description of the error can be obtained by calling sp_last_error_code() or sp_last_error_message(). 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: @@ -82,32 +83,72 @@ The available functions are as follows: Enumeration ----------- -char **sp_list_ports(); +int sp_get_port_by_name(const char *portname, struct sp_port **port_ptr); - Lists the serial ports available on the system. The value returned is an array - of port names as C strings, terminated by a NULL. It should be freed after use - by calling sp_free_port_list(). + 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. -void sp_free_port_list(char **list); + 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 variable pointed to by port_ptr will be set to NULL. + Otherwise, it will be set to point to the newly allocated port. - Frees the data structure returned by sp_list_ports(). +void sp_free_port(struct sp_port *port); + + Frees a port structure obtained from sp_get_port_by_name(). + +int sp_list_ports(struct sp_port ***list_ptr); + + Lists the serial ports available on the system. The result obtained is an + array of pointers to sp_port structures, terminated by a NULL. 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_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. + + 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(). 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 ------------------------- -int sp_open(struct sp_port *port, char *portname, int flags); +int sp_open(struct sp_port *port, int flags); Opens the specified serial port. Parameters: - port: Pointer to empty port structure, allocated by caller. - portname: Name of port to open. + port: Pointer to port structure. 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 or name 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); @@ -119,22 +160,105 @@ int sp_close(struct sp_port *port); Setting port parameters ----------------------- -int sp_set_params(struct sp_port *port, int baudrate, - int bits, int parity, int stopbits, - int flowcontrol, int rts, int dtr); +int sp_set_baudrate(struct sp_port *port, int baudrate) + + Sets the baud rate for the specified serial port. + + Parameters: + + port: Pointer to port structure. + baud: Baud rate in bits per second. + + Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + for invalid arguments. + +int sp_set_bits(struct sp_port *port, int bits) + + Sets the number of data bits for the specified serial port. + + Parameters: + + port: Pointer to port structure. + bits: Number of data bits to use. Valid values are 5 to 8. + + Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + for invalid arguments. + +int sp_set_parity(struct sp_port *port, int parity) - Sets serial parameters for the specified serial port. + Sets the parity for the specified serial port. + + Parameters: + + port: Pointer to port structure. + parity: Parity setting to use. + (SP_PARITY_NONE, SP_PARITY_EVEN or SP_PARITY_ODD) + + Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + for invalid arguments. + +int sp_set_stopbits(struct sp_port *port, int stopbits) + + Sets the number of stop bits for the specified port. + + Parameters: + + port: Pointer to port structure. + stopbits: Number of stop bits to use (1 or 2). + + Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + for invalid arguments. + +int sp_set_flowcontrol(struct sp_port *port, int flowcontrol) + + Sets the flow control type for the specified serial port. + + This function is a wrapper that sets the RTS, CTS, DTR, DSR and + XON/XOFF settings as necessary for the specified flow control + type. For more fine-grained control of these settings, use their + individual configuration functions or the sp_set_config() function. Parameters: port: Pointer to port structure. - baudrate: Baud rate to set. - bits: Number of data bits to use. - parity: Parity setting to use - (SP_PARITY_NONE, SP_PARITY_EVEN or SP_PARITY_ODD) - stopbits: Number of stop bits to use (1 or 2). - flowcontrol: Flow control setting to use - (SP_FLOW_NONE, SP_FLOW_HARDWARE or SP_FLOW_SOFTWARE) + flowcontrol: Flow control setting to use. Valid settings are: + + SP_FLOWCONTROL_NONE: No flow control. + SP_FLOWCONTROL_XONXOFF: Software flow control using XON/XOFF characters. + SP_FLOWCONTROL_RTSCTS: Hardware flow control using RTS/CTS signals. + SP_FLOWCONTROL_DTRDSR: Hardware flow control using DTR/DSR signals. + + Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG + for invalid arguments. + +int sp_set_config(struct sp_port *port, struct sp_port_config *config) + + Sets all parameters for the specified serial port. + + The user should populate a struct sp_port_config, then pass a pointer to it + as the config parameter. + + The fields of sp_port_config are: + + int baudrate: Baud rate in bits per second. + int bits: Number of data bits to use. Valid values are 5 to 8. + int parity: Parity setting to use. + (SP_PARITY_NONE, SP_PARITY_EVEN or SP_PARITY_ODD) + int stopbits: Number of stop bits to use (1 or 2). + int rts: RTS pin mode. + (SP_RTS_ON, SP_RTS_OFF or SP_RTS_FLOW_CONTROL) + int cts: CTS pin mode. + (SP_CTS_IGNORE or SP_CTS_FLOW_CONTROL) + int dtr: DTR pin mode. + (SP_DTR_ON, SP_DTR_OFF or SP_DTR_FLOW_CONTROL) + int dsr: DSR pin mode. + (SP_DSR_IGNORE or SP_DSR_FLOW_CONTROL) + int xon_xoff: XON/XOFF flow control mode. + (SP_XONXOFF_DISABLED, SP_XONXOFF_IN, + SP_XONXOFF_OUT or SP_XONXOFF_INOUT) + + To retain the current value of any setting, set the field to to a + negative value. Returns: SP_OK on success, SP_ERR_FAIL on failure, or SP_ERR_ARG for invalid arguments. @@ -144,26 +268,30 @@ Reading, writing and flushing data 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.