免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1809 | 回复: 1
打印 上一主题 下一主题

[应用] 嵌入式Linux串口程序只发不收 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-12-10 14:48 |只看该作者 |倒序浏览
我用的是Ubuntu12.04,  修改串口应用程序,虽然是单串口还是用了select,现在在飞凌的AM335XS(TI AM3354)板子上只能write,read 是怎么也收不到数,光是超时。我排除了硬件问题,难道程序有错吗?请大家看看
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/time.h>

#define N 64
#define M 64

int open_port(int fd, int comport)
{
        char *dev[]={"/dev/ttyO0", "/dev/ttyO1", "/dev/ttyO2"};
        long vdisable;
        if(comport == 1)
        {
                fd = open(dev[0], O_RDWR|O_NOCTTY|O_NDELAY);
                if(-1 == fd)
                {
                        perror("Can't Open Serial Port");
                        return (-1);
                }
        }
        else if(comport == 2)
        {
                fd = open(dev[1], O_RDWR|O_NOCTTY|O_NDELAY);
                if(-1 == fd)
                {
                        perror("Can't Open Serial Port");
                        return (-1);
                }
        }       
        else if(comport == 3)
        {
                fd = open(dev[2], O_RDWR|O_NOCTTY|O_NDELAY);
                if(-1 == fd)
                {
                        perror("Can't Open Serial Port");
                        return (-1);
                }
        }

        if(fcntl(fd, F_SETFL, 0) < 0)
                printf("fcntl failed!\n");
        else
                printf("fcntl=%d\n", fcntl(fd, F_SETFL, 0));

        if(isatty(STDIN_FILENO)==0)
        {
                printf("standard input is not a terminal device\n");
        }
        else
                printf("isatty success!\n");
                printf("fd-open=%d\n", fd);
        return fd;
}


int set_opt(int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
        struct termios newtio, oldtio;
        if(tcgetattr(fd, &oldtio) != 0)
        {
                perror("SetupSerial 1");
                return -1;
        }
        bzero(&newtio, sizeof(newtio));

        newtio.c_cflag != CLOCAL | CREAD;
        newtio.c_cflag &= ~CSIZE;

        switch(nBits)
        {
                case 7:
                        newtio.c_cflag |= CS7;
                        break;
                case 8:
                        newtio.c_cflag |= CS8;
                        break;
        }

        switch(nEvent)
        {
                case 'O':
                        newtio.c_cflag |= PARENB;
                        newtio.c_cflag |= PARODD;
                        newtio.c_iflag |= (INPCK | ISTRIP);
                        break;
                case 'E':
                        newtio.c_iflag |= (INPCK | ISTRIP);
                        newtio.c_cflag |= PARENB;
                        newtio.c_cflag &= ~PARODD;
                        break;                                               
                case 'N':
                        newtio.c_cflag &= ~PARENB;
                        break;
        }

        switch(nSpeed)
        {
                case 2400:
                        cfsetispeed(&newtio, B2400);
                        cfsetospeed(&newtio, B2400);
                        break;
                case 4800:               
                        cfsetispeed(&newtio, B4800);
                        cfsetospeed(&newtio, B4800);
                        break;
                case 9600:                       
                        cfsetispeed(&newtio, B9600);
                        cfsetospeed(&newtio, B9600);
                        break;
                case 19200:       
                        cfsetispeed(&newtio, B19200);
                        cfsetospeed(&newtio, B19200);
                        break;
                case 115200:       
                        cfsetispeed(&newtio, B115200);
                        cfsetospeed(&newtio, B115200);
                        break;
                default:                       
                        cfsetispeed(&newtio, B9600);
                        cfsetospeed(&newtio, B9600);
                        break;
        }
       
        if(nStop == 1)
                newtio.c_cflag &= ~CSTOPB;
        else if(nStop == 2)
                newtio.c_cflag |= CSTOPB;

        newtio.c_cc[VTIME] = 0;
        newtio.c_cc[VMIN] = 0;
         
        tcflush(fd, TCIFLUSH);

        if((tcsetattr(fd, TCSANOW, &newtio)) != 0)
        {
                perror("com set error");
                return -1;
        }
        printf("set done!\n");
        return 0;
}



int main(void)
{
        int fd;
        int nread, i, result;
        char send_buff[N] = "Zigbee_UART Init Success\n";
        char recv_buff[M] = {0};
        fd_set rd;
        struct timeval tv;

        if((fd = open_port(fd, 3)) < 0)
        {
                perror("open_port error");
                return 1;
        }       
        
        if((i = set_opt(fd, 115200, 8, 'N',1)) < 0)
        {
                perror("set_opt error");
                return;
        }
       
        write(fd, send_buff, strlen(send_buff));

        FD_ZERO(&rd);
        FD_SET(fd, &rd);
       
        while(1)
        {                       
                tv.tv_sec = 3;
                tv.tv_usec = 0;

                result = select(fd+1, &rd, NULL, NULL, &tv);
               
                switch(result)
                {
                        case 0:
                                printf("timeout\n");
                                break;
                        case -1:
                                printf("select\n");
                                break;
                        default:
                                if(FD_ISSET(fd, &rd))
                                {                       
                                        memset(recv_buff, 0, M);
                                        if((nread = read(fd, recv_buff, strlen(recv_buff))) > 0)
                                        {
                                                printf("nread=%d, %s\n", nread, recv_buff);
                                                write(fd, recv_buff, strlen(recv_buff));
                                        }
                                }
                }
                FD_ZERO(&rd);
                FD_SET(fd, &rd);
        }
        close(fd);       
        return;
}

论坛徽章:
2
2015年亚洲杯之阿曼
日期:2015-03-23 18:11:212015亚冠之大阪钢巴
日期:2015-09-07 13:54:16
2 [报告]
发表于 2015-12-20 20:19 |只看该作者
newtio.c_cflag != CLOCAL | CREAD;
->
newtio.c_cflag |= CLOCAL | CREAD;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP