0

I am very beginner in NodeJS, I am taking data from S71200 PLC device using nodes7 library, I want to pass data using socket io emit but I can't pass data to socket io emit below my code

app.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var nodes7 = require('nodes7');  // This is the package name, if the repository is cloned you may need to require 'nodeS7' with uppercase S
var conn = new nodes7;
var doneReading = false;
var doneWriting = false;
var variables = { 
        
          TEST7: 'DB1,INT2.3',
          TEST1: 'DB1,X0.0.3',
          TEST2: 'DB1,INT2'
};

conn.initiateConnection({port: 102, host: '127.0.0.1', rack: 0, slot: 1}, connected); 
function connected(err) {
    if (typeof(err) !== "undefined") {
        // We have an error.  Maybe the PLC is not reachable.
        console.log(err);
        process.exit();
    }
    conn.setTranslationCB(function(tag) {return variables[tag];});  // This sets the "translation" to allow us to work with object names
    conn.addItems(['TEST7','TEST1']);
    //conn.writeItems('TEST2', 90, valuesWritten);
    setInterval(function(){
        conn.readAllItems(valuesReady);
    },1000)
    
}

function valuesReady(anythingBad, values) {
    if (anythingBad) { console.log("SOMETHING WENT WRONG READING VALUES!!!!"); }
   
    console.log(values.TEST1[0],values.TEST1[1],values.TEST1[2],values.TEST7[0],values.TEST7[1],values.TEST7[2]);
   
    //console.log( typeof(temp));
    doneReading = true;
     
}
function valuesWritten(anythingBad) {
    if (anythingBad) { console.log("SOMETHING WENT WRONG WRITING VALUES!!!!"); }
    console.log("Done writing.");
    doneWriting = true;
}
io.on('connection',function(socket){
    console.log('one user connected '+socket.id);
    socket.emit("channelname", {
        message: "Passing S71200 data"
      });
      
    socket.on('disconnect',function(){
        console.log('one user disconnected '+socket.id);
    });

})

http.listen(3000,function(){
    console.log('server listening on port 3000');
})

I am using interval function because every second data fetch from PLC device, I got all data from values.TEST1[0],values.TEST1[1],values.TEST1[2],values.TEST7[0],values.TEST7[1],values.TEST7[2] this data passing to

io.on('connection',function(socket){
    console.log('one user connected '+socket.id);
    socket.emit("channelname", {
        message: "Passing S71200 data"
      });

    socket.on('disconnect',function(){
        console.log('one user disconnected '+socket.id);
    });

})

Help me to solve this problem

Arasan
  • 13
  • 5
  • Hey @Arasan, if you want to send everyone the same data they you have from PLC then you can use broadcasting from documentation. https://socket.io/docs/v4/broadcasting-events/ Please check your respective version of io server :) – halilcakar May 03 '21 at 22:35
  • Here I found info to handle data https://socket.io/docs/v4/client-socket-instance/. socket.on("data", () => { console.log('data',data) }); I don't know if it works for you though. I'm coming from nodejs side I look for solution to send ASCII string to FX5 PLC via net socket – Zeghra Oct 05 '21 at 08:43

0 Answers0