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Îļþ£¬¸Õ¿ªÊ¼ÎÒÒÔΪÊDz»ÊÇÉÙÁËÄĸöÍ·ÎļþûÌí¼Ó£¬µ«ÊÇУ¶Ô¹ýÁ˲»ÊÇÕâ¸öÎÊÌ⣬ÎÒ¼ì²éÁËÿ¸ö³ÌÐò£¬¿ÉÄÜÎÒˮƽ²é£¬¿´²»³öÄÄÀï³öÁËÎÊÌ⣬Ҳ¿ÉÄÜÊÇÎÒ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
evan6 ÓÚ 2011-05-02 13:27:12·¢±í:
2# txgc_wm ´úÂëÊǶàÁËÒ»µã¶øÒÑ{:3_120:}
evan6 ÓÚ 2011-05-02 13:26:11·¢±í:
µÚÒ»¾ä»°ÊÇ¡°´®¿ÚÅäÖá±£¬ÎÒ´ò´í³É¡°´°¿ÚÅäÖÃÁË¡±£¬ÓèÓë¾ÀÕý¡£
txgc_wm ÓÚ 2011-05-01 23:52:37·¢±í:
¸ßÉ