CODE
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main(void)
{
FILE *input;
FILE *output;
input = fopen("/dev/tty", "r"); //open the terminal keyboard
output = fopen("/dev/tty", "w");
int fd; /* File descriptor for the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
int c=fgetc(input);
while(c!=0)
{
fputc(c,output);//output to screen
write(fd, c, 1);//write to port
c=fgetc(input);//get char from keyboard
fcntl(fd, F_SETFL, 0);//read from port
}
printf("%d",fd);
return (fd);
}