串行端口卡在 read() 数据
serial port getting stuck at read() data
我已经将 HC-05 蓝牙模块连接到 microZed 开发板并尝试在 Linux 中通过 uart 发送和接收数据,现在当我将数据从我的开发板发送到应用程序时发送代码可以正常工作它工作得很好,下面是我发送的代码
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf;
int i;
while(1) {
buf++;
write(portfd, &buf, 1);
write(portfd, "\r\n", 2);
usleep(200000);
}
return 0;
}
现在当我尝试将数据从应用程序发送到蓝牙模块时出现问题,有时程序会停止并显示 "random nonblocking pool initialized" 或卡在下面给出的代码中的 i = read(portfd, buf, 20);
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
printf("hello1\n\r");
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
printf("hello2\n\r");
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf[20];
printf("hello3\n\r");
int i;
while(1) {
i = read(portfd, buf, 20);
printf("hello\n\r");
buf[i] = 0;
printf("%s", buf);
printf("\n\r");
}
return 0;
}
有什么建议可以解决这个问题吗?
我找到了解决方案,这个 post 指出了第一个错误。
我应该设置等待字符读取大于 0 和最小字符数读取为零的时间
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 0;
我需要将 tty.c_lflag
标志设置为 0 以启用原始输入而不是规范输入,现在代码正在不断读取数据
我已经将 HC-05 蓝牙模块连接到 microZed 开发板并尝试在 Linux 中通过 uart 发送和接收数据,现在当我将数据从我的开发板发送到应用程序时发送代码可以正常工作它工作得很好,下面是我发送的代码
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf;
int i;
while(1) {
buf++;
write(portfd, &buf, 1);
write(portfd, "\r\n", 2);
usleep(200000);
}
return 0;
}
现在当我尝试将数据从应用程序发送到蓝牙模块时出现问题,有时程序会停止并显示 "random nonblocking pool initialized" 或卡在下面给出的代码中的 i = read(portfd, buf, 20);
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
printf("hello1\n\r");
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
printf("hello2\n\r");
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf[20];
printf("hello3\n\r");
int i;
while(1) {
i = read(portfd, buf, 20);
printf("hello\n\r");
buf[i] = 0;
printf("%s", buf);
printf("\n\r");
}
return 0;
}
有什么建议可以解决这个问题吗?
我找到了解决方案,这个 post 指出了第一个错误。 我应该设置等待字符读取大于 0 和最小字符数读取为零的时间
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = 0;
我需要将 tty.c_lflag
标志设置为 0 以启用原始输入而不是规范输入,现在代码正在不断读取数据