I am using a function to import a C++ addon to my flow. As I want to send synchronous data and recieve it in Node-RED, the C++ addon is treated as a function, and I can only return one array at the end of the function which will be the payload returned from the "require" instruction. However I want to send an array in each iteration in a for loop in the addon.
My question is: I thought about using "printf" in the addon which allows me to output elements of the array in the Node-RED system window. Now can I use this output in my flow and consequently in a function? or is there another way to solve the problem without having to use the "printf" instruction?
I am using Native Abstractions for nodejs, is that what you mean?
The code reads digital samples from an osciloscope. It has a while loop, in each iteration the device's buffer is read and the device write the read samples into an array . The while loop goes on until a desired number of samples has been read and accumulated into the array.
I want to return what the array has new samples in each iteration of the while loop, but I can't since I can only return one value using info.Getreturnvalue
With out knowing more about the API you've build for you native node it's had to say. But I would suggest implementing it as a event emitter and having it emit an event with the array for each block of samples.
And then I would write a custom Node-RED node to wrap it as that will be easier than trying to use a function node. Creating Nodes : Node-RED
Your advice was pretty helpful. I impelented an event emitter that emitts an event whenever a new value arrives. Couldn't send an array and got an error so I had to send individual samples rather than arrays.
I have one remaining issue coming up. The javascript code is working on VS Code, but when I put it inside of a function node, the Node-RED server exits and I have to call back again using node-red.
The same problem happens when wrapping the code in a Node-RED node.
This is the code I have:
const EventEmitter = require('events');
const test = require('C:/Users/mylaptop/source/repos/TestAD2/build/Release/addon');
const emitter = new EventEmitter();
emitter.on('something',(evt) => {msg.payload=evt; node.send(msg)});
test.Hello(emitter.emit.bind(emitter))
Also if you still want to use an input to start/stop sending samples you can re-arrange the code you have to do that, but you should only call require once (outside the on('input'...) handler
Tried that also, but Node-RED keeps exiting. I also tried sending blocks of samples with each event to reduce the number of events emitted, but still the same problem occurs. Do you think I should post a new topic about it?
I am able to print the arrays using console.log, but I can't send them, and I can't see them in the debug node, even though I can see from this picture that some send attempts have actually been made before force exit
I'm going to guess that trying to send a message for every sample is VERY unlikely to work at any normal oscilloscope sampling rate, the number of messages is just going to be way too high.
You need to go back to your native code and work out how to only send the collections to get the message rate down to something managable.
Everything is working properly now.
But the problem is that node.send(msg) sends the messages after the addon has finished its execution, i.e after 60000 samples has been sent. Then at that time I can see them in my debug node. Can I send each message to the next node upon arrival and see them simultaneously?
Thanks a lot for your help!