| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 546 人关注过本帖
标题:求助,串口0无法正常接收数据
只看楼主 加入收藏
cuixingasd
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:54
专家分:135
注 册:2009-3-24
结帖率:100%
收藏
 问题点数:0 回复次数:2 
求助,串口0无法正常接收数据
最近研究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,能打印发送端的发送过来的数据。
请问达人,在不开启终端的情况下应该怎样修改程序,才能输出发送端传来的字符呢?
我也试过RAW模式,可是依旧这种情况。串口的标准模式与非标准模式我也都试过了,依然不行。
搜索更多相关主题的帖子: 数据 串口 
2009-09-21 15:51
cuixingasd
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:54
专家分:135
注 册:2009-3-24
收藏
得分:0 
看来也没人回答,结贴了
2009-09-23 09:13
beyond_one
Rank: 4
等 级:业余侠客
帖 子:61
专家分:206
注 册:2009-6-26
收藏
得分:0 
到单片机的论坛上去看看吧
2009-09-23 12:26
快速回复:求助,串口0无法正常接收数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017144 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved