Reading from named FIFO on Linux

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.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Ag. de 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 comentarios

Jack Williams
Jack Williams el 17 de Ag. de 2017
Editada: Walter Roberson el 17 de Ag. de 2017
That was one of the most clear explanations I have seen so far thank you. Apologies for the crudeness of the example code with regard to number of entries. I was just trying to see if any extra scan tokens would give me anything that was hidden.
I have converted my code to try to use a text file in /tmp/ which the c++ program writes to and then closes and the loop in matlab attempts to use either textscan or fscanf to get anything from it and it always returns an empty vector. Not sure why I am so unable to transfer data over files. there is definitely data being printed to the file I just cant get it in to matlab. The text file contains a single integer
disp('Trying to read')
formatSpec = '%d';
while 1 % stop condition ommitted for testing
frame = fscanf(fileID_slam_file,formatSpec);
if frame ~= last_frame
disp(frame)
% Do stuff with data
end
last_frame = frame;
end
Sorry if the C code is messy I cannot get it to format like MATLAB code. This block is executed every loop and so the number being passed in to the file is constantly incrementing.
slam_file.open(slam_f);
if (slam_file.is_open()){
slam_file << args->frame_num << std::endl;
}
else{
std::cerr << "Error - opening slam file" << std::endl;
exit(0);
}
slam_file.close();
Walter Roberson
Walter Roberson el 17 de Ag. de 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.)
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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 17 de Ag. de 2017

Comentada:

el 17 de Ag. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by