Sending and receiving data.
Sending and receiving data.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc < 2 || argc > 3) {
printf("Usage: %s <port 1> [<port 2>]\n", argv[0]);
return -1;
}
int num_ports = argc - 1;
char **port_names = argv + 1;
for (int i = 0; i < num_ports; i++) {
printf("Looking for port %s.\n", port_names[i]);
printf("Opening port.\n");
printf("Setting port to 9600 8N1, no flow control.\n");
}
for (int tx = 0; tx < num_ports; tx++) {
int rx = num_ports == 1 ? 0 : ((tx == 0) ? 1 : 0);
struct sp_port *tx_port = ports[tx];
struct sp_port *rx_port = ports[rx];
char *data = "Hello!";
int size = strlen(data);
unsigned int timeout = 1000;
int result;
printf("Sending '%s' (%d bytes) on port %s.\n",
if (result == size)
printf("Sent %d bytes successfully.\n", size);
else
printf("Timed out, %d/%d bytes sent.\n", result, size);
char *buf = malloc(size + 1);
printf("Receiving %d bytes on port %s.\n",
if (result == size)
printf("Received %d bytes successfully.\n", size);
else
printf("Timed out, %d/%d bytes received.\n", result, size);
buf[result] = '\0';
printf("Received '%s'.\n", buf);
free(buf);
}
for (int i = 0; i < num_ports; i++) {
}
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;
}
}
@ SP_PARITY_NONE
No parity.
@ SP_MODE_READ_WRITE
Open port for read and write access.
@ SP_FLOWCONTROL_NONE
No flow control.
@ 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.
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_stopbits(struct sp_port *port, int stopbits)
Set the stop bits 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_set_baudrate(struct sp_port *port, int baudrate)
Set the baud rate for the specified serial port.
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_parity(struct sp_port *port, enum sp_parity parity)
Set the parity setting for the specified serial port.
enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, unsigned int timeout_ms)
Read bytes from the specified serial port, blocking until complete.
enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout_ms)
Write bytes to the specified serial port, blocking until complete.
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.