NI Home > Community > NI Discussion Forums

LabVIEW

Reply
Member
myle00
Posts: 117
0 Kudos

Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

Continuing from the comments in this idea: http://forums.ni.com/t5/LabVIEW-Idea-Exchange/Access-to-console-output/idc-p/1768066 here's a set of VIs that let you get stuff you write to stderr and stdout in a dll loaded by LabVIEW into LabVIEW. It basically works by redirecting these std ios through a pipe and reading the pipe in LabVIEW.

 

For stderr and stdout, just run the "example output" VI and while that is running write to stdout or stderr from a dll loaded in LabVIEW like the following code or using the "STD write" VI and it'll show up in the "example output" VI's indicator.

 

extern "C"	__declspec(dllexport) void __stdcall STDerr(char * str)
{
	fprintf(stderr, "%s\n", str);
	fflush(stderr);
}

 Note, I had to add both a new line and fflush, otherwise it didn't consistently get to LabVIEW.

 

Now, for stdin I was having a bit of trouble. Doing the same thing (running the "example input" VI) resulted in a string control not being read in the loop. Just run the "example input" VI and while it's running change the "string to write" control to different text and you'll see that the new text will only be written after you press the stop button which doesn't make any sense. 

 

The second issue I had was when I tried to read stdin from the dll loaded by LabVIEW. Even with trying AllocConsole() I always got EOF (e.g. when calling getchar(), see the code) independently of the VIs, it just seemed that stdin was disabled so I couldn't read from it, yet AllocConsole should have enabled it but it didn't. Also, printing to the console didn't actually display anything on the console. This may have something to do with this thread: http://forums.ni.com/t5/LabVIEW/using-stdin-stdout-with-LabVIEW/td-p/361573

 

Anyway, I if someone can get stding to work, I imagine they would be able to use the second set of VIs to write into stdin from LabVIEW and read it in the dll.

 

        //AllocConsole();
	FILE* f;
	fopen_s(&f, "test stdin.txt", "w");
	for (int i= 0;i<100;++i)
	{
		fprintf(f, "%d\n", getchar());
	}
	//FreeConsole();
	fclose(f);

 

Member
ie.aleja
Posts: 49
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

Hello myle,

 

I ran the VI's it seems that it's writing and reading, I used the example input. I don't know exactly what it is that you want to fix on this VIs, if you give me more information I can work on it.

 

Regards,

Miriam
Field Applications Engineer
NI Colombia
CLD
Member
myle00
Posts: 117
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.


ie.aleja wrote:

Hello myle,

 

I ran the VI's it seems that it's writing and reading, I used the example input. I don't know exactly what it is that you want to fix on this VIs, if you give me more information I can work on it.

 

Regards,


You can write off the first issue as user error :smileymad: I updated the string control to new text, but I never clicked out of the string control until I clicked the stop button so the new string only got read when I hit the stop button :smileysad:I didn't realize/forgot that the string control only updates to the new text once you click out of it. So it does work as expected.

 

Thanks.

Member
FabioBone
Posts: 28
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

Please, can you post the files saved for Labview 8.2? Thank you.

Member
VGA_CD-ROM
Posts: 162
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

There is a property associated with string controls called "Update While Typing", accessible via right-click menu or as a property node. I think it's typically used with event structures, but if you need that kind of functionality, it might be useful to set in your program too.

 

http://zone.ni.com/reference/en-XX/help/371361H-01/lvhowto/update_value_while_typing/

http://zone.ni.com/reference/en-XX/help/371361H-01/lvprop/strng_updt_while_typing/

Regards,
Vijay S.
National Instruments
Member
ColdenR
Posts: 167
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

If you want files converted to LabVIEW 8.2, you can visit the Version Conversion discussion forum to request someone to convert the files for you:

http://forums.ni.com/t5/Version-Conversion/bd-p/VersionConversion

Colden Rouleau

Product Support Engineer | NI-RIO R&D | National Instruments
Member
Justin@broadcom
Posts: 1
0 Kudos

Re: Redirecting stdout/stderr/stdin to/from LabVIEW in dlls.

I tried this method in Labview 2012.  However the fflush() is causing labview to crash!

 

I can't seem to get any output without the flush either.