主题 : 急---串口问题-ttySAC0  tts/0问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 9256
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2009-09-23
最后登录: 2017-09-13
楼主  发表于: 2010-01-03 13:32

 急---串口问题-ttySAC0  tts/0问题

我在mini2440上面开发短信猫程序

代码如下:
           int   i;
    int   status;
    struct termios   options;
                     char *dev = "/dev/tts/1";   //在网上很多同仁说是ttySAC1, 请问下ttySAC1 和 tts/1 是不是同一个设备。 因为我/dev目录下面没有显示ttySAC1, 为什么?
                    int    fd = open( dev, O_RDWR|O_NONBLOCK |O_NOCTTY);

    if(0 != tcgetattr(fd, &options))
    {
        printf("tcgetattr error errno=%d\n",errno);
        return;
    }
    cfsetispeed(&options, B9600);  
    cfsetospeed(&options, B9600);  

    options.c_cflag |= CS8 | CREAD;
    options.c_cflag &= ~PARENB;   /* Clear parity enable */
    options.c_cflag &= ~CSTOPB;
    options.c_oflag = 0;
    options.c_lflag = 0;
options.c_cflag &= ~CRTSCTS;
//    options.c_cflag |= CRTSCTS;
    options.c_cc[VTIME] = 150;
    options.c_cc[VMIN] = 0;  
    if (tcsetattr(fd,TCSANOW,&options) != 0)  
    {
        printf("set_parity error. errno=%d\n", errno);  
        return (FALSE);  
    }


按照上面打开后, write没有出现错误。但是read时一直得不得任何数据.   请问是是哪里出现错误?? 谢谢, 问题困扰好多天了。
程序在pc linux上可以运行,但移植到arm上后 read 就一直读不到任何数据。   谢谢!!
级别: 新手上路
UID: 9256
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2009-09-23
最后登录: 2017-09-13
1楼  发表于: 2010-01-03 13:37
对了, 短信猫是 mc39i
这个阶段正是我事业的上升期,我怎么能走得开呢?
级别: 精灵王
UID: 3197
精华: 3
发帖: 770
金钱: 6995 两
威望: 5398 点
综合积分: 1600 分
注册时间: 2008-12-30
最后登录: 2010-12-31
2楼  发表于: 2010-01-03 13:46

 Re:急---串口问题-ttySAC0 tts/0问题

mini2440附带的光盘和手册中已经提供了一个串口编程的例子:comtest,仔细搜索一下。
级别: 新手上路
UID: 9256
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2009-09-23
最后登录: 2017-09-13
3楼  发表于: 2010-01-03 14:11

 Re:急---串口问题-ttySAC0 tts/0问题

comtest 代码没有搜到  不过在arm上面测试时
armcomtest –d /dev/ttySAC1 -o  时, pc 超级终端上输入 在arm上面可以看到,但超级终端上显示不出任何字符。 why???

楼上, 可以comtest发代码发给我吗?  谢谢。
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2010-01-03 14:58

 Re:急---串口问题-ttySAC0 tts/0问题

把完整的程序代码贴出来;
把你认为read()函数不正常的地方标出来
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 9256
精华: 0
发帖: 4
金钱: 20 两
威望: 4 点
综合积分: 8 分
注册时间: 2009-09-23
最后登录: 2017-09-13
5楼  发表于: 2010-01-03 15:36

 Re:急---串口问题-ttySAC0 tts/0问题

int new_set_Parity(int fd,int databits,int stopbits,int parity)
{
    int   i;
    int   status;
    struct termios   options;

    if(0 != tcgetattr(fd, &options))
    {
        printf("tcgetattr error errno=%d\n",errno);
        return;
    }
    cfsetispeed(&options, B9600);  
    cfsetospeed(&options, B9600);  

    options.c_cflag |= CS8 | CREAD;
    options.c_cflag &= ~PARENB;   /* Clear parity enable */
    options.c_cflag &= ~CSTOPB;
    options.c_oflag = 0;
    options.c_lflag = 0;
options.c_cflag &= ~CRTSCTS;
//    options.c_cflag |= CRTSCTS;
    options.c_cc[VTIME] = 150;
    options.c_cc[VMIN] = 0;  
    if (tcsetattr(fd,TCSANOW,&options) != 0)  
    {
        printf("set_parity error. errno=%d\n", errno);  
        return (FALSE);  
    }
    
    sleep(2);

    return TRUE;
}    

