【发布时间】:2013-06-07 10:30:39
【问题描述】:
我从串行设备读取缓冲区。它会返回这些结果(每次 2 行)
Hello World.
My name is John.
Hello World.^M^JMy name
is Mike.
Hello World.^M^JMy name
is ^M^JERROR Peter.
这些结果在 Linux 命令行中。 ^M^J 是 EOL,在 Windows 中表示 \r\n。第一个结果还可以,但其他两个很糟糕。有没有办法检查 ^M^J 字符并删除它们?因为我想要这些结果:
Hello World.
My name is John.
Hello World.
My name is Mike.
Hello World.
My name is Peter.
通过这段代码我读取了缓冲区
char buff[150];
memset(buff, 0, sizeof(buff));
for (;;)
{
n=read(fd,buff,sizeof(buff));
printf("%s", buff);
}
更新
我以这种方式打开和配置我的设备
int open_port(void)
{
int fd; // file description for the serial port
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) // if open is unsucessful
{
//perror("open_port: Unable to open /dev/ttyAMA0 - ");
printf("open_port: Unable to open /dev/ttyAMA0. \n");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("port is open.\n");
}
return(fd);
} //open_port
并配置端口
int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in
cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);
port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);
} //configure_port
【问题讨论】:
-
我认为您必须逐个字符地检查 ascii 值 en.wikipedia.org/wiki/ASCII#ASCII_control_code_chart
-
read()一个 EOF 之后在 Windows 中会发生什么?阅读停止了吗? -
我不知道在 Windows 中会发生什么。我使用 Linux,我有这些字符
-
@mf_ 显然 EOF 是 EOL 的拼写错误。