Accessing configuration settings of a port.
Accessing configuration settings of a port.
#include <stdio.h>
#include <stdlib.h>
const char *parity_name(
enum sp_parity parity);
int main(int argc, char **argv)
{
if (argc != 2) {
printf("Usage: %s <port name>\n", argv[0]);
return -1;
}
char *port_name = argv[1];
printf("Looking for port %s.\n", port_name);
printf("Opening port.\n");
printf("Setting port to 115200 8N1, no flow control.\n");
int baudrate, bits, stopbits;
printf("Baudrate: %d, data bits: %d, parity: %s, stop bits: %d\n",
baudrate, bits, parity_name(parity), stopbits);
printf("Creating new config for 9600 7E2, XON/XOFF flow control.\n");
printf("Applying new configuration.\n");
printf("Setting port back to previous config.\n");
return 0;
}
{
char *error_message;
switch (result) {
printf("Error: Invalid argument.\n");
abort();
printf("Error: Failed: %s\n", error_message);
abort();
printf("Error: Not supported.\n");
abort();
printf("Error: Couldn't allocate memory.\n");
abort();
default:
return result;
}
}
const char *parity_name(
enum sp_parity parity)
{
switch (parity) {
return "(Invalid)";
return "None";
return "Odd";
return "Even";
return "Mark";
return "Space";
default:
return NULL;
}
}
sp_parity
Parity settings.
@ SP_PARITY_INVALID
Special value to indicate setting should be left alone.
@ SP_PARITY_EVEN
Even parity.
@ SP_PARITY_MARK
Mark parity.
@ SP_PARITY_ODD
Odd parity.
@ SP_PARITY_NONE
No parity.
@ SP_PARITY_SPACE
Space parity.
@ SP_MODE_READ_WRITE
Open port for read and write access.
@ SP_FLOWCONTROL_NONE
No flow control.
@ SP_FLOWCONTROL_XONXOFF
Software flow control using XON/XOFF characters.
@ SP_ERR_SUPP
The requested operation is not supported by this system or device.
@ SP_ERR_FAIL
A system error occurred while executing the operation.
@ SP_ERR_MEM
A memory allocation failed while executing the operation.
@ SP_OK
Operation completed successfully.
@ SP_ERR_ARG
Invalid arguments were passed to the function.
void sp_free_port(struct sp_port *port)
Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
Obtain a pointer to a new sp_port structure representing the named port.
enum sp_return sp_close(struct sp_port *port)
Close the specified serial port.
char * sp_get_port_description(const struct sp_port *port)
Get a description for a port, to present to end user.
enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
Open the specified serial port.
char * sp_get_port_name(const struct sp_port *port)
Get the name of a port.
enum sp_return sp_set_config_parity(struct sp_port_config *config, enum sp_parity parity)
Set the parity setting in a port configuration.
void sp_free_config(struct sp_port_config *config)
Free a port configuration structure.
enum sp_return sp_get_config_bits(const struct sp_port_config *config, int *bits_ptr)
Get the data bits from a port configuration.
enum sp_return sp_get_config_stopbits(const struct sp_port_config *config, int *stopbits_ptr)
Get the stop bits from a port configuration.
enum sp_return sp_set_stopbits(struct sp_port *port, int stopbits)
Set the stop bits for the specified serial port.
enum sp_return sp_set_config(struct sp_port *port, const struct sp_port_config *config)
Set the configuration for the specified serial port.
enum sp_return sp_set_bits(struct sp_port *port, int bits)
Set the data bits for the specified serial port.
enum sp_return sp_new_config(struct sp_port_config **config_ptr)
Allocate a port configuration structure.
enum sp_return sp_get_config_baudrate(const struct sp_port_config *config, int *baudrate_ptr)
Get the baud rate from a port configuration.
enum sp_return sp_set_config_flowcontrol(struct sp_port_config *config, enum sp_flowcontrol flowcontrol)
Set the flow control type in a port configuration.
enum sp_return sp_set_baudrate(struct sp_port *port, int baudrate)
Set the baud rate for the specified serial port.
enum sp_return sp_get_config(struct sp_port *port, struct sp_port_config *config)
Get the current configuration of the specified serial port.
enum sp_return sp_set_config_bits(struct sp_port_config *config, int bits)
Set the data bits in a port configuration.
enum sp_return sp_get_config_parity(const struct sp_port_config *config, enum sp_parity *parity_ptr)
Get the parity setting from a port configuration.
enum sp_return sp_set_flowcontrol(struct sp_port *port, enum sp_flowcontrol flowcontrol)
Set the flow control type for the specified serial port.
enum sp_return sp_set_config_stopbits(struct sp_port_config *config, int stopbits)
Set the stop bits in a port configuration.
enum sp_return sp_set_parity(struct sp_port *port, enum sp_parity parity)
Set the parity setting for the specified serial port.
enum sp_return sp_set_config_baudrate(struct sp_port_config *config, int baudrate)
Set the baud rate in a port configuration.
char * sp_last_error_message(void)
Get the error message for a failed operation.
void sp_free_error_message(char *message)
Free an error message returned by sp_last_error_message().
An opaque structure representing a serial port.
An opaque structure representing the configuration for a serial port.