[chuck-users] Communicate with a C++ programme (Guille Elias Alonso)

Perry Cook prc at CS.Princeton.EDU
Thu May 12 16:34:23 EDT 2016


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 <stdlib.h>
#include <stdio.h>
#include <string.h>

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 at 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 <guille_elias_alonso at hotmail.com>
> To: "chuck-users at lists.cs.princeton.edu"
> 	<chuck-users at lists.cs.princeton.edu>
> Subject: [chuck-users] Communicate with a C++ programme
> Message-ID: <DUB125-W7866DF5CBF0938708FB36CB3710 at phx.gbl>
> 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 		 	   		  
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20160510/cf395d6b/attachment-0001.html>
> 


More information about the chuck-users mailing list