59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <poll.h>
|
|
|
|
int main(void) {
|
|
int fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
|
|
if (fd < 0) { perror("open"); return 1; }
|
|
|
|
// Configure serial port
|
|
struct termios tty;
|
|
tcgetattr(fd, &tty);
|
|
cfmakeraw(&tty);
|
|
cfsetspeed(&tty, B115200);
|
|
tty.c_cflag |= CLOCAL | CREAD;
|
|
tty.c_cc[VMIN] = 0;
|
|
tty.c_cc[VTIME] = 1;
|
|
tcsetattr(fd, TCSANOW, &tty);
|
|
|
|
// Set stdin to raw mode
|
|
struct termios old_stdin, raw_stdin;
|
|
tcgetattr(STDIN_FILENO, &old_stdin);
|
|
raw_stdin = old_stdin;
|
|
cfmakeraw(&raw_stdin);
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin);
|
|
|
|
write(STDOUT_FILENO, "Holcks minitty - ctrl-d to exit\r\n", 33);
|
|
|
|
struct pollfd fds[2] = {
|
|
{ .fd = STDIN_FILENO, .events = POLLIN },
|
|
{ .fd = fd, .events = POLLIN },
|
|
};
|
|
|
|
char buf[256];
|
|
while (1) {
|
|
poll(fds, 2, -1);
|
|
if (fds[0].revents & POLLIN) {
|
|
int n = read(STDIN_FILENO, buf, sizeof(buf));
|
|
if (n > 0) {
|
|
for (int i = 0; i < n; i++)
|
|
if (buf[i] == 4) goto done; // Ctrl-D
|
|
write(fd, buf, n);
|
|
}
|
|
}
|
|
if (fds[1].revents & POLLIN) {
|
|
int n = read(fd, buf, sizeof(buf));
|
|
if (n > 0) write(STDOUT_FILENO, buf, n);
|
|
}
|
|
}
|
|
|
|
done:
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &old_stdin);
|
|
close(fd);
|
|
return 0;
|
|
}
|