#include#include#include#include#include#include#include#include#include#include#include#includetypedefenum{
SERIAL_8N1=0,
SERIAL_7E1=1,
SERIAL_7O1=2,
SERIAL_7S1=3}serial_format;constchar*serial_port[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3","/dev/ttyS4"};intserial_open(intport)
{intfd=open(serial_port[port], O_RDWR|O_NONBLOCK);if(-1==fd)
{
perror("Can't Open Serial Port");
exit (-1);
}else{
fcntl(fd, F_SETFL,0);
}returnfd;
}voidserial_format_set(intfd,serial_format format)
{intstatus=0;structtermios options;if( tcgetattr( fd,&options)!=0)
{
perror("serial fomat abnormal");
exit (-1);
}
options.c_cflag&=~CSIZE;switch(format)
{caseSERIAL_8N1:
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
options.c_cflag|=CS8;
options.c_iflag&=~(INPCK|ISTRIP);break;caseSERIAL_7E1:
options.c_cflag|=PARENB;
options.c_cflag&=~PARODD;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
options.c_cflag|=CS7;
options.c_iflag|=(INPCK|ISTRIP);break;caseSERIAL_7O1:
options.c_cflag|=PARENB;
options.c_cflag|=PARODD;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
options.c_cflag|=CS7;
options.c_iflag|=(INPCK|ISTRIP);break;caseSERIAL_7S1:
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
options.c_cflag|=CS8;
options.c_iflag&=~INPCK;
options.c_iflag|=ISTRIP;break;default:
perror("serial format abnormal");
exit (-1);
}
tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW,&options);if(status!=0)
{
perror("tcsetattr format abnormal");
exit(-1);
}
}voidserial_speed_set(intfd,intbaudrate)
{intstatus;structtermios options;/** Get the current options for the port
*/tcgetattr(fd,&options);/** Set the baud rates to 19200
*/cfsetispeed(&options, baudrate);
cfsetospeed(&options, baudrate);/** Enable the receiver and set local mode
*/options.c_cflag|=(CLOCAL|CREAD);/** Set the new options for the port
*/tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW,&options);if(status!=0)
{
perror("tcsetattr speed abnormal");
exit(-1);
}
}voidserial_etc_config(intfd)
{intstatus=0;structtermios options;if( tcgetattr( fd,&options)!=0)
{
perror("serial etc abnormal");
exit (-1);
}//no hardware flow controloptions.c_cflag&=~CRTSCTS;//no soft controloptions.c_iflag&=~(IXON|IXOFF|IXANY);//raw set_inputoptions.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);//no output process , raw set_inputoptions.c_oflag&=~OPOST;//min characteroptions.c_cc[VMIN]=1;//time to wait for dataoptions.c_cc[VTIME]=15;
tcflush(fd,TCIOFLUSH);
status=tcsetattr(fd, TCSANOW,&options);if(status!=0)
{
perror("tcsetattr etc abnormal");
exit(-1);
}
}voidserial_config(intfd,intbaudrate,serial_format format)
{
serial_speed_set(fd,baudrate);
serial_format_set(fd,format);
serial_etc_config(fd);
}intmain(intargc,char**argv)
{
fd_set set_input;intfd;charbuff[50]={'\0',};structtimeval timeout;intret_select=0;intbytes=0;intwrite_num=0;intread_num=0;intwait=0;
fd=serial_open(0);
serial_config(fd,B9600,SERIAL_8N1);
FD_ZERO(&set_input);
FD_SET(fd,&set_input);intloop_test=5;while(loop_test)
{
printf("Loop %d\n",loop_test);
loop_test--;//tcflush(fd,TCIOFLUSH);if(loop_test%2)
{
write_num=write(fd,"hello \nlinux world !\n",strlen("hello \nlinux world !\n"));
}else{
write_num=write(fd,"hello \nserial port !\n",strlen("hello \nserial port !\n"));
}
printf("now %d bytes has been written to the serial port\n",write_num);
timeout.tv_sec=1;
timeout.tv_usec=0;
ret_select=select(fd+1,&set_input, NULL,NULL,&timeout);/*See if there was an error*/if(ret_select<0)
perror("select failed");elseif(ret_select==0)
printf("timeout\n");else{/*We have set_input*/if(FD_ISSET(fd,&set_input))
{
bytes=0;
wait=0;do{
ioctl(fd, FIONREAD,&bytes);
}while( wait++,bytes!=write_num);
printf("now %d bytes is availble to read\n",bytes);
printf("delay %d times for read\n",wait);
read_num=read(fd, buff, bytes);
printf("now %d bytes has been read from serial\n",read_num);
buff[bytes]='\0';
printf("%s", buff);
}
}
printf("************\n");
}
close(fd);
exit (0);
}