57 lines
1.4 KiB
C
57 lines
1.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
|
|
#define DEFAULT_PORT "/dev/ttyESP32_A"
|
|
#define DEFAULT_BAUD B1500000
|
|
|
|
int open_serial(const char *path, speed_t baud) {
|
|
int fd = open(path, O_RDONLY | O_NOCTTY);
|
|
if (fd < 0) { perror("open"); return -1; }
|
|
|
|
struct termios tio;
|
|
if (tcgetattr(fd, &tio) != 0) { perror("tcgetattr"); close(fd); return -1; }
|
|
|
|
cfsetispeed(&tio, baud);
|
|
cfsetospeed(&tio, baud);
|
|
|
|
// Raw mode
|
|
tio.c_iflag &= ~(IGNBRK | BRKINT | ICRNL | INLCR | IXON | IXOFF | IXANY |
|
|
PARMRK | INPCK | ISTRIP);
|
|
tio.c_oflag &= ~(OPOST);
|
|
tio.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
|
|
tio.c_cflag &= ~(CSIZE | PARENB | CSTOPB | CRTSCTS);
|
|
tio.c_cflag |= (CS8 | CLOCAL | CREAD);
|
|
|
|
tio.c_cc[VMIN] = 1;
|
|
tio.c_cc[VTIME] = 0;
|
|
|
|
if (tcsetattr(fd, TCSANOW, &tio) != 0) { perror("tcsetattr"); close(fd); return -1; }
|
|
|
|
return fd;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
const char *port = (argc > 1) ? argv[1] : DEFAULT_PORT;
|
|
speed_t baud = DEFAULT_BAUD;
|
|
|
|
int fd = open_serial(port, baud);
|
|
if (fd < 0) return 1;
|
|
|
|
uint8_t buf[8192];
|
|
while (1) {
|
|
ssize_t n = read(fd, buf, sizeof(buf));
|
|
if (n > 0) {
|
|
fwrite(buf, 1, n, stdout);
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|