Reading from named FIFO on Linux

조회 수: 7 (최근 30일)
Jack Williams
Jack Williams 2017년 8월 17일
댓글: Walter Roberson 2017년 8월 17일
I am trying to read from a named fifo in the /tmp/ folder on the linux file system. I have one fifo which sends data to another named fifo and is recieved by a c++ program. Now I need to do that in reverse but I am struggling to recieve any data in MATLAB. I have some test code built in to my main matlab program at the point which the file needs to be read:
disp('Trying to read')
formatSpec = '%s %s';
count = 1;
while 1
disp(count)
A1 = fscanf(fileID_slam,formatSpec);
A2 = textscan(fileID_slam,formatSpec);
disp(A1)
disp(A2)
count = count + 1;
end
Assume the pipes were correctly setup.
What happens is it reaches disp(count) and then blocks until the c++ program ends and then loops infinitely reading [0x1] matrices and the current count value.
The C++ code is simply a loop which does the following:
pipe << test_num << std::endl;
pipe.flush();
test_num++;
If anyone has any advice on the best reading function to use in MATLAB for this situation or any reason why the read function might be blocking I would really appreciate it.

채택된 답변

Walter Roberson
Walter Roberson 2017년 8월 17일
The line
A2 = textscan(fileID_slam,formatSpec);
always means that textscan() is to read until either end of file or a format mismatch. Because the %s format always skips leading whitespace including possibly multiple end of lines, there is no way to get a format mismatch for %s format. Therefore your textscan line is always going to read until end of file is signaled.
You could use fscanf to read a single line, just like you do for A1; or you could use add a repeat count of 1 after the formatSpec to cause it to only use the format once. But keep in mind what I said about %s potentially skipping multiple end of line.
I am unclear as to why you are using the format to read two entries at a time, seemingly expecting them to be on one line, when your C++ appears to be generating one number per line.
... and where is your end of file test?
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 8월 17일
(You have tabs in your C/C++ code; tabs are nearly ignored by the formatter, and the formatter does not know the are whitespace at the beginning of lines to trigger code presentation mode.)
Walter Roberson
Walter Roberson 2017년 8월 17일
Perhaps you could use
!od -cx /tmp/WHATEVERNAME
to examine the temporary file to make sure it is sound? Perhaps you could attach one of the sample /tmp files?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Use Prebuilt MATLAB Interface to C++ Library에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by