|
写了个读串口数据的程序,弄了半天,可以收到了,过了几天后,发现收不到了,大家帮忙看看是怎么回事啊,我也没改过啊。代码如下:
读串口的:
/*********ttyReceive.c*************/
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
#include <string.h>
#define FALSE -1
#define TRUE 0
int speed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,
B38400,B19200,B9600,B4800,B2400,B1200,B300};
int name_arr[]={38400,19200,9600,4800,2400,1200,300,
38400,19200,9600,4800,2400,1200,300};
int OpenDev(char *Dev)
{
int fd=open(Dev,O_RDWR);
if(-1==fd)
{
perror("Can't Open Serial Port!\n");
return(FALSE);
}
else
return fd;
}
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd,&Opt);
for (i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if (speed==name_arr)
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&Opt,speed_arr);
cfsetospeed(&Opt,speed_arr);
status=tcsetattr(fd,TCSANOW,&Opt);
if (status!=0)
{
perror("tcsetattr fd\n");
return;
}
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(FALSE);
}
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(FALSE);
}
switch(parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity!\n");
return(FALSE);
}
switch(stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return(FALSE);
}
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME]=0;
options.c_cc[VMIN]=1;
if (tcsetattr(fd,TCSANOW,&options)!=0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
int main(void)
{
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttyS1";
fd=OpenDev(dev);
set_speed(fd,9600);
if(set_Parity(fd,8,1,'N')==FALSE)
{
printf("Set Parity Error!\n");
exit(0);
}
while(1)
{
nread=read(fd,buff,512);
buff[nread]=0;
printf(":%s\n",buff);
}
close(fd);
exit(0);
}
下面是写串口的:
/**************ttySend.c******************/
#include <stdio.h> /*标准输入输出定义*/
#include <stdlib.h> /*标准函数库定义*/
#include <unistd.h> /*Unix标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h> /*文件控制定义*/
#include <termios.h> /*PPSIX终端控制定义*/
#include <errno.h> /*错误号定义*/
#define FALSE -1
#define TRUE 0
int speed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,
B38400,B19200,B9600,B4800,B2400,B1200,B300};
int name_arr[]={38400,19200,9600,4800,2400,1200,300,
38400,19200,9600,4800,2400,1200,300};
int OpenDev(char *Dev)
{
int fd=open(Dev,O_RDWR);
if(-1==fd)
{
perror("Can't Open Serial Port!\n");
return(FALSE);
}
else
return fd;
}
void set_speed(int fd,int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd,&Opt);
for (i=0;i<sizeof(speed_arr)/sizeof(int);i++)
{
if (speed==name_arr)
{
tcflush(fd,TCIOFLUSH);
cfsetispeed(&Opt,speed_arr);
cfsetospeed(&Opt,speed_arr);
status=tcsetattr(fd,TCSANOW,&Opt);
if (status!=0)
{
perror("tcsetattr fd\n");
return;
}
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(FALSE);
}
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(FALSE);
}
switch(parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
case 'S':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity!\n");
return(FALSE);
}
switch(stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return(FALSE);
}
if (parity != 'n')
options.c_iflag |= INPCK;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME]=150;
options.c_cc[VMIN]=0;
if (tcsetattr(fd,TCSANOW,&options)!=0)
{
perror("SetupSerial 3");
return(FALSE);
}
return(TRUE);
}
int main(void)
{
int i;
int fd;
int nread;
char buff[512];
char *dev = "/dev/ttyS0";
for(i=0;i<512;i++)
buff='A';
fd=OpenDev(dev);
set_speed(fd,9600);
if(set_Parity(fd,8,1,'N')==FALSE)
{
printf("Set Parity Error!\n");
exit(0);
}
while(1)
{
nread=write(fd,buff,512);
}
close(fd);
exit(0);
}
往串口1写数据,从串口2读数据,两个串口我用串口线连起来了,用串口调试助手测试是正常的。前几天都是好用的,就今天发现不好用了。怪事! |
|