简体   繁体   English

在Linux中将串行设备的I / O重定向到标准I / O的API是什么

[英]what is the api for redirecting i/o of serial device to standard i/o in linux

I am trying to port Vxworks application to Linux. 我正在尝试将Vxworks应用程序移植到Linux。 For redirecting i/o of one of the serial device to standard i/o they are using ioTaskStdSet(); 要将串行设备之一的I / O重定向到标准I / O,请使用ioTaskStdSet();。 in vxworks. 在vxworks中。 But I am not able to find the api in linux like as. 但是我无法像这样在linux中找到api。 Only duplicating device is available in linux that is also not working in my application. 在Linux中只有复制设备可用,但在我的应用程序中也不起作用。

Can anyone help me out in this? 有人可以帮我吗?

As @Ignacio mentioned dup2 is used exactly for this purpose. 正如@Ignacio提到的,dup2正是用于此目的。 For example this program redirect stdout to "echo" file: 例如,该程序将标准输出重定向到“ echo”文件:

#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int fd = open("echo", O_RDWR | O_CREAT, 00644);
    if(fd == -1)
        return 1;

    if(dup2(fd, 1) == -1)
        return 1;
    close(fd);

    system("echo Hi!");
    return 0;
}
int main()
{
    for(i=0;i<4;i++)
    {
        sprintf(dev_name,"tsports%d",i);
        fd[i] = open(dev,O_RDWR | O_SYNC);
        pthread_create(tid[i],NULL,&thread_fun,(void *)fd[i]);
    }
    pthread_exit(NULL);
}
int thread_fun(void *chan)
{
     int new_fd,old_fd;
     old_fd = (int)chan;

     new_fd = dup2(old_fd,0);
     new_fd = dup2(old_fd,1);
     ts_fd = old_fd;
     tn();
     pthread_exit(NULL);
}
void tn()
{
     printf("hello on terminal");
     while(1)
     {
          read(ts_fd,&ch,1);
          /* command line */
          ........
          ........
          ........

          /* starts telnet client application on terminal port*/         
          .......
       ......
           ......
          .......
          sleep(1);
     }
}

 In the above code while taking input from terminal 1 it is again redirecting stdi/o to terminal 2 in second thread.

So i want thread specific stdi/o even though redirected on each terminal. 所以我想要线程特定的stdi / o,即使在每个终端上重定向。 That is available in Vxworks as IoTaskStdSet API. 在Vxworks中可以作为IoTaskStdSet API使用。 Is it possible to implement in linux. 是否有可能在linux中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM