linux环境下串口测试问题
我在linux环境下写了一个C语言的串口测试程序,测试串口的自发自接功能
源代码如下
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/time.h>
void init_ttyS(int fd)
{
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_lflag &= ~(ECHO | ICANON);
newtio.c_cflag = B9600 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_oflag &= ~(OPOST);
newtio.c_cc[VTIME] = 5; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 9 chars received */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
}//end init_ttyS
int main(void)
{
int fd;
int nread,i,ss;
char buff[]="Hello\n",*bufff;
// open seriel port
fd = open("/dev/ttyS0", O_RDWR);
if (fd == -1)
{
printf("open device error\n");
}
else
{
init_ttyS(fd); // init device
printf("fd=%d\n",fd);
printf("start send\n");
nread=write(fd,buff,8);
printf("nread=%d,%s\n",nread,buff);
while(1)
{
ss=read(fd,bufff,6);
if(ss>0)
{
printf("ss=%d,%s\n",ss,bufff);
break;
}
}
}
close(fd);
return 0;
} // end main
运行之前将串口的2号和3号引脚连接起来
运行时收不到字符,求解