• serial programming using termios


    Hi guys
    I'm trying to connect to CISCO router using termios. So I decided to have two threads one for reading data and the other one for writing data.
    And here's my code :
    
    
    int mainfd=0;
    char ch[2] = {NULL};
    
    void *write(void *)
    {
        char temp;
        while(1)
        {
            temp = getchar();
            ch[0] = temp;   ch[1] = '';
            if(temp == '~')
            {
                printf("connection closed.
    ");
                close(mainfd);
                pthread_exit(NULL);
            }
            check=write(mainfd, ch, 1);
            ch[0]='';
        }
    }
    
    void *read(void *)
    {
        char outputbuffer[10000]= {0};
        while(1)
        {
                    outputbuffer[0]='';
                    int charnumber=read(mainfd, &outputbuffer, sizeof(outputbuffer));
                    outputbuffer[charnumber] = '';
                    printf("%s",outputbuffer);
                    outputbuffer[0] = '';
        }
    }
    
    int main(int argc,char *argv[])
    {
        //////////////////
        struct termios old = {0};
        if (tcgetattr(0, &old) < 0)
            perror("tcsetattr()");
        old.c_lflag &= ~ICANON;
        old.c_lflag &= ~ECHO;
        old.c_cc[VMIN] = 1;
        old.c_cc[VTIME] = 0;
        if (tcsetattr(0, TCSANOW, &old) < 0)
             perror("tcsetattr ICANON");
        //////////////////
        struct termios options;
        static int portnum=atoi(argv[1]);
    
        mainfd = open_port(portnum);
    
        fcntl(mainfd, F_SETFL, FNDELAY);  
        tcgetattr(mainfd, &options);
        cfsetspeed(&options, speed);
        options.c_cflag |= (CLOCAL | CREAD);
        options.c_cflag &= ~PARENB;
        options.c_cflag |= CSTOPB;
    
        options.c_cflag &= ~CSIZE;
        options.c_cflag |=  CS8;
        options.c_cflag &= ~CRTSCTS;
        options.c_iflag &= ~(ISTRIP|ICRNL);
        options.c_oflag &= ~OPOST;
        options.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
        options.c_cc[VMIN] = 1;
        options.c_cc[VTIME] = 0;
        //
        tcsetattr(mainfd, TCSAFLUSH, &options);
        pthread_t threads[2];
        pthread_create(&threads[0], NULL, write, NULL);
        pthread_create(&threads[1], NULL, read, NULL);
        pthread_exit(NULL);
    }
     Edit & Run
    
    
    The problem is that I have to add sleep(2) (at least 2s) after line number 17
    otherwise the output would not be what I expect. Without this sleep command, every character I type, the output is shown with next character not at time :
    1
    2
    Router>
    Router>abc // While I typed "abcd". If I continue with typing "e", then the output will be "abcd" and so on ... 
    
    
    How can I fix this ?
     Mar 22, 2013 at 2:54am
    tcs (519)
    Hmmm...
    
    Everything seems to be ok. I cannot find a bug by code review only. As my computer doesn't has a serial communication line, I cannot try it in practice.
    
    May be your router doesn't echo a character received as soon as possible? F.e. it may use its own VTIME > 0 or CR/LF as read termination condition?
    
    (The only optimization may be to omit the NUL termination in line 10. You may use &tmp instead of ch as parameter to write(2). But this surely has nothing to do with your question).
     Mar 22, 2013 at 4:31am
    kooth (743)
    Try setting c_cc[ VTIME] = 1 instead of zero and see what that does.
     Apr 6, 2013 at 2:26pm
    xubin (9)
    I still have the problem. I've tested many ways, but the problem still remains. I'm gonna give up :(
    Do you guys have any ideas?
  • 相关阅读:
    如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?
    C#实现执行数据库事务案例
    RGB色彩对照表
    C# list.ForEach用法
    C# 实现list=list.OrderBy(q=>q.字段名).ToList(); 按多个字段排序
    IIS 7如何实现http重定向https
    Windows 2008 R2上配置IIS7或IIS7.5中的URLRewrite(URL重写)实例
    MVC网站发布到 IIS
    完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    js返回上一页的实现方法
  • 原文地址:https://www.cnblogs.com/iwana/p/12896133.html
Copyright © 2020-2023  润新知