我从串行设备读取缓冲区。它返回我这些结果(每次 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