int testSet(int fd)
{
    int   i;
    int   status;
    struct termios   options;
    speed_t   baud;

    printf("\n===============test======================\n");

    if(0 != tcgetattr(fd, &options))
    {
        printf("tcgetattr error errno=%d\n",errno);
        return;
    }
    
    if(B9600 != (baud=cfgetispeed(&options)))
    {
        printf("testSet : i baud is not equal to B9600, value = %d\n", baud);
    }
    
    if(B9600 != (baud=cfgetospeed(&options)))
    {
        printf("testSet : o baud is not equal to B9600, value = %d\n", baud);
    }

    if(options.c_cflag & CRTSCTS)
    {
        printf("testSet : i/o flow control is CRTSCTS\n");
    }
    else
    {
        printf("testSet : i/o flow control isnot CRTSCTS\n");
    }
    
    if(options.c_cflag & PARENB)
    {
        printf("testSet : PARENB\n");
    }
    else
    {
        printf("testSet : isnot PARENB\n");
    }

    printf("========================end==================\n");

    return 0;
}


int OpenDev(char *Dev)
{
    //int    fd = open( Dev, O_RDWR|O_NONBLOCK | O_NOCTTY | O_NDELAY);    
    int    fd = open( Dev, O_RDWR|O_NONBLOCK |O_NOCTTY);
    //int    fd = open( Dev,  O_RDWR | O_NOCTTY);

    if (-1 == fd)    
    {            
        printf("Can't Open Serial Port, errno=%d\n", errno);
        return -1;        
    }    
    else    
    {
        return fd;
    }
}

int OpenComm(const int  Port)
{

    char *Comm1  = "/dev/tts/1";
    int opendev_ret;
    
    if(1 == Port)
    {
        printf("use %s\n", Comm1);
        opendev_ret = OpenDev(Comm1);
    }    
    else if(2 == Port)
    {
        printf("use %s\n", Comm2);
        opendev_ret = OpenDev(Comm2);
    }    
    else
         return -1;        
    
    if(-1 == opendev_ret)
        return -1;        

    if(FALSE == new_set_Parity(opendev_ret, 8, 1, 'N'))
    {
        printf("Set Parity Error\n");
        return -1;        
    }

    testSet(opendev_ret);

     return opendev_ret;
}

int CloseComm(int CommHandle)
{
     return close(CommHandle);
}    

int ReadComm(int CommHandle, void* pData, int nLength)
{
    fd_set set;
    struct timeval val;

    while(1){
    val.tv_sec = 2;
    val.tv_usec = 0;
    FD_ZERO(&set);
    FD_SET(CommHandle, &set);
    
    int ret = select(1, &set, NULL, NULL, &val);                    // 这个地方,一直都是time out, 刚开始不使用select函数而是直接使用read函数也是出错。
    if(ret)
    {
        printf("start to read data\n");
        if(FD_ISSET(CommHandle, &set))
        {
            return  read(CommHandle ,  pData, nLength);
        }
    }
    else if(ret == 0)
    {
        printf("select timeout\n");
    }
    else if(ret == -1)
    {
        printf("select error : errno = %d, errinfo=%s\n", errno, strerror(errno));
    }    
    }
}

int WriteComm(int CommHandle, const  void* pData, int nLength)
{    
    int writeNum =  write(CommHandle ,  pData, nLength);    
    if(0 != tcdrain(CommHandle))               //使用tcdrain确保所有写数据发送成功。
    {
        printf("tcDrain error: errno=%d, errinfo=%s\n", errno, strerror(errno));
    }

    return writeNum;
}

int main()
{
    char    data[20] = {0};
     int fd = OpenComm(1);
     int writeNum = WriteComm(fd, "AT\r\n", 4);     //这边返回4, 说明写ok
     int  readNum = ReadComm(fd,    data, 20);   // 调用read时, 得不得任何数据
  
     ....................
}
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
6楼  发表于: 2010-01-03 16:30

 回 3楼(lgd_nuaa) 的帖子

comtest的源代码包含在http://www.arm123.com.cn/linux/examples.tgz里面;
comtest\comtest.c
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."