Re: [chuck-users] Communicate with a C++ programme (Guille Elias Alonso)
It’s pretty old-school, but often my solution for this is to pipe text output of ChucK into stdin of the Cxx program in a terminal. You need to be sure to send to chout and not just use the <<< “PRINT THIS” >>>; form, because the latter prints to stderr, not stdout. Here’s a simple ChucK program that writes a random number to stdout every 16 ms. Below that is a very simple C program to read and report. You’d need to have your C++ program read from stdin and use the argument(s) accordingly. You’d invoke the whole thing like this:
chuck MyChucK.ck | myCProgram
where myCProgram is your compiled C++ executable.
// MyChucK.ck ChucK Program to write random numbers to stdout
while (true) {
16::ms => now;
chout <= Std.ftoa(Math.random2f(0.0,3.14159),4); // 4 decimal places
chout <= "\n"; // newline
chout.flush(); // be sure and do this!!
}
// END OF CHUCK PROGRAM TO WRITE FLOATS
// myCProgram.c a C Program to read single floats from Stdin
#include
On May 10, 2016, at 2:09 PM, chuck-users-request@lists.cs.princeton.edu wrote:
1. Communicate with a C++ programme (Guille Elias Alonso) 2. Re: Communicate with a C++ programme (Stephen D Beck) 3. Re: Communicate with a C++ programme (Guille Elias Alonso)
----------------------------------------------------------------------
Message: 1 Date: Tue, 10 May 2016 22:53:48 +0200 From: Guille Elias Alonso
To: "chuck-users@lists.cs.princeton.edu" Subject: [chuck-users] Communicate with a C++ programme Message-ID: Content-Type: text/plain; charset="iso-8859-1" Hello guys, I'm writing a programme that analyses the micro input and makes some actions according to the analysis. Everything about the analysis is written in Chuck. The analysis of the micro signal gives a single number every 16 ms, and this number has to be sent to the C++ programme but I don't know how I can connect the two parts. This application is highly time-sensitive so I need a fast way of communicating. I had read about these OSC events but it seems it's only for Chuck programmes receiving input from the outside and I want just the opposite. I've also thought about some mechanism of shared memory between the Chuck programme and the C++ programme but I don't see anywhere some way of implementing that with Chuck. Do you have any ideas? Thanks,Guillermo
That makes everything much simpler and it's probably faster thanks!
From: prc@CS.Princeton.EDU Date: Thu, 12 May 2016 13:34:23 -0700 To: chuck-users@lists.cs.princeton.edu Subject: Re: [chuck-users] Communicate with a C++ programme (Guille Elias Alonso)
It’s pretty old-school, but often my solution for this is to pipe text output of ChucK into stdin of the Cxx program in a terminal. You need to be sure to send to chout and not just use the <<< “PRINT THIS” >>>; form, because the latter prints to stderr, not stdout. Here’s a simple ChucK program that writes a random number to stdout every 16 ms. Below that is a very simple C program to read and report. You’d need to have your C++ program read from stdin and use the argument(s) accordingly. You’d invoke the whole thing like this:
chuck MyChucK.ck | myCProgram
where myCProgram is your compiled C++ executable.
// MyChucK.ck ChucK Program to write random numbers to stdout while (true) { 16::ms => now; chout <= Std.ftoa(Math.random2f(0.0,3.14159),4); // 4 decimal places chout <= "\n"; // newline chout.flush(); // be sure and do this!! } // END OF CHUCK PROGRAM TO WRITE FLOATS
// myCProgram.c a C Program to read single floats from Stdin #include
#include #include int main() { char inString[256]; float inVal; while (1) { fgets(inString,256,stdin); // read standard input inVal = atof(inString); // peel off float value inString[strlen(inString)-1] = 0; // kill the newline printf("Got it!! %s, %f\n", inString, inVal); // report!! } return 1; } // END C PROGRAM TO READ FLOATS
On May 10, 2016, at 2:09 PM, chuck-users-request@lists.cs.princeton.edu wrote:
1. Communicate with a C++ programme (Guille Elias Alonso) 2. Re: Communicate with a C++ programme (Stephen D Beck) 3. Re: Communicate with a C++ programme (Guille Elias Alonso)
----------------------------------------------------------------------
Message: 1 Date: Tue, 10 May 2016 22:53:48 +0200 From: Guille Elias Alonso
To: "chuck-users@lists.cs.princeton.edu" Subject: [chuck-users] Communicate with a C++ programme Message-ID: Content-Type: text/plain; charset="iso-8859-1" Hello guys, I'm writing a programme that analyses the micro input and makes some actions according to the analysis. Everything about the analysis is written in Chuck. The analysis of the micro signal gives a single number every 16 ms, and this number has to be sent to the C++ programme but I don't know how I can connect the two parts. This application is highly time-sensitive so I need a fast way of communicating. I had read about these OSC events but it seems it's only for Chuck programmes receiving input from the outside and I want just the opposite. I've also thought about some mechanism of shared memory between the Chuck programme and the C++ programme but I don't see anywhere some way of implementing that with Chuck. Do you have any ideas? Thanks,Guillermo
participants (2)
-
Guille Elias Alonso
-
Perry Cook