繁体   English   中英

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

[英]How to read character using serial port in Linux

我试图通过使用串行端口“ / 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;
}

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

#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;
}

您应使用以下命令将终端设备的模式更改为“原始”

tty.setraw(file_descriptor)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM