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;
}
}