Monday, 1 May 2017

execute the child process execSync using node v7.9.0

I'm a beginner in node.js and I appreciate if anyone can help me. I've done a server using express, the code is below. When some button is pressed on the website is executed 'clientEvent' on the server. Inside there, it is a for loop which allows executing an executable C program 'sudo ./dac8532_test %s %s' in order to generate different voltages in a 2-channel DAC converter. This executable program needs two input arguments for generating a voltage for each channel. This value should change while the for loop is executing. Unfortunately, there is an error "Error command failed: ./dac8532_test 0 0" and "segmentation fault". I know that the problem is originated by replacing these values ('i') in the variable com_dac. Anyone know how to correct this problem? I would really appreciate it. :)

    var express = require('express');
    var fs = require('fs');
    var util = require('util');
    var jsonfile = require('jsonfile');       
    var app = express();
    var http = require('http').Server(app);
    var exec = require('child_process').exec;
    var spawnSync = require('child_process').spawnSync;
    var execSync = require('child_process').execSync;
    var io = require('socket.io')(http);
    var serFunc = require('./serverFunctions.js');

    app.get('/', function(req,res){
        res.sendFile(path.join(__dirname + '/public/index.html'));
    });

    //Static Directories
    app.use(express.static(path.join(__dirname + '/public')));

    io.on('connection', function(socket){ 
        console.log('An user is connected')

    socket.on('clientEvent', function(data){
        var param = data;
        var i=parseFloat(param[0]);
        var incr= parseFloat(param[1]);
        var val_max=parseFloat(param[2]);
        var ADC_read = new Array();
        var ANI0 = new Array();
        var ANI1 = new Array();
        var ANI2 = new Array();
        var ANI3 = new Array();
        var ANI4 = new Array();
        var ADC_vector = new Array();
        var com_dac = "sudo ./dac8532_test %s %s";
        var listOfObjects = [];

        console.log(param);

        for(i; i<= val_max; i+=incr ){

        execSync(com_dac.replace("%s",i).replace("%s",i)); //Here is the problem  :( !!!!
        spawnSync("sudo",['./ads1256_probe', 'adc.txt']);
        ADC_read = fs.readFileSync("adc.txt").toString()
        ADC_vector = ADC_read.split(" ");
        ANI0.push(parseFloat(ADC_vector[0]));
        ANI1.push(parseFloat(ADC_vector[1]));           
        }; 
    });

    socket.on('disconnect', function(data){
    console.log('An user is disconnected');
    });   
});     

//Server Starting
http.listen(8080, function(err){
    if(err){
    console.log('Error starting http server');
    } else{
    console.log('Sever running at http://localhost:8080 ');
    }
});



via Margarita Gonzalez

No comments:

Post a Comment