- 论坛徽章:
- 0
|
硬件上只接了GND,TDX,RDX三根线.运行以下程序时,一直接收不到数据,是否哪儿写错了.但可以发送出数据
#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <sys/types.h>;
#include <sys/stat.h>;
#include <fcntl.h>;
#include <errno.h>;
#include <string.h>;
#include <termios.h>;
int main()
{ int fd,nbyte,nread,i,j;
struct termios opt;
char buffer[1024]="*hello,xianren!*";
char buffer1[]="00000000000000000";
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);//open com
if(fd==-1)perror("Unable to open /dev/ttyS0!" ;
//printf("fd=%d\n",fd);
tcgetattr(fd,&opt);
cfsetispeed(&opt,B9600);//baud rate
cfsetospeed(&opt,B9600);
opt.c_cflag|=(CLOCAL|CREAD);
tcsetattr(fd,TCSANOW,&opt);
opt.c_cflag &=~PARENB;//8N1
opt.c_cflag &=~CSTOPB;
opt.c_cflag &=~CSIZE;
opt.c_cflag |=CS8;
//opt.c_cflag &=~CNEW_RTSCTS; //remove hardware control
opt.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG); //origingal input
opt.c_oflag &=~OPOST; //original output
opt.c_cc[VMIN]=0;
opt.c_cc[VTIME]=10; //wait for 1s
printf("Send and receive begin:\n" ;
for(i=0;i<16;i++)
{
//nbyte=write(fd,&buffer,1);
//printf("%d data sended:%c \t",nbyte,buffer);
nread=read(fd,&buffer1,1);
//while(nread<=0);
printf("%d data received:%c\n",nread,buffer1);
}
buffer1[16]='\0';
printf("Send and Receive end!\n data received is:%s\n",buffer1);
//nbyte=write(fd,buffer,1);
//printf("%d data sended:%s\n",nbyte,buffer);
close(fd);
return(0);
} |
|