Reading from named FIFO on Linux

4 Ansichten (letzte 30 Tage)
Jack Williams
Jack Williams am 17 Aug. 2017
Kommentiert: Walter Roberson am 17 Aug. 2017
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Aug. 2017
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 Kommentare
Walter Roberson
Walter Roberson am 17 Aug. 2017
(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 am 17 Aug. 2017
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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by