Compare commits
2 Commits
afd878407e
...
55528a6363
Author | SHA1 | Date | |
---|---|---|---|
55528a6363 | |||
053986cf00 |
@ -1,36 +1,45 @@
|
|||||||
#include <libserialport.h>
|
#include <libserialport.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
/* Helper function for error handling. */
|
/* Helper function for error handling. */
|
||||||
int check(enum sp_return result);
|
int check(enum sp_return result);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int result;
|
|
||||||
const int size = 30;
|
|
||||||
char *buffer = malloc(size + 1);
|
|
||||||
const char *port_name = "/dev/ttyACM0";
|
|
||||||
const unsigned int timeout = 1000;
|
|
||||||
struct sp_port *serial_port;
|
struct sp_port *serial_port;
|
||||||
|
char *port_name = "/dev/ttyACM0";
|
||||||
|
|
||||||
|
const int size = 256;
|
||||||
|
char *buffer = malloc(size + 1);
|
||||||
|
|
||||||
|
const unsigned int timeout = 1000;
|
||||||
|
int result;
|
||||||
|
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("Usage: %s <port>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
port_name = argv[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Connecting to '%s'...\n", port_name);
|
||||||
check(sp_get_port_by_name(port_name, &serial_port));
|
check(sp_get_port_by_name(port_name, &serial_port));
|
||||||
|
|
||||||
printf("Opening port.\n");
|
|
||||||
check(sp_open(serial_port, SP_MODE_READ_WRITE));
|
check(sp_open(serial_port, SP_MODE_READ_WRITE));
|
||||||
|
|
||||||
printf("Setting port to 9600 8N1, no flow control.\n");
|
|
||||||
check(sp_set_baudrate(serial_port, 9600));
|
check(sp_set_baudrate(serial_port, 9600));
|
||||||
check(sp_set_bits(serial_port, 8));
|
check(sp_set_bits(serial_port, 8));
|
||||||
check(sp_set_parity(serial_port, SP_PARITY_NONE));
|
check(sp_set_parity(serial_port, SP_PARITY_NONE));
|
||||||
check(sp_set_stopbits(serial_port, 1));
|
check(sp_set_stopbits(serial_port, 1));
|
||||||
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
check(sp_set_flowcontrol(serial_port, SP_FLOWCONTROL_NONE));
|
||||||
|
puts("Connected.\n");
|
||||||
|
|
||||||
result =
|
|
||||||
check(sp_blocking_write(serial_port, "ping", strlen("ping"), timeout));
|
|
||||||
|
|
||||||
while (1) {
|
while (true) {
|
||||||
result = check(sp_blocking_read(serial_port, buffer, size, timeout));
|
int pos = 0;
|
||||||
|
|
||||||
|
while (pos < size) {
|
||||||
|
result = check(sp_blocking_read(serial_port, buffer + pos, 1, timeout));
|
||||||
|
|
||||||
if (result == -1) {
|
if (result == -1) {
|
||||||
printf("Error reading from serial port\n");
|
printf("Error reading from serial port\n");
|
||||||
@ -39,12 +48,20 @@ int main(int argc, char **argv) {
|
|||||||
printf("No more data\n");
|
printf("No more data\n");
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
buffer[result] = '\0';
|
if (buffer[pos] == '\n') {
|
||||||
printf("=====\n%s\n", buffer);
|
buffer[pos] = '\0';
|
||||||
|
break;
|
||||||
|
} else
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
|
||||||
|
puts(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper function for error handling. */
|
/* Helper function for error handling. */
|
||||||
|
Loading…
Reference in New Issue
Block a user