打印

串口0接收数据问题求助!!!

[复制链接]
1482|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
maoyanketi|  楼主 | 2010-8-9 21:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近研究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,能打印发送端的发送过来的数据。

请问大虾,在不开启终端的情况下应该怎样修改程序,才能输出发送端传来的字符呢?

相关帖子

沙发
nusummit| | 2010-8-11 18:01 | 只看该作者
要这样设定

tcgetattr(handle,&oldtio); /* save current modem settings */

/*
Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
Also don't hangup automatically and ignore modem status.
Finally enable receiving characters.
*/
        newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;

/*
Ignore bytes with parity errors and make terminal raw and dumb.
*/
        newtio.c_iflag = IGNPAR;

/*
Raw output.
*/
        newtio.c_oflag = 0;

/*
Don't echo characters because if you connect to a host it or your
modem will echo characters for you. Don't generate signals.
*/
        newtio.c_lflag = 0;

/* blocking read until 1 char arrives */
        newtio.c_cc[VMIN]=0;
        newtio.c_cc[VTIME]=20;

/* now clean the modem line and activate the settings for modem */
        tcflush(handle, TCIFLUSH);
        tcsetattr(handle,TCSANOW,&newtio);
        //cfsetospeed(&newtio,B38400);
        if(!flag)
                cfsetispeed(&newtio,BAUDRATE);
        else
                cfsetospeed(&newtio,BAUDRATE);

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

143

主题

1079

帖子

0

粉丝