LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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);

 

Message 1 of 13
(9,950 Views)

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
0 Kudos
Message 2 of 13
(9,924 Views)

@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 Smiley Mad 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 Smiley SadI 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.

0 Kudos
Message 3 of 13
(9,918 Views)

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

0 Kudos
Message 4 of 13
(9,773 Views)

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/

0 Kudos
Message 5 of 13
(9,769 Views)

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
0 Kudos
Message 6 of 13
(9,754 Views)

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.

 

 

0 Kudos
Message 7 of 13
(9,295 Views)

I was trying to get Labview to write to the console and I ended up making this dll and vi project to write to the console. Attached

Download All
0 Kudos
Message 8 of 13
(8,646 Views)

Hi.

I have built a .dll that writes to the standard output (attached) the source code is here:

 

// stdout test.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include <stdio.h>
 
extern "C" __declspec(dllexport) void printSomething(void)
{
    printf("Hello World\n");
    fflush(stdout);

}

When using the above stdout "Example Output.vi" I do not seem to get any response down the stdout in LabVIEW. Could someone tell me if I am misunderstanding how this works?
Many thanks, Alec

0 Kudos
Message 9 of 13
(7,525 Views)

Hello alcjcook,

 

Could you specify what you mean by "stdout" in LabVIEW? are you trying to have a something show up in LabVIEW?

JY
Application Engineer, RF and Communications
National Instruments
0 Kudos
Message 10 of 13
(7,494 Views)