Using Function node to input C/C++ codes instead of JavaScript.

3,027 views
Skip to first unread message

Dibyanshu Jaiswal

unread,
Apr 27, 2015, 3:07:57 AM4/27/15
to node...@googlegroups.com
Hi!
I have been using Node-RED since 2 months (with not much knowledge of backend).
I would like to come up with a node similar to function node, where one can input C/C++ codes instead of JavaScript. Having searched on google in this regard I figured out that Node-RED use ORION Editor to provide a a text editor in the application, which supports C/C++, java also.

I tried making a copy of function node provided by Node-RED and added the editor in the same way, changed the lang:"c"

But I am unable to figure out wether this is the right way, as I am not much familiar with Node.js or JavaScript. I have attached the html file for the node i have created.


Further I aim to use this code input to be passed to the exec node as command line argument in the, but since the code contains various \n and spaces things get messed up and returns syntax errors. from the exec node.

Hence with above snapshot of problems my Questions are :

1. How to create a node that excepts C/C++/Java codes from the user?
2. How to use the above input to pass it as Command Line argument to the exec node or any other method?
CCode.html

Antoine Aflalo

unread,
Apr 27, 2015, 3:25:16 AM4/27/15
to Node-RED Mailing LIst
Hello Dibyanshu,
To put it easily, it's simply not possible.

C++/C are complied language, you can interpret them on-the-fly like it's the case for JavaScript. Moreover, it's platform dependent.
What you can do, is create your C/C++ program before hand, compile it and use exec to run it.

Or even, use the function node to "code" and make that code your msg.payload to write it in a file on wich you'll use an exec node to compile it then run it.

In other word, the function node can only be used for JavaScript.



--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Antoine Aflalo

Antoine Aflalo

unread,
Apr 27, 2015, 3:25:51 AM4/27/15
to Node-RED Mailing LIst
Correction : you can't interpret them on-the-fly like it's the case for JavaScript.
--
Antoine Aflalo

Dibyanshu Jaiswal

unread,
Apr 27, 2015, 4:21:04 AM4/27/15
to node...@googlegroups.com

Thank for your prompt reply.
I can understand that interpreting C/C++ on the fly is not possible. My end goal is to fire a script from the exec (say: sourceCodeGenerator.c) node which takes Command line arguments (here : input C code) and hence generate another  .c file as output, compile it and hence run it.

I hope the picture is more clear now.

One wayout could be (as indicated by you) to write the function input code to a file and hence read it from there.. But in this case it may cause problem if i have more than one such CCode input nodes. hence maintaining them (many files) can be troublesome.

Ben Hardill

unread,
Apr 27, 2015, 6:18:57 AM4/27/15
to node...@googlegroups.com
If I've understood what you are trying to do here, I wouldn't use the function node as the starting point.

  • I would look at using the template node  to hold the c/c++ and update it with the input data, then output that to file.
  • Then use the watch node to detect when the file has been written to trigger a exec node to compile the code. You have to do it this way because the file write node is asynchronous so you don't know when it's finished.
  • You would then use a second watch node to detect that the compiler has written the executable to trigger a second exec node to actually run the code.

Now having said all that, I have to ask why you want to modify code on the fly like this?

Wouldn't it be better if you absolutely have to use the C/C++ to make it take the relevant information as command line arguments? This would mean that you only ever compile the code once up front and not have to worry about any compile time errors that may be introduced.

Dibyanshu Jaiswal

unread,
Apr 27, 2015, 8:17:32 AM4/27/15
to node...@googlegroups.com
Thanks for the solution with respect to watch nodes, that was not known to me.

The reason I am upto such a thing is that my objective is to generate a C/C++ file based on some user inputs.

Here user inputs are taken from some other nodes made by me, which are passed as a command line argument in the exec node.

But in some cases the outputs of certain nodes needs to formatted (for example typecast, append something, or  switch parameters from a,b to b,a) to be used by the next node, hence want to provide a node similar to function node where the user writes C codes (to typecast or append something). Hence the user provided C code is then added to the source code generated by the system in the correct place.

If one can have a better solution than what I am trying to achieve, suggestions are always welcome.

--
http://nodered.org
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/1imgt8mDZOk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Dibyanshu Jaiswal
Mb: +91 9038304989


Julian Knight

unread,
Apr 27, 2015, 9:16:12 AM4/27/15
to node...@googlegroups.com
Sounds like the template node would be the best starting point. You don't really need a separate watch node as this would probably be confusing to use. Node.JS is the underlying technology that Node-Red is built on & it has a filing system library that is capable of creating watch functions that will auto-run when a file changes - if your C programme is going to be in a file. If the programme is going to be generated on the fly, it might not need a file other than perhaps some .h files or similar library files. The actual code could be passed in the msg variable if it is small enough and you could probably pass it direct to the compiler - if that is possible, I'm not a C programmer. Otherwise, you can use the Node.js filing system library to write the code to a temporary file then call the compiler on that.

The other question is whether your flow needs to stop and wait for the compilation to happen before continuing or whether you can let the flow continue? If the user has to wait, that could be quite a while.

lingvo

unread,
Oct 17, 2016, 11:11:04 AM10/17/16
to Node-RED
Hi there,

I have a similar question about C/C++ usage in Node-RED with the exception that I don't need to compile C-code on the fly. I have an executable C file for smae platform, where Node-RED is running, which accepts some arguments, performs calculations and generates some output. I want Node-RED to execute this file periodically with arguments, coming from messages, capture the output and forward it to next node(s).
Is it somehow possible?

Mark Setrem

unread,
Oct 17, 2016, 11:21:05 AM10/17/16
to Node-RED
Sounds like a job for the exec node

Julian Knight

unread,
Oct 17, 2016, 4:51:44 PM10/17/16
to Node-RED
Yup, I think lots of us do similar things. For example, I run command line ARP checks every minute to detect known and unknown devices on the network.

Seb D

unread,
Jun 29, 2017, 5:33:24 PM6/29/17
to Node-RED
https://nodejs.org/api/addons.html

This is a recent node that apparently use Node.js C++ native add-on

I'm currently looking into that
Reply all
Reply to author
Forward
0 new messages