gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:08 25 4
gpt4 key购买 nike

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

最佳答案

我不知道这是否是您正在寻找的解决方案,但您必须禁用标准输入缓冲以使 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;
}

关于c - 如何在Linux中使用串口读取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35193613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com