Passing Arrays from C++ to Javascript using Nan

36 views
Skip to first unread message

Martin Probst

unread,
Jan 31, 2017, 3:01:15 PM1/31/17
to nodejs
Hi all,

I am using the following C++ code to read values from an ADC. HandleOKCallback is properly called and adc and volt arrays are being filled with data.


 class ReadWorker : public Nan::AsyncWorker {
    public:
      ReadWorker(Nan::Callback *callback, int channel): Nan::AsyncWorker(callback), channel(channel) { }

      void Execute() {
        ...
      }

      void HandleOKCallback() {
        Nan:: HandleScope scope;

        Local<Value> argv[3];
        argv[1] = Nan::New<Array>(*adc),
        argv[2] = Nan::New<Array>(*volt);

        if (!initialized) {
          argv[0] = Nan::Error("failed to initialize ADC");
        } else if (failed) {
          argv[0] = Nan::Error("failed to read ADC");
        } else {
          argv[0] = Nan::Null();
        }

        callback->Call(3, argv);
      }
   private:
    int channel;
    int32_t adc[8];
    int32_t volt[8];
    ...


However the following Javascript yields an error under node.js telling me that adc and volt are not available.

ad.read(channel, function(err, adc, volt) {
   end = new Date().getTime();
    if (err) {
      console.warn('' + err);
    } else {
      var elapsed = end - start;
      console.log("adc: %s, volt: %s%%, time: %dms", adc[7].toFixed(2), volt[7].toFixed(2), end - start);
    }
  });

I assume that there is something wrong with the code highlighted in red above but I don't get it. Any help is appreciated!

Thanks,
Martin. 
Reply all
Reply to author
Forward
0 new messages