Reputation: 170
I am trying to write a program that accepts keyboard input and puts the output on the screen but acting as if it is a serial port. I am not sure this is even doable. My current code for the serial port that works is:
int fd;
char *portname;
char buf[255];
struct termios tty;
portname = "/dev/ttyUSB0";
// opening serial port
fd = open(portname, O_RDWR | O_NOCTYY | O_SYNC );
//writing to serial port
write (fd, "hello!\n", 7);
//Reading from serial port
read (fd, buf, 255)
So, is it possible that instead of setting portname as /dev/ttyUSB0 I set it as something else (e.g. /dev/stdin?) and then get the exact same UART functionality but keyboard is set as input and screen as output?
Thank you for your help.
Upvotes: 1
Views: 793
Reputation: 1416
UNIX/Linux sets up stdin (fd 0), stdout (fd 1) and stderr (fd 2) already open to the terminal (whether serial line with real hardware terminal, virtual console or graphics terminal) your keyboard and screen are connected to. It's available for termio control and as a special device filename /dev/tty
the termios routines termios(3) man page operate on the already open fild descriptor, which allows getting of terminal driver attributes, turning off canonical mode and later resetting the values at end of your program.
You simply read/write as normal, in POSIX everything's a file, whether it's a serial line, disk, terminal emulator is abstracted away, to the process doing I/O by the OS kernel.
Upvotes: 1