如何使用管道进行非阻塞IPC(UART仿真)

时间:2010-12-22 11:28:08

标签: linux ipc pipe

问题:

我想编写一些模拟串口连接的测试/仿真代码。该 实际代码如下所示:

  

DUT< - UART - > testtool.exe

我的计划是在linux上使用创建测试应用程序(CodeUnderTest.out),该应用程序使用两个(读取和写入)命名管道作为参数来启动testool.out。但我无法弄清楚如何使所有管道IO无阻塞!

设置如下:。

  

CodeUnderTest.out< - 命名管道 - > testTool.out(从CodeUnderTest.out发起)

我试过打开管道如下:

open(wpipe,O_WRONLY|O_NONBLOCK);
open(rpipe,O_RDONLY|O_NONBLOCK);

但是写入会阻塞,直到读者打开wpipe。接下来我尝试了以下内容:

open(wpipe,O_RDWR|O_NONBLOCK);
open(rpipe,O_RDONLY|O_NONBLOCK);

但是第一条消息的读者永远不会获得任何数据(不会阻止)

我还尝试在每条消息周围添加打开和关闭调用,但这也无效......

测试代码:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

pid_t pid;
char* rpipe, *wpipe,*x;
FILE *rh,*wh;
int rfd,wfd;

void openrpipe( void )
   {
   rfd = open(rpipe,O_RDONLY|O_NONBLOCK);
   rh = fdopen(rfd,"rb");
   printf("%sopeningr %x\n",x,rh);
   }
void openwpipe( void )
   {
   //Fails when reader not already opened
   //wfd = open(wpipe,O_WRONLY|O_NONBLOCK);
   wfd = open(wpipe,O_RDWR|O_NONBLOCK);
   wh = fdopen(wfd,"wb");
   printf("%sopeningw %x\n",x,wh);
   }
void closerpipe( void )
   {
   int i;
   i = fclose(rh);
   printf("%sclosingr %d\n",x,i);
   }
void closewpipe( void )
   {
   int i;
   i = fclose(wh);
   printf("%sclosingw %d\n",x,i);
   }
void readpipe( char* expect, int len)
   {
   char buf[1024];
   int i=0;
   printf("%sreading\n",x);
   while(i==0)
      {
      //printf(".");
      i = fread(buf,1,len,rh);
      }
   printf("%sread (%d) %s\n",x,i,buf);
   }
void writepipe( char* data, int len)
   {
   int i,j;
   printf("%swriting\n",x);
   i = fwrite(data,1,len,rh);
   j = fflush(rh); //No help!
   printf("%sflush %d\n",x,j);
   printf("%swrite (%d) %s\n",x,i,data);
   }
int main(int argc, char **argv)
   {
   rpipe = "readfifo";
   wpipe = "writefifo";
   x = "";
   pid = fork();
   if( pid == 0)
      {
      wpipe = "readfifo";
      rpipe = "writefifo";
      x = "   ";
      openrpipe();
      openwpipe();
      writepipe("paul",4);
      readpipe("was",3);
      writepipe("here",4);
      closerpipe();
      closewpipe();
      exit(0);
      }
   openrpipe();
   openwpipe();
   readpipe("paul",4);
   writepipe("was",3);
   readpipe("here",4);
   closerpipe();
   closewpipe();
   return( -1 );
   }

顺便说一句:

要使用上面的测试代码,您需要在当前目录中创建2个管道:

  

mkfifo ./readfifo

     

mkfifo ./writefifo

UPDATE:

好吧我觉得我现在有正确的设置。如果可以做得更好,请告诉我

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

pid_t pid;
char* rpipe, *wpipe,*x;
int rfd,wfd;

FILE* openpipe( char* str, char* access )
   {
   FILE* fh;
   rfd = open(str,O_RDWR|O_NONBLOCK);
   fh = fdopen(rfd,access);
   printf("%sopen(%s,%s)=%x\n",x,str,access,fh);
   return(fh);
   }
void closepipe( FILE* fh )
   {
   int i;
   i = fclose(fh);
   printf("%sclosing %d\n",x,i);
   }
void readpipe( char* expect, int len, FILE* fh)
   {
   char buf[1024];
   int i=0;
   printf("%sreading\n",x);
   while(i==0)
      {
      //printf("%c",strlen(x)?'.':'#');
      i = fread(buf,1,len,fh);
      }
   buf[i] = 0;
   printf("%sread (%d) %s\n",x,i,buf);
   }
void writepipe( char* data, int len, FILE* fh)
   {
   int i=0,j;
   printf("%swriting\n",x);
   //while(i==0)
   i = fwrite(data,1,len,fh);
   j=fflush(fh);
   printf("%sflush %d\n",x,j);
   printf("%swrite (%d) %s\n",x,i,data);
   }
int main(int argc, char **argv)
   {
   FILE *rh,*wh;
   rpipe = "readfifo";
   wpipe = "writefifo";
   x = "";
   pid = fork();
   if( pid == 0)
      {
      FILE *rh,*wh;
      wpipe = "readfifo";
      rpipe = "writefifo";
      x = "   ";
      rh=openpipe(rpipe,"rb");
      wh=openpipe(wpipe,"wb");
      writepipe("paul",4,wh);
      readpipe("was",3,rh);
      writepipe("here",4,wh);
      closepipe(rh);
      closepipe(wh);
      exit(0);
      }
   rh=openpipe(rpipe,"rb");
   wh=openpipe(wpipe,"wb");
   readpipe("paul",4,rh);
   writepipe("was",3,wh);
   readpipe("here",4,rh);
   closepipe(rh);
   closepipe(wh);
   return( -1 );
   }

2 个答案:

答案 0 :(得分:1)

通常,为此类测试模拟串行端口的最佳方法是使用伪终端,因为串行端口是tty,因此是pty

posix_openpt()grantpt()unlockpt()ptsname()是您需要的电话。主机侧由设备仿真器读取和写入,从机侧作为串行端口传递给正在测试的程序。

答案 1 :(得分:0)

在有读者之前不要写入管道。您应该能够使用selectpoll来了解读者何时联系。