LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read stdio of another program

Hi,

I have a program that reads data from PC serial port and prints that into the command prompt (stdio) in text format.

The idea has been that the output is redirected to a file and then the file is analysed off-line with Excel or Matlab.

 

Now I would like to make a front-end program with LabWindows that shows the data graphically in real-time.

I don't want to change the original program that reads the port and prints the data to command prompt as it is needed as such also in the future.

 

The question is how to read the stdio of another program in real-time with LabWindows?

 

I have tried LaunchExecutable() to start the other program from LabWindows but cannot get hold of the data printed on the command prompt.

There are examples of DDE-solutions for two programs to exchange data but this seems an overkill for my problem as there is only one-directional data that I want to read into the Labwindows program.

 

I thought that it would be quite common to implement such a graphical front-end to display real-time graphics of some old text-based program. Amazing that there seems not to be good and easy solutions for LabWindows.

(I did implement a quick solution with Qt but LabWindows would offer much better graphical elements for drawing the measurement data.)

 

Thanks!

 

0 Kudos
Message 1 of 6
(3,261 Views)

Hello ejt,

could this thread offer some useful information to you?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(3,257 Views)

Hi,

Thank you for the link.

I implemented those short programs in LabWindows and tried them. Yes, works perfectly!

Write-program's output is redirected to a file with ">" and the Read-program can read the file even if the programs are running simultaneously.

 

The bad news is that this does not work with the program that I need to use.

There is a small difference in the behavior of the programs that write output to the command-promt. I don't understand why.

1) The small Write-program implemented from the link opens a new command-prompt where the output goes if there is no redirection. And if there is the redirection ">" it still opens the new command-prompt but it stays empty as the ouput goes to the file. (And the Read-program can read the file; everything works okay.)

2) The program that I need to use does not open a new command-prompt when started; it's output goes to the same command-prompt where it is started from. With redirection the output goes to the file; no new command-prompt this time either. But the problem is that the reading program cannot read the file. The Read-program can read the file if it has been created and written before. But not if it tries to read it at the same time that it is written by the other program.

 

Maybe this is a simple problem if you know how to solve it... 😕

0 Kudos
Message 3 of 6
(3,242 Views)

Hi ejt,

 

The reason why the program you wrote is opening a new console window when it generates output is probably because you did not mark it as a console program (Build>>Target Settings>>Create Console Application option).

 

However, it's not likely that that is the reason why your reader program cannot read the file that your other program redirects to. Do you know what the error is that fopen is returning? Since you mentioned that it can read the file when the file already existed, that makes it seem as if the problem is that the reader is trying to read the file too soon, before it's even been created by the write. In programming, there's almost never such a thing as "at the same time", so you need to guard against the possibility that you're trying to read it too soon. What your reader program should do is to loop, while checking for the existence of the file, before it tries to open it for reading.

 

Luis

0 Kudos
Message 4 of 6
(3,228 Views)

Hi,

That's a good point but I have checked several times and that is unfortunately not the problem.

No matter how long I wait before starting the Reading-program it cannot read the file simultaneously when the other program is writing into the file.

If I use append redirection ">>" the Reading-program reads what was already in the file but cannot read from the point where the other program started to append new data in.

Of course it is possible that the operating system (Windows) buffers the data somewhere (?) and flushes it into the file only when the program is terminated.

But this kind of buffering does not happen with the small Writing-program I implemented from the linked example.

 

I still think that there can be a very simple solution if somebody just happen to know it.

Hoping for it...

0 Kudos
Message 5 of 6
(3,215 Views)

I don't really have any other ideas right now but it might help to know how exactly the failure is manifesting itself. Is it that the fopen call fails, or is it that you can open it but then you get an EOF when you try to read the new data?

 

Also, assuming that you have access to the source code of the writer program, can you start modifying it until you get it to look pretty much like the other small writer program that you did get to work? Given that you have one program that works and a similar one that doesn't, what I would normally do in that situation would be to gradually morph one program into the other until I found the relevant difference.

 

Luis

0 Kudos
Message 6 of 6
(3,208 Views)