最近研究w90p710串口通讯,看了下手册,发现有四个串口可以用,我选择了UART0来进行数据传输。telnetd到开发板上,进行操作,没有采用终端控制。
发送程序:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
int main()
{
int fd;
int nwrite;
struct termios oldtio,newtio;
char buff[]="hello world";
if((fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY)) == -1)
{
perror("open");
return -1;
}
printf("fd = %d\n",fd);
tcgetattr(fd, &oldtio);
cfsetispeed(&oldtio, B115200);
cfsetospeed(&oldtio, B115200);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CS8;
newtio.c_cflag &=~PARENB;
newtio.c_iflag &=~INPCK;
newtio.c_cflag &=~CSTOPB;
newtio.c_cc[VTIME]=1;
newtio.c_cc[VMIN]=0;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW, &newtio);
tcsetattr(fd, TCSANOW, &oldtio);
/*向串口写数据*/
if((nwrite = write(fd,buff,sizeof(buff))) == -1)
{
perror("read");
exit(EXIT_FAILURE);
}
//printf("%s\n",buff);
return 0;
}
运行以后的结果是 fd = 3 然后等待
接收程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
int main()
{
int fd;
int nread;
struct termios oldtio,newtio;
char buff[16];
if((fd = open("/dev/ttyS0", O_RDWR|O_NOCTTY)) == -1)
{
perror("open");
return -1;
}
printf("fd = %d\n",fd);
tcgetattr(fd, &oldtio);
cfsetispeed(&oldtio, B115200);
cfsetospeed(&oldtio, B115200);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= CS8;
newtio.c_cflag &=~PARENB;
newtio.c_iflag &=~INPCK;
newtio.c_cflag &=~CSTOPB;
newtio.c_cc[VTIME]=1;
newtio.c_cc[VMIN]=0;
tcflush(fd,TCIFLUSH);
tcsetattr(fd,TCSANOW, &newtio);
tcsetattr(fd, TCSANOW, &oldtio);
/*向串口读数据*/
if((nread = read(fd,buff,sizeof(buff))) == -1)
{
perror("read");
exit(EXIT_FAILURE);
}
printf("%s\n",buff);
return 0;
}
运行后的结果 fd = 3 一直等待。
当我选择终端控制时,接收端运行后的结果是fd = 4,能打印发送端的发送过来的数据。
请问大虾,在不开启终端的情况下应该怎样修改程序,才能输出发送端传来的字符呢? |