这是一个窗口配置,然后打开串口进行读操作的程序,我对makefile文件进行编译的时候(make -f port_makefile),出现了以下错误:
evan@evan-desktop:~/example2$ make -f port_makefile
gcc -c open_port.c -o open_port.o
open_port.c: In function ‘open_port’:
open_port.c:9: error: ‘O_RDWR’ undeclared (first use in this function)
open_port.c:9: error: (Each undeclared identifier is reported only once
open_port.c:9: error: for each function it appears in.)
open_port.c:9: error: ‘O_NiOCTTY’ undeclared (first use in this function)
open_port.c:9: error: ‘O_NDELAY’ undeclared (first use in this function)
open_port.c:15: warning: incompatible implicit declaration of built-in function ‘printf’
open_port.c:19: error: ‘O_NOCTTY’ undeclared (first use in this function)
open_port.c:38: error: ‘F_SETFL’ undeclared (first use in this function)
open_port.c:39: warning: incompatible implicit declaration of built-in function ‘printf’
open_port.c:43: error: ‘STDIN_FILENO’ undeclared (first use in this function)
make: *** [open_port.o] 错误 1
evan@evan-desktop:~/example2$
我反复检查了open_port.c文件,刚开始我以为是不是少了哪个头文件没添加,但是校对过了不是这个问题,我检查了每个程序,可能我水平查,看不出哪里出了问题,也可能是我port_makefile文件写错了,呵呵,那位高手指教一下,不胜感激。
几个程序如下:
/*文件名set_opt.c, 功能:串口配置设置*/
int set_opt(int fd,int nspeed ,int nbits,char nevent,int nstop)
{
struct termios newtio ,oldtio;
/*保存测试现有串口参数设置,如果串口号等出错,会给相关信息*/
if(tcgetattr(fd,&oldtio)!=0)
{
perror("setupserial 1");
return -1;
}
bzero(&newtio,sizeof(newtio));//将结构体参数全部清零
/*步骤1,设置字符大小*/
newtio.c_cflag |=CLOCAL|CREAD;//本地连接和接受使能
newtio.c_cflag &=~CSIZE;//先去除数据位中的位掩码,再重新设置
/*设置数据位*/
switch(nbits)
{
case 7:
newtio.c_cflag|=CS7;
break;
case 8:
newtio.c_cflag|=CS8;
break;
}
/*设置奇偶校验位*/
switch(nevent)
{
case 'O':
newtio.c_cflag|= PARENB;
newtio.c_cflag|= PARODD;
newtio.c_iflag|= (INPCK|ISTRIP);
break;
case 'E':
newtio.c_iflag |= PARENB;
newtio.c_cflag &= ~PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'N'
newtio.c_cflag &= ~PARENB;
break;
}
/*设置波特率*/
switch(nspeed)
{
case 2400:
cfsetispeed(&newtio,B2400);
cfsetospeed(&newtio,B2400);
break;
case 4800:
cfsetispeed(&newtio,B4800);
cfsetospeed(&newtio,B4800);
break;
case 9600:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
case 115200:
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
break;
case 460800:
cfsetispeed(&newtio,B460800);
cfsetospeed(&newtio,B460800);
break;
default :
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
}
/*设置停止位*/
switch(nstop)
{
case 1:
newtio.c_cflag &= ~CSTOPB;
break;
case 0:
newtio.c_cflag |= CSTOPB;
break;
}
/*设置等待时间和最小接受字符*/
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMINJ] =0;
/*处理未接收字符*/
tcflush(fd,TCIFLUSH);
/*激活新配置*/
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done !\n");
return 0;
}
/*文件名open_port.c,功能:打开串口*/
//#include
int open_port(int fd,int comport)
{
char *dev[]={ "/dev/ttys0","/dev/ttys1","/dev/ttys2"};
switch(comport)
{
case 1:
if(fd=open("/dev/ttys0",O_RDWR|O_NiOCTTY|O_NDELAY)<0)
{
perror("can not open serial port");
return (-1);
}
else
printf("open /dev/ttys0 sucess\n");
// }
case 2:
if(fd=open("/dev/ttys1",O_RDWR|O_NOCTTY|O_NDELAY)<0)
{
perror("can not open serial port");
return (-1);
}
else
printf("open /dev/ttys1 sucess\n");
// }
case 3:
if(fd=open("/dev/ttys2",O_RDWR|O_NOCTTY|O_NDELAY)<0)
{
perror("can not open serial port");
return (-1);
}
else
printf("open /dev/ttys2 sucess\n");
}
/*恢复串口为阻塞状态*/
if(fcntl(fd,F_SETFL,0)<0)
printf("fcntl failde !\n");
else
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
/*测试是否为终端设备*/
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty sucess!\n");
printf("fd-open=%d\n",fd);
return fd;
}
/*文件名:port.c,头函数文件*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*文件名port_main.c*/
#include
/*读串口*/
int main()
{
int fd;
int nread,i;
char buff[]="hello\n";
if((fd=open_port(fd,1))<0)
{
perror("open_port error");
return ;
}
if((i=set_port(fd,115200,8,'N',1))<0)
{
perror("set_port error");
return;
}
printf("fd=%d\n",fd);
fd=3;
nread=read(fd,buff,8);
printf("nread -%d,%s\n",nread,buff);
close(fd);
return 0;
}
/*port_makefike文件*/
port:open_port.o set_opt.o main.o
gcc -o port main.o set_opt.o open_port.o
main.o:port_main.c port.h
gcc -c main.c -o main.o
open_port.o:open_port.c port.h
gcc -c open_port.c -o open_port.o
set_opt.o:set_opt.c port.h
gcc -c set_opt.c -o set_opt.o