如何在Linux中使用串口读取字符

时间:2016-02-04 06:02:37

标签: c linux serial-port tty

我尝试使用串口" / dev / ttyS0" 读取串行原始字节。在我的程序中,我想要的是按下该字母并立即看到我在没有按 ENTER 的情况下重复介绍的那封信。例如,如果我按下字母' a'我想看到另一个' a'在它旁边。

我的代码在这里:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

int main()
{
        int n = 0, fd = 0;

        struct termios term,trm;

        printf("%d\n",getpid());

        fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);

        if (fd == -1)
        {
                perror("open");
                return 1;
        }
        else
        {
                fcntl(fd, F_SETFL, 0);
                perror("Port");
        }

        if (n = tcgetattr(fd, &term) == -1)
        {
                perror("tcgetattr");
                return 1;
        }

        if (n = cfsetispeed(&term, B115200) == -1)
        {
                perror("cfsetispeed");
                return 1;
        }

        if (n = cfsetospeed(&term, B115200) == -1)
        {
                perror("cfsetospeed");
                return 1;
        }

        term.c_cflag |= (CLOCAL | CREAD);
        term.c_cflag &= ~PARENB;
        term.c_cflag &= ~CSTOPB;
        term.c_cflag &= ~CSIZE;
        term.c_cflag |= CS8;
        term.c_cflag &= ~CRTSCTS;
        term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        term.c_iflag &= ~(IXON | IXOFF | IXANY);
        term.c_iflag |= (INPCK | ISTRIP);
        term.c_oflag &= ~OPOST;

        unsigned char c,d;
        ssize_t s=0;

        tcflush(fd, TCIOFLUSH);

        system("/bin/stty raw");
        while((c=getchar()) != 'q')
        {
                write(fd, &c,1);

                term.c_cc[VMIN] = 1;
                term.c_cc[VTIME] = 0;

                tcsetattr(fd, TCSANOW, &term);

                if((s=read(fd, &d,1)) != -1)
                {
                        perror("read");
                        printf("%c",d);
                }
        }
        system("/bin/stty cooked");
        close(fd);
        return 0;
}

2 个答案:

答案 0 :(得分:2)

我不知道它是否是您正在寻找的解决方案,但您必须禁用stdin缓冲以使getchar退出每个char用户输入。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>

int main()
{
        int n = 0, fd = 0;

        struct termios term, old_stdin, new_stdin;

        printf("%d\n",getpid());

        fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

        if (fd == -1)
        {
                perror("open");
                return 1;
        }
        else
        {
                fcntl(fd, F_SETFL, 0);
                perror("Port");
        }

        if (n = tcgetattr(fd, &term) == -1)
        {
                perror("tcgetattr");
                return 1;
        }

        if (n = cfsetispeed(&term, B115200) == -1)
        {
                perror("cfsetispeed");
                return 1;
        }

        if (n = cfsetospeed(&term, B115200) == -1)
        {
                perror("cfsetospeed");
                return 1;
        }

        term.c_cflag |= (CLOCAL | CREAD);
        term.c_cflag &= ~PARENB;
        term.c_cflag &= ~CSTOPB;
        term.c_cflag &= ~CSIZE;
        term.c_cflag |= CS8;
        term.c_cflag &= ~CRTSCTS;
        term.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
        term.c_iflag &= ~(IXON | IXOFF | IXANY);
        term.c_iflag |= (INPCK | ISTRIP);
        term.c_oflag &= ~OPOST;

        unsigned char c,d;
        ssize_t s=0;

        tcflush(fd, TCIOFLUSH);
        term.c_cc[VMIN] = 1;
        term.c_cc[VTIME] = 0;

        tcsetattr(fd, TCSADRAIN, &term);

        // get the terminal settings for stdin 
        tcgetattr(STDIN_FILENO,&old_stdin);

        new_stdin = old_stdin;

        // disable canonical mode (buffered i/o) and local echo 
        new_stdin.c_lflag &=(~ICANON & ~ECHO);

        // set the new settings
        tcsetattr(STDIN_FILENO,TCSANOW,&new_stdin);

        while((c=getchar()) != 'q')
        {
            write(fd, &c,1);

            if((s=read(fd, &d,1)) != -1)
            {
                perror("read");
                printf("%c",d);
            }
        }
        close(fd);

        // Restore the old stdin setup
        tcsetattr(STDIN_FILENO,TCSANOW,&old_stdin);

        return 0;
}

答案 1 :(得分:0)

您应该使用

将终端设备的模式更改为“raw”
tty.setraw(file_descriptor)