下面是我写的一个简单的写串口的程序,在虚拟机上可以正常运行,但是在ARM开发板上会出现错误:tcgetattr: Input/output error。请问是怎么回事?谢谢
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <termios.h>
#define BUFFER_SIZE 1024
int main(void)
{
int fd,fd1;
char buff[BUFFER_SIZE];
int real_read,real_write;
struct termios new_cfg,old_cfg;
int speed;
//**打开串口**//
fd = open("/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
printf("fd=%d\n",fd);
if (fd < 0)
{
perror("open serial port");
return(-1);
}
/*恢复串口为阻塞状态*/
if (fcntl(fd, F_SETFL, 0) < 0)
{
perror("fcntl F_SETFL\n");
}
/*测试是否为终端设备*/
if (isatty(STDIN_FILENO) == 0)
{
perror("standard input is not a terminal device");
}
//**设置串口为波特率为2400,8位数据,偶校验,一位停止位**//
/*保存并测试现有串口参数设置,在这里如果串口号等出错,会有相关的出错信息*/
if (tcgetattr(fd,&old_cfg)!=0)
{
printf("tcgetattr=%s\n",tcgetattr);
perror("tcgetattr");
return -1;
}
/*设置字符大小*/
new_cfg=old_cfg;
cfmakeraw(&new_cfg);/*配置为原始模式*/
new_cfg.c_cflag &= ~CSIZE;
/*设置波特率*/
speed = B2400;
cfsetispeed(&new_cfg,speed);
cfsetospeed(&new_cfg,speed);
/*设置数据位*/
new_cfg.c_cflag &= ~CSIZE; /*用数据位掩码清空数据位设置*/
new_cfg.c_cflag |= CS8;
/*设置奇偶校验位*/
new_cfg.c_cflag |= PARENB;
new_cfg.c_cflag &= ~PARODD; /*清除偶校验标志,则配置为奇校验*/
new_cfg.c_iflag |= INPCK;
/*设置停止位*/
new_cfg.c_cflag &= ~CSTOPB;
/*设置等待时间和最小接收字符*/
new_cfg.c_cc[VTIME] = 0;
new_cfg.c_cc[VMIN] = 1;
/*处理未接收字符*/
tcflush(fd,TCIFLUSH);
/*激活信配置*/
if ((tcsetattr(fd, TCSANOW, &new_cfg)) != 0)
{
perror("tcsetattr");
return -1;
}
memset(buff, 0, BUFFER_SIZE);
printf("buff=%s\n",buff);
fd1 = open("/home/newsu/senddates", O_RDWR|O_NOCTTY|O_NDELAY);
printf("fd1=%d\n",fd1);
real_read = read(fd1, buff, BUFFER_SIZE);
printf("real_read=%d\n",real_read);
printf("buff=%s\n",buff);
real_write = write(fd, buff, strlen(buff));
printf("real_write=%d\n",real_write);
close(fd);
return 0;
}