串口测试程序,将串口的2,3脚直接相连,实现同一台电脑的收发信息。
设置串口函数
int speed_array[]={B115200,B38400,B19200,B9600,B4800,B2400,B1200,B300};
int name_array[]={115200,38400,19200,9600,4800,2400,1200,300};
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd,&Opt);
for (i=0;i {
if(speed==name_array[i])
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&Opt,speed_array[i]);
cfsetospeed(&Opt,speed_array[i]);
status=tcsetattr(fd,TCSANOW,&Opt);
if(status!=0)
{
perror("tcsetattr fd1");
exit(0);
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if(tcgetattr(fd,&options)!= 0)
{
perror ("SetupSerial 1");
return -1;
}
options.c_cflag&= ~CSIZE;
switch(databits)
{
case 7:
options.c_cflag|=CS7;
break;
case 8:
options.c_cflag|=CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return -1;
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag&=~PARENB; /* Clear parity enable */
options.c_iflag&=~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag|=(PARODD | PARENB);
options.c_iflag|=INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag|=PARENB; /* Enable parity */
options.c_cflag&=~PARODD;
options.c_iflag|=INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity */
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return -1;
}
switch(stopbits)
{
case 1:
options.c_cflag&= ~CSTOPB;
break;
case 2:
options.c_cflag|=CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return -1;
}
/* Set input parity option */
if(parity!='n')
options.c_iflag|=INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME]=50;
options.c_cc[VMIN]=0; /* Update the options and do it NOW */
options.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
options.c_iflag&=~(ICRNL|IXON);
options.c_oflag&=~(ICRNL|IXON);
options.c_oflag&=~OPOST;
if(tcsetattr(fd,TCSANOW,&options)!= 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}
send.c
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
fd=open("/dev/ttyS1",O_RDWR);
if(fd==-1)
{
perror("Can't Open Serial Port\n");
exit(-1);
}
set_speed(fd,115200);
if(set_Parity(fd,8,1,'n')==-1)
{
printf("set_parity error\n");
}
char buf[]="hello";
write(fd,buf,sizeof(buf));
close(fd);
}
recive.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int fd;
fd=open("/dev/ttyS1",O_RDWR|O_NONBLOCK);
if(fd==-1)
{
perror("Can't Open Serial Port\n");
exit(-1);
}
set_speed(fd,115200);
if(set_Parity(fd,8,1,'n')==-1)
{
printf("set_parity error\n");
}
fd_set fds;
struct timeval timeout={10,0};
char buf[100];
char *p;
p=buf;
FD_ZERO(&fds);
FD_SET(fd,&fds);
switch(select(fd+1,&fds,NULL,NULL,&timeout))
{
case -1:printf("error\n");return 0;
case 0:printf("timeout\n");break;
default:
if(FD_ISSET(fd,&fds))
{
read(fd,p,1);
while(*p)
{
printf("%c",*p);
p++;
read(fd,p,1);
}
printf("\n");
}
}
close(fd);
}
阅读(3022) | 评论(0) | 转发(0) |