2023-07-29 22:32:44 +03:00
|
|
|
#include <libserialport.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2023-07-29 23:15:23 +03:00
|
|
|
#include <stdbool.h>
|
2023-07-30 00:52:44 +03:00
|
|
|
#include <signal.h>
|
2023-07-29 22:32:44 +03:00
|
|
|
|
|
|
|
/* Helper function for error handling. */
|
|
|
|
int check(enum sp_return result);
|
2023-07-30 00:52:44 +03:00
|
|
|
void handle_sigint(int sig);
|
|
|
|
|
|
|
|
bool INTERRUPT = false;
|
2023-07-29 22:32:44 +03:00
|
|
|
|
2023-07-29 23:15:23 +03:00
|
|
|
int main(int argc, char **argv) {
|
|
|
|
struct sp_port *serial_port;
|
2023-07-29 23:45:51 +03:00
|
|
|
char *port_name;
|
2023-07-29 23:15:23 +03:00
|
|
|
|
2023-07-29 22:53:01 +03:00
|
|
|
const int size = 256;
|
2023-07-29 22:32:44 +03:00
|
|
|
char *buffer = malloc(size + 1);
|
2023-07-29 23:15:23 +03:00
|
|
|
|
2023-07-29 22:32:44 +03:00
|
|
|
const unsigned int timeout = 1000;
|
2023-07-29 23:15:23 +03:00
|
|
|
int result;
|
|
|
|
|
2023-07-29 23:45:51 +03:00
|
|
|
/* Get the port name from the command line. */
|
2023-07-29 23:15:23 +03:00
|
|
|
if (argc != 2) {
|
2023-07-29 23:45:51 +03:00
|
|
|
printf("Usage: %s <port>\n\n", argv[0]);
|
|
|
|
|
|
|
|
struct sp_port **port_list;
|
|
|
|
enum sp_return result = sp_list_ports(&port_list);
|
|
|
|
|
|
|
|
/* Getting the available ports. */
|
|
|
|
if (result != SP_OK) {
|
|
|
|
puts("Getting available ports failed!");
|
|
|
|
} else {
|
|
|
|
puts("Available ports:");
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; port_list[i] != NULL; i++) {
|
|
|
|
/* Get the name of the port. */
|
|
|
|
struct sp_port *port = port_list[i];
|
|
|
|
char *port_name = sp_get_port_name(port);
|
|
|
|
|
|
|
|
printf(" * %s\n", port_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nAvailable %d ports.\n", i);
|
|
|
|
|
|
|
|
sp_free_port_list(port_list);
|
|
|
|
}
|
|
|
|
|
2023-07-29 23:15:23 +03:00
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
port_name = argv[1];
|
|
|
|
}
|
2023-07-29 22:32:44 +03:00
|
|
|
|
2023-07-29 23:15:23 +03:00
|
|
|
printf("Connecting to '%s'...\n", port_name);
|
2023-07-29 22:32:44 +03:00
|
|
|
check(sp_get_port_by_name(port_name, &serial_port));
|
|
|
|
check(sp_open(serial_port, SP_MODE_READ_WRITE));
|
|
|
|
|
|
|
|
check(sp_set_baudrate(serial_port, 9600));
|
|
|
|
check(sp_set_bits(serial_port, 8));
|
|
|
|
check(sp_set_parity(serial_port, SP_PARITY_NONE));
|
|
|
|
check(sp_set_stopbits(serial_port, 1));
|
|
|
|
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("Connected.");
|
2023-07-29 23:15:23 +03:00
|
|
|
|
2023-07-30 00:52:44 +03:00
|
|
|
signal(SIGINT, handle_sigint);
|
|
|
|
|
2023-07-30 00:55:11 +03:00
|
|
|
FILE *output_file;
|
|
|
|
output_file = fopen("./abc.txt", "w");
|
|
|
|
|
2023-07-29 23:45:51 +03:00
|
|
|
/* Reading lines from serial port. */
|
|
|
|
bool reading = true;
|
2023-07-30 00:52:44 +03:00
|
|
|
while (reading && !INTERRUPT) {
|
2023-07-29 22:53:01 +03:00
|
|
|
int pos = 0;
|
|
|
|
|
2023-07-29 23:45:51 +03:00
|
|
|
/* Character-by-character reading. */
|
2023-07-29 22:53:01 +03:00
|
|
|
while (pos < size) {
|
|
|
|
result = check(sp_blocking_read(serial_port, buffer + pos, 1, timeout));
|
2023-07-29 22:32:44 +03:00
|
|
|
|
2023-07-29 22:53:01 +03:00
|
|
|
if (result == -1) {
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("Error reading from serial port");
|
2023-07-29 23:45:51 +03:00
|
|
|
|
|
|
|
reading = false;
|
2023-07-29 22:53:01 +03:00
|
|
|
break;
|
|
|
|
} else if (result == 0) {
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("No more data");
|
2023-07-29 22:53:01 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (buffer[pos] == '\n') {
|
|
|
|
buffer[pos] = '\0';
|
|
|
|
break;
|
2023-07-30 01:09:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
2023-07-29 22:53:01 +03:00
|
|
|
}
|
2023-07-29 22:32:44 +03:00
|
|
|
}
|
2023-07-30 00:49:51 +03:00
|
|
|
|
|
|
|
puts(buffer);
|
2023-07-30 00:55:11 +03:00
|
|
|
fputs(buffer, output_file);
|
2023-07-29 22:32:44 +03:00
|
|
|
}
|
|
|
|
|
2023-07-30 00:55:11 +03:00
|
|
|
fclose(output_file);
|
2023-07-29 22:32:44 +03:00
|
|
|
free(buffer);
|
2023-07-29 22:53:01 +03:00
|
|
|
|
|
|
|
return 0;
|
2023-07-29 22:32:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function for error handling. */
|
|
|
|
int check(enum sp_return result) {
|
|
|
|
char *error_message;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case SP_ERR_ARG:
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("Error: Invalid argument.");
|
2023-07-29 22:32:44 +03:00
|
|
|
abort();
|
|
|
|
case SP_ERR_FAIL:
|
|
|
|
error_message = sp_last_error_message();
|
|
|
|
printf("Error: Failed: %s\n", error_message);
|
|
|
|
sp_free_error_message(error_message);
|
|
|
|
abort();
|
|
|
|
case SP_ERR_SUPP:
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("Error: Not supported.");
|
2023-07-29 22:32:44 +03:00
|
|
|
abort();
|
|
|
|
case SP_ERR_MEM:
|
2023-07-29 23:20:53 +03:00
|
|
|
puts("Error: Couldn't allocate memory.");
|
2023-07-29 22:32:44 +03:00
|
|
|
abort();
|
|
|
|
case SP_OK:
|
|
|
|
default:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 00:52:44 +03:00
|
|
|
|
|
|
|
void handle_sigint(int sig) { INTERRUPT = true; }
